TDM 20200: Project 12 — 2024

Motivation: Containers are everywhere and a very popular method of packaging an application with all of the requisite dependencies. This project we will learn some basics of containerization in a virtual environment using Alpine Linux. We first will start a virtual machine on Anvil, then create a simple container in the virtual machine. You may find more information about container and relationship between virtual machine and container here: www.redhat.com/en/topics/containers/whats-a-linux-container

Context: The project is to provide very foundational knowledge about containers and virtualization, focusing on theoretical understanding and basic system interactions.

Scope: Python, containers, UNIX

Learning Objectives
  • Improve your mental model of what a container is and why it is useful.

  • Use UNIX tools to effectively create a container.

Make sure to read about, and use the template found here, and the important information about projects submissions here.

Questions

Question 1 (2 pts)

  1. Logon to Anvil and use a bash command to find an available port you may use later

Example code, you may modify or create your own code if needed

#! /bin/bash

for port in {1025..65535}; do
    python3 -c "import socket; s=socket.socket(socket.AF_INET, socket.SOCK_STREAM); result=s.connect_ex(('127.0.0.1', $port)); s.close(); exit(result)"
    if [ $? -ne 0 ]; then
        echo "port $port open"
        break
    fi
done

Question 2 (2 pts)

  1. Launch a virtual machine (VM) on Anvil. (Note that Docker is already pre-installed on Anvil.) Submit the output showing the job id and process id, after you start a virtual machine; it should look like this, for example:

.output
[1] 3152048

The most popular containerization tool at the time of writing is likely Docker. We will Launch a virtual machine on Anvil (which already has Docker pre-installed).

Open up a terminal on Anvil. You may do it from within Jupyter Lab. Run the following code, to ensure that the SLURM environment variables don’t alter or effect our SLURM job.

for i in $(env | awk -F= '/SLURM/ {print $1}'); do unset $i; done;

Next, let’s make a copy of a pre-made operating system image. This image has Alpine Linux and a few basic tools installed, including: nano, vim, emacs, and Docker.

cp /anvil/projects/tdm/apps/qemu/images/builder.qcow2 $SCRATCH

Next, we need to make qemu available to our shell. Open a terminal and run the following code

module load qemu
# check the module loaded
module list

Next, let’s launch our virtual machine with about 8GB of memory and 4 cores. Replace the "1025" with the port number that you got from question 1.

qemu-system-x86_64 -vnc none,ipv4=on -hda $SCRATCH/builder.qcow2 -m 8G -smp 4 -enable-kvm -net nic -net user,hostfwd=tcp::1025-:22 &
  • 1025 is an example port number; this needs to be replaced with your port number!

Next, it is time to connect to our virtual machine. We will use ssh to do this.

ssh -p 1025 tdm@localhost -o StrictHostKeyChecking=no

If the command fails, try waiting a minute and rerunning the command — it may take a minute for the virtual machine to boot up.

When prompted for a password, enter purdue. Your username is tdm and password is purdue.

Finally, now that you have a shell in your virtual machine, you can do anything you want! You have superuser permissions within your virtual machine! For this question, submit a screenshot showing the output of hostname from within your virtual machine!

Question 3 (2 pts)

  1. Use df -h to check the disk space.

  2. Use printenv to get the user’s environment variables. Choose 2 of those environment variables and explain their meanings.

  3. Use ls to list the files in the current directory

    • You may refer to the following sample code (or create your own approach)

    • If ls does not return anything, use ls -la

    df -h
    printenv
    ls

Question 4 (2 pts)

  1. Write and execute a shell script that calculates the number of files in the current directory and displays the result in a formatted message.

echo 'echo "There are $(ls | wc -l) files in the current directory."' > countFiles.sh
  • run the shell script

chmod +x countFiles.sh
./countFiles.sh

Question 5 (2 pts)

After you complete the previous questions, you can see that you can use the virtual machine just like your own computer. Now use the following steps, to use Docker within the virtual machine to create and manage a container. Run all the commands in your terminal. Copy the output to your Jupyter Lab cells.

  1. List the docker version inside the virtual machine

    docker --version
  2. Pull the "ubuntu" image from Docker Hub

    docker pull ubuntu
    .Run a container based on the "ubuntu" image
    docker run -it ubuntu bash

    When the command runs, docker will create a container from the ubuntu image and run it.

  3. Once inside the container shell, you should see the prompt changed to root@. Run the following command to install cowsay

    apt-get update && apt-get install -y cowsay
  4. Now find the directory that cowsay locates. Go to that directory to run cowsay with following command

    ./cowsay "Your greetings here :)"
  5. Use exit to leave the container

    exit
  6. List the container(s) with following command. It will provide you with a list of all of the containers that are currently running.

    docker ps -a
  7. After you confirm that the container ran successfully, you may using following command to remove it.

    docker rm [Container_id]

    Replace [Container_id] with the id that you got from previous question.

Project 12 Assignment Checklist

  • Jupyter Lab notebook with your code, comments and output for the assignment

    • firstname-lastname-project12.ipynb

  • bash file with code and comments for the assignment

    • firstname-lastname-project12.sh

  • Submit files through Gradescope

Please make sure to double check that your submission is complete, and contains all of your code and output before submitting. If you are on a spotty internet connection, it is recommended to download your submission after submitting it to make sure what you think you submitted, was what you actually submitted.

In addition, please review our submission guidelines before submitting your project.