MP 7: Introduction to Docker


This MP will introduce Docker and Dockerfiles. You will first creating a Docker image that runs Ubuntu and python3. Then, you’ll set up a small web server inside your image.

Docker Setup

Important: This week’s MP will not be done on the cluster. Rather, you should install Docker in your local environment and complete the MP that way.

Install docker Community Edition (not Enterprise). This can be found on the Docker website:

Note: Docker is available for Windows, but we recommend using a UNIX-based system if possible.


In this MP, you will learn to write Dockerfiles that do the following actions:

  1. Inherits from the public base image.
  2. Sets Docker-specific labels for the image maintainer
  3. Installs python3.
  4. Sets environment variables.
  5. Writes to a file inside container
  6. Copies files outside the container into the image during the build process
  7. Sets the current user to CS398.
  8. Runs bash when the container is run.

For information on building a Dockerfile, look at the docs.

Part 1

Inherit from the standard ubuntu image on Dockerhub. (Why do we do this?) You will want to look up the FROM command to be able to do this.

You can build and test your Dockerfile by running these commands:

$ docker build -t <image name> .

For example, we can make an image tagged as “part1” by running docker build -t part1 .. Now, you have a container named whatever you specified. Try running:

$ docker run -i -t <image name>

For example, we can run docker run -i -t part1. You should see something like this:


You’ve been launched into bash, and you have made your first container!

Part 2

Once you have your initial Docker container up and running, you can exit out of it by typing exit, or Ctrl-D. Now, we are going to make some modifications to the Dockerfile. Include the following Docker labels in your Dockerfile using the LABEL command.

Now, if you docker build ... and then run docker inspect <image name> outside your container you should be able to see the two labels have been added to your image:

"Labels": {
    "CLASS": "CS398",

Part 3

Stop your container again and with your labels set up, download python3 in the container. A simple way of doing this is by using apt-get commands. You should use the RUN directive in your Dockerfile to accomplish this.

You should be able to do the following once python3 is setup in your container:

root@8a788562c667:/# python3

Note: You might want to run apt-get update before trying to install python3.

Part 4

Next, copy the file info.txt into a directory called /data in your Docker image. You should do this in the Dockerfile, not in the container (Hint: use the ADD or COPY commands). When you run your container, you should be able to see this file in the /data folder.

Once you’ve rebuilt and restarted your container, you should now be able to access the contents of this file:

root@8a788562c667:/# cat /data/info.txt
Karl the fog is the best cloud computing platform

Now, create a new user called cs398. Alter your Dockerfile so that the default shell is logged in as the cs398 user. Also, set and environment variable called SCHOOL to be uiuc. When you log run the dockerfile, you should see something like.

cs398@8a788562c667:/# whoami
cs398@8a788562c667:/# echo $SCHOOL

Part 5

So far, we’ve seen how to inherit from a base Docker image, load data into Docker images, and setup basic environment settings like the active user and environment variables.

Now, we want to explore a slightly more useful and realistic application of Docker: serving a web application. We’ve created a very basic web application in the file called If you have python and flask installed, you can run this application locally to try it out. It accepts HTTP traffic on port 8080.

Suppose we want to containerize this application so that we can more easily deploy it. Here’s the steps you need to follow:

  1. Copy into your container.
  2. Add a RUN command to your Docker image so that it runs pip3 install flask (you will have to also install pip3 if that did not come with your python3 distribution).
  3. Setup your Docker image so that it runs python3 /path/to/ (make sure to use the correct path) when your image starts up. You can do this using the CMD or ENTRYPOINT Dockerfile instructions.
  4. Rebuild your image after completing the above.

Now, we have our web app in a Docker image. To run this as a container with access to the network, run the following command: docker run -p 8080:8080 mp7.

The -p flag tells Docker to bind a port on our image to the port on the host (the machine running the Docker daemon). Essentially, we bind port 8080 in the image to port 8080 on the host. You can read more about this here.

With the container running, you should be able to go to http://localhost:8080 in your browser, and be able to connect to the web application. That’s it! You successfully containerized a web application.


MP 7 is due on Tuesday, April 10th, 2018 at 11:59PM.

You can find starter code and the Git repository where you should submit your code here:

If you have issues accessing your repository, make sure that you’ve signed into Gitlab. If you still do not have access, please make a private post on the course Piazza.

Please write your solutions as indicated in the following file:

… and commit your code to Gitlab like this:

git add .
git commit -m "Completed MP" # Your commit message doesn't need to match this
git push