User-Level Intro to Linux

M10 - Linux Containers with Docker

This module introduces the concept of software container using Linux and Docker as underlying technologies.

Topics Studied in this Module

T1 - Docker: The big picture
This first topic will allow you to explore some study material that will provide you with a high-level overview of Docker; its inception, motivations, and goals. You will use this material to participate in a forum-based discussion on selected topics.
T2 - Creating your own Docker Images & Containers
This topic will lead you through the creation of your own Docker images and their deployment as basic Docker containers. You will then apply what you just learned to create a new image and deploy it on your own. We are then going to go a bit beyond the introductory Doker "Get Started" tutorial and explore other features available when running containers.

Learning Objectives

By the end of this module, students will be able to;

  • Identify situations where using containers might be better than using virtualization
  • Explain the benefits of distributing applications as containers
  • Explain the differences between a docker image and a docker container
  • Install docker on a Ubuntu host
  • Access Docker images from registries and run them on a docker host
  • Create new Docker images
  • Administer containers running on a docker host
  • Administer images running on a docker host
  • Push an image to Docker Hub
  • Run an image from Docker Hub
  • Allow a container to mount a folder from the docker host

T1 - Docker: The Big Picture

Let us start with the big picture about containers in general and Docker technologies in particular.

We are going to start this module with a discussion forum (DF) assignment. You will find below some material for you to study, followed by a section providing you guidelines on how to participate in a discussion about what you just studied.

Your instructor will provide you details separately about how you will receive credit for this part of the module.

The following webinar is part of Cerner's Tech Talk series, see http://engineering.cerner.com/ and @CernerEng. It may look a bit dated now (2014) but is still a great introduction to Docker technologies. It is also not too technical in nature so it will help you get familiar with the big ideas underlying Docker.

We will then supplement it with more technical sources and then get you started experimenting with Docker hands-on yourself.

  • Docker for Developers - Jérôme Petazzoni
  • Video: YouTube Slides: SlideShare Slides: PDF
  • As is usual for longer videos, here are links to the main parts;

    • [00:00] Speaker's Introduction
    • [01:39] Talk's Outline
    • [02:15] History of Docker and its Philosophy
    • [09:25] Demo time - What does it look like?
    • [11:23] More History
    • [16:10] Building Containers; dockerfiles, snapshots
    • [21:49] Shipping Containers; docker hub
    • [26:24] Running Containers; efficiency vs. VMs
    • [29:45] Run Any App, Anywhere
    • [32:30] What are Containers Exactly
    • [35:46] The Docker Ecosystem; engine, hub
    • [41:12] Working with Docker

    The following parts are optional;

The Docker website provide a very good overview of the whole technology. Use the link below to review the terminology introduced in the above talk and complete your big picture of Docker.

T2 - Creating your own Docker Images & Containers

We are now ready to learn about the foundations of Docker technology: containers. To do so, we will review the next part of the official Docker "Getting Started" tutorial and show how to perform these tasks in the environment you previously set up.

It is a good idea to follow along so that you have already been through these steps when you start working on the PA-2 assignment below.

You may want to download the Dockerfile, requirements.txt, and app.py files used in the tutorial.

  • Walkthrough for the above tutorial
  • Video Link: ASCIInema.org
  • This video captures the terminal interactions illustrating the steps of the above tutorial. It might come in handy if you prefer a "live" demonstration to supplement the text. Please note that this is the same video that is linked on the Docker tutorial page. It is a ASCIInema recording and thus does not feature a sound track. It is only meant as a recording of what happens in your terminal.

Now that you have a good idea of what Docker is all about, and how it works, let us prepare our workstation to play with some docker containers.

The following are step-by-step instructions to get your computer setup to play with docker containers. Use them to follow-along and install all the necessary tools to be ready to play with Docker containers in the next topic of this module.

Task #1 - Installing VirtualBox

For the purpose of this module, we are going to assume that you want to deploy a new instance of a Linux system in order to experiment with Docker. This Linux system could be installed directly on physical hardware, however, in courses like this one, it is often more practical to run things in virtual machine that are much easier to wipe and re-install when something goes wrong. Therefore, our first step will be to install VirtualBox on your computer.

  • VirtualBox Downloads
  • URL: https://www.virtualbox.org/wiki/Downloads
  • First step is to visit this page and download the installer for the operating system you are using on your computer. Follow the instructions provided by the installer. These should be easy to follow if you have experience installing software.

Once you have installed VirtualBox itself, make sure you also download and install the Extension Pack also available on the above page. Once you have it downloaded, you will be able to tell VirtualBox to install it by simply double clicking the file.

  • VirtualBox User Manual
  • URL: https://www.virtualbox.org/manual
  • Just in case you need to reference to it later on, we provide you also with the link to the online edition of the VirtualBox user manual. You will find that Chapter 2 is a good resource to consult if you bumped into problems during the installation of th software.

Task #2 - Installing a Linux Docker Host

Now that we have software on our computer that allows us to run virtual machines, we are going to create a new virtual machine that will host our docker containers. For this module, we are going to use a Ubuntu distribution of Linux, desktop edition.

  • Ubuntu Downloads
  • URL Link: https://www.ubuntu.com/download
  • First step is for you to visit the above URL and download the .ISO file for the "Ubuntu Desktop" distribution. We recommend that you download the most recent version marked LTS (Long Term Support). This might not be the latest and greatest available version of the Ubuntu Desktop distribution but it will provide you with everything you need for this module and is less likely to differ significantly from what is depicted in the manuals and tutorials we are providing you on this page.
  • Docker for Developers - Jérôme Petazzoni
  • Video: YouTube
  • This video should provide you with step-by-step instructions to instal Ubuntu inside a new VirtualBox virtual machine. The version of Ubuntu Desktop, and of VirtualBox, might be slightly different than those used this semester but the steps will remain the same.

Task #3 - Installing Docker

We are now finally ready to login to our brand new Linux virtual machine and install docker in it.

Task #4 - Testing your Docker Installation

The last step is for us to make sure that your Docker installation is operational. To do so, we are going to following the steps shown in the "Orientation" section of the Docker "Get Started" tutorial.

  • Docker "Get Started" Tutorial - Orientation
  • URL Link: https://docs.docker.com/get-started/
  • Follow the steps detailed in this section of the Docker "get started" tutorial to ensure that you are able to download images and run them as containers in your freshly installed Ubuntu Desktop virtual machine.

Now, you might have noticed that you need to prefix all of your docker commands with sudo. If you want a given user on your system to be able to run docker commands without having to repeatedly prefix them by sudo then follow these two steps;

sudo groupadd docker
sudo usermod -aG docker $USER

You will then have to logout and log back in, but, after you do, you will be able to run all Docker commands without sudo when logged in as that user.

  • Docker - Post Installation Steps for Linux
  • URL Link: https://docs.docker.com/install/linux/linux-postinstall/
  • This page from the official docker documentation website will provide additional details on the above steps. It will also be helpful for several other things you generally want to do right after you finished installing Docker on your Linux system; e.g. start the Docker daemon at boot time.

Assignment - Screenshots

In order to reward you from successfully completing the above hands-on learning activity, you will be given instructions on how to submit screenshots and receive credit for your work.

For this specific assignment, you will have to provide one screenshot showing the completion of each of the above-listed tasks;

Task #1
Show VirtualBox opened on your desktop with a new blank virtual machine created. You will use your first name, last name and the date for the name of this virtual machine; e.g. JohnSmith20180312. Make sure your screenshot shows this virtual machine in the list presented by VirtualBox when you first open it.
Task #2
Show that you logged to your Ubuntu Desktop virtual machine. You will open a terminal window on your Ubuntu desktop and type in it your first name and last name, followed by the date.
Task #3
Show a screenshot of the terminal in your Ubuntu Desktop virtual machine after installation of Docker. As usual, type your first name, last name and the date on the next line of the terminal.
Task #4
Show a screenshot of the terminal in which you ran the hello-world docker image in a container (without using sudo). Once again, the next line in the terminal should show your first name, last name and the date.

The best way to ensure that you learned something from following the above tutorial is to perform a similar task without simply following along. So we are going to dockerize a different application and then learn a few additional features available when running Docker containers.

Let us start by confirming what you most likely already suspect; "dockerizing" is a made-up word.

Task #1 - Taking notes, you should!

So, first step of this learning activity, go back to the tutorial and write down concise notes. Your notes should capture what you would need if you were to have to do a similar work without *GULP* access to the internet!

Now that you are done, close any tabs you have open on the above tutorial and rely only on your notes for the rest of this learning activity.

Task #2 - Working on a brand new image

Our first step will be to take a java application and package it inside a docker container. This will boil down to applying almost exactly the same steps that you studied in the above tutorial; as such you will get an opportunity to make sure you are able to apply directly what you just learned. Of course, we are going to also use a new application so that you have to adapt a bit what you learned and not simply cut and paste what the tutorial tells you to do in a terminal window :)

Create a new dockerfile that will use, as base, an Ubuntu 16.04LTS server image. We will then supplement this image by running the following updates;

Preparing the Ubuntu image
We are going to start off with a few RUN commands in our Dockerfile that will ensure we have all the tools we need.
  • apt-get update # ensures that our base image is up to date
  • apt-get -y install default-jdk # installs the java compiler and runtime
  • svn checkout https://svn.code.sf.net/p/jade/branches/2016-code-revamp jade-code # downloads the Jade software's source code
Preparing the experiment
Once the above are out of the way, we need to compile the Jade software from the source code we just downloaded.
  • mkdir MyFirstExp
  • cd ./jade-code/src ; javac ./jade/*/*.java ; jar cfm /app/MyFirstExp.jar ./Manifest.txt ./jade/*/*.class ; cd -
This command, that you should execute with a single RUN statement in the Dockerfile, will compile the Jade software into a jar file named "MyFirstExp.jar" and deposit it in the "/app" folder of our container.
Copying a cfg file into the container
When the Jade software runs one of its so-called experiments, it starts by reading a configuration file. On the docker host, download the file at this URL by clicking the "Download this file" link. In your Dockerfile, you need to use ADD in order to get that baseline-sga.cfg file inside the container, under the simpler name "/app/cfg"
Running the experiment
The command that you actually need to run the app is the following;
  • CMD ['java', '-jar', 'MyFirstExp.jar']

Task #3 - Running your image

You may now use what you learned in the Docker tutorial to run your new image, using the tag name "jade".

Task #4 - Mounting a local folder inside your container

So far we have relied on the ADD command in the dockerfile to copy a specific cfg file from the docker host into the container. We could have downloaded a variety of cfg files available in the Jade sourceforge repository. Ideally, we would like to be able to use the same image to run any of the config files.

To do so, we are going to remove the ADD line we used so far in our Dockerfile. Instead, we are going to make it so that the docker host shares a folder that the container may read after it is started and where it will find a cfg file already selected.

In such scenarios, we need a way to designate a folder on the docker host and have the container link it to a folder inside the container. This kind of "link" is equivalent of the concept of mounting a file system at a specific folder location in a Linux system. In our case, we are in effect mounting the docker host's folder that we specified inside the docker container that will see it through another folder. As you may guess, this will allow you to run a container, drop files for it in your docker host folder, and have the app running inside the container dump some output data in files inside that same folder.

  • Mounting Folders from Docker Host into Containers
  • Video Link: YouTube
  • This video illustrate how you may configure a docker container to mount a folder named ~/shared-folder/ located in the file system of the docker host, to a folder named /app/DATA/ inside the container.

Now that we added this technique to your repertoire of docker tools, you are going to leverage it to allow the container that you have been working on so far to mount a folder named ~/website/ from the docker host. This folder will be used to provide the container with the files and folder hierarchy we want it to serve.

As a result, any changes to the local data base files will be accessible from the docker host and will also be made available to the next container we run if we have it mount the same folder.

Task #5 - Pushing your image to a repository

We now have a way to build an image that will run an experiment with the Jade software, based on whatever cfg file we have on our docker host when we run the container. This is nice in so far that we can now give the image to others, already built, and let them run containers based on it and the specific cfg files that represent the actual experiments they want to run.

To do so, we are going to "push" our image to the Docker Hub registry.

Start by opening an account on Docker Hub with a user name following the pattern COP3353FirstnameLastnameSemesterYear. An example of such an account would be COP3353JohnSmithSpring2018. Make sure you follow this template if you want to get credit for your work (see the next section).

Once you have your Docker Hub account, apply what you learned in the Docker tutorial to push your "jade" image to it. Make sure you test that you are able to download your image from Docker Hub and run it. The best way to do so, is to test on a brand new Linux virtual machine on which you just installed docker.

Assignment - Submit your image

By the time you are done with the above hands-on learning activity, you should have an image of a new container committed to Docker hub. Your instructor will review your work by pulling your image and running it. You will have to provide the necessary information for them to do so, along with a note indicating which of the above tasks you successfully completed. Details on how you will have to submit this information will be specified by your instructor when they assign you this module.

The following list will provide you with different possible topics you may chose to explore and discuss with your classmates;

  • In what scenarios would you recommend (or not) running a container rather than a virtual machine?
  • In what scenarios would you recommend (or not) pushing images to Docker Hub, as opposed to keeping them local to your machine?
  • Why do we need both snapshots and images in Docker? What are the differences?
  • Why is copy on write storage important to Docker?

Your TODO list for this assignment

Your first task is to pick one of the above topics, prepare a response (about 400 words, if you need an idea of the size) and post it in Canvas to the forum used for this module DF assignment.

Please note that, when you first open the forum, you will not be able to see other students'posts. This is an option available in Canvas that will allow you to post your own work before you have seen what others who picked the same topic had to say. Once you have posted your work, then you will be able to see what others submitted and compare to your own effort.

Your second task is to respond to the arguments made by other classmates in their post. To do so, you need to reply to another student's post on a topic different than the one you selected for your own post.

Requirements for your Posts

Your first post should follow these quality guidelines;

  • Back up each of the claims you make by referring to the study material that was used in this assignment. Your post should not be about personal arbitrary opinions, or guesses, but reflect your understanding of the study material and your ability to leverage it to provide a rational response to one of the proposed questions.
  • When you reference a specific segment of a video, provide a link in your post. To link to a specific time in a YouTube video, right click on the video and select the option providing you with a link to that exact timestamp.
  • When you reference to a specific segment of a document, cut and paste the relevant material in your post but clearly identify it as a quote by presenting it between double quotes.

Your reply to a classmate's post should follow these quality guidelines;

  • Focus on addressing misconceptions or misrepresentations of the information from the study material first.
  • If you do not find such problems in the post, then contribute additional information that expands upon what the original poster shared.
  • Your contribution will have to be original; i.e. do not simply reply "I thought the same thing" while referring to a classmate's reply to the original poster.

This tab provides you with optional resources meant for those who want to learn a bit more about Docker.

Books

The following books are available to USF students as free downloads when logged on the Campus network (including via VPN);

Freely Available Web Resources

YouTube channels;

Official Docker Documentation;

Other resources;