Week 5 Docker Tutorial

In this week’s lab, you will learn about Docker.

Installation [1]

For macOS and Linux#

Please make sure you have the following things installed and ready to go!

For Windows#

If you have Windows 10 Professional (it doesn’t work in Home), try WSL 2 and Docker for WSL 2. WSL stands for Window Subsystem for Linux. It allows you to run Linux within Windows. That’s what I’ll be using. WSL 2 works faster than WSL1 but it’s harder to set up since it’s still in preview.

If you do not have Windows 10 Professional and you do not want to buy it, follow this blog post to install VirtualBox and a Linux VM so you can follow inside of Linux. I’d suggest using Ubuntu.

Or, if you know PowerShell really well and know how to translate bash commands to PowerShell commands, feel free to install Docker Desktop Community and do everything from PowerShell (honestly it shouldn’t be too bad.)

Verify Docker installation:#

Make sure when you go to a bash prompt and type docker info that it outputs system info and doesn’t error out. This will let you know that everything is working.

Overview

Virtual Machines are relatively heavy-weight; what if you want to spin up machines in an automated fashion? Enter containers!

  • Amazon Firecracker
  • Docker
  • rkt
  • lxc

Containers are mostly just an assembly of various Linux security features, like virtual file system, virtual network interfaces, chroots, virtual memory tricks, and the like, that together give the appearance of virtualization.

Not quite as secure or isolated as a VM, but pretty close and getting better. Usually higher performance, and much faster to start, but not always.

The performance boost comes from the fact that unlike VMs which run an entire copy of the operating system, containers share the linux kernel with the host. However note that if you are running linux containers on Windows/macOS a Linux VM will need to be active as a middle layer between the two.

Comparison between Docker containers and Virtual Machines. Credit: blog.docker.com

Containers are handy for when you want to run an automated task in a standardized setup:

  • Build systems
  • Development environments
  • Pre-packaged servers
  • Running untrusted programs
    • Grading student submissions
    • (Some) cloud computing
  • Continuous integration
    • Travis CI
    • GitHub Actions

Moreover, container software like Docker has also been extensively used as a solution for dependency hell. If a machine needs to be running many services with conflicting dependencies they can be isolated using containers.

Usually, you write a file that defines how to construct your container. You start with some minimal base image (like Alpine Linux), and then a list of commands to run to set up the environment you want (install packages, copy files, build stuff, write config files, etc.). Normally, there’s also a way to specify any external ports that should be available, and an entrypoint that dictates what command should be run when the container is started (like a grading script).

In a similar fashion to code repository websites (like GitHub) there are some container repository websites (like DockerHub)where many software services have prebuilt images that one can easily deploy.

Mandatory xkcd comic:

xkcd-containers

Task 1: Mount Contents Inside of Container

  1. Run the nginx Docker image using the docker run command.
  2. Access the web server and make sure you can see the default page.
  3. Create an index.html page on your local computer.
  4. Modify the docker run command to display your HTML page instead of the default page.

Task 2: Mount Contents inside of Container with Compose

  1. Using the following documentation, write a Docker Compose file to run the nginx container so you can run it without using the docker run command
  2. Make sure that the Docker Compose file mounts the volume so you see your own index.html instead of the default one.

Hints

If you want to look inside of a running container (using a shell) to look at the filesystem or see the running processes, you can use the following command:

$ docker exec -it CONTAINER_ID /bin/bash

If you want to start a container and use a shell to look at the filesystem or see running processes, you can use the following command:

docker run -it IMAGE_NAME /bin/bash

If you want to find a file on a Linux filesystem, try looking at the find command.

If you want to mount volumes in Docker, you might find the -v switch helpful.

Finally, for most distros nginx stores it’s default index.html file to be loaded in /usr/share/nginx/html (For reference, the current docker image of nginx is based on Debian, see here)

References

[1] https://github.com/btholt/complete-intro-to-containers/blob/master/README.md

bars search times arrow-up