Docker
Docker - Theory¶
You already worked with Virtual Machines, which can virtualize the hardware of an entire computer and run different operating systems. Additionally, you have already seen Docker containers in action when you created hosts in the GNS3 environment. Docker offers an alternative to fully virtualizing an entire machine by utilizing containerization. Check the video below for an introduction on Docker's approach.
To read more about the difference between VMs and containers, you can read this blog post.
Docker - Hands-On¶
For this part, you can open a terminal directly on your VM. The CLI prompt should say netlab@netlab-linux:~$.
Basics¶
In Docker, we distinguish between images and containers (also called instances). Images are the pre-built templates or snapshots that package all the software that is required for the intended use case. The use case can be a fresh install of a specific Linux distribution, a pre-installed and pre-configured service (web server, mail server, database, ..), or more. For this, an image can include code and configuration files, runtimes and libraries, as well as other dependencies. Based on such an image, we can launch one or multiple running containers that execute the environment defined in the image.
-
To check which images are available on your machine, you can run
docker images. How many images are listed in your case? -
You can create an instance based on the
ubuntuimage by runningThe output should give you a long ID which is used to uniquely identify that specific instance. For easier reference, we also provided a more memorable name,
my-ubuntu-container.Tip
You can learn more about the options of docker run in the documentation. For now, it is enough to know that
docker run -ditinstantiates a container in the background, and allows us to attach to its console later on. -
You can confirm that the instance has been created using
docker ps(process status). The output contains the ID, base image name, and other high-level information about your running container instances. Following step 2, you can also launch additional instances from the same image and confirm their presence usingdocker ps.Tip
If you create multiple containers and explicitly provide a name, make sure that the name is unique to avoid errors.
-
With the containers running, you can attach to them via
Your prompt should change fromnetlab@netlab-linux:~$toroot@fa00f8e10813:/#, indicating that you are now logged in as root on the container with the respective ID. Create some files or folders inside the container.Tip
In this case, the
containerIDcan be either the ID from the outputs in the previous step (the first few characters are enough as long as they are enough to uniquely identify your container), or the name you chose when running the container. -
Try running
cat /etc/os-releaseinside your container, thenexitback to your netlab VM and run the same command to compare the operating system versions. What can you observe?Hint
This step illustrates some of the power of using containers. The Docker image we used as the basis for our container uses a different version of the same operating system and, as we shall see later, it is even possible to run different Linux distributions in our containers, regardless of the main host's distribution.
-
To clean up, you can stop and remove containers using
docker stop <containerID>followed bydocker rm <containerID>. -
Re-create one of the containers according to step 2. What happened to the files/folders you created in step 4? Discuss how this relates to the concepts of images and containers.
Building Our Own Image¶
We can also define our own image to create containers which run our desired software. As an example, we will create the web server from the fist part of today's lab. To this end, we will use the ubuntu image as a starting point and extend it to fit our needs. The Docker way of doing this involves a Dockerfile which you can think of as a recipe for building images.
-
Download an exemplary Dockerfile project from
https://folk.ntnu.no/stanisll/2024/ttm4175/ttm4175-webserver.zipusing the command line. For this, you can usecurl(it will complain, but will point you to relevant flags) or can consultaproposor a web search to find other command line tools that can be used instead. -
Extract the zip archive using
unzip ttm4175-webserver.zipand navigate into the newly createdttm4175-webserverfolder. -
Read through the commented
Dockerfileand try to understand what it does. You do not need to execute the commands in that file.Tip
You can refer to the Dockerfile manual if you want to learn more about the different Dockerfile-specific instructions like
FROM,RUN, andEXPOSE. For more information about the apt software package manager, you can useman apt. -
Extend the Dockerfile to not only copy the
example.htmlfile, but alsolipsum.txt. You can use thenanoeditor for this. -
To build an image based on the Dockerfile, execute
docker build -t ttm4175-webserver .(don't forget the dot). This will instruct docker to scan the current directory (.) for a Dockerfile and build an image calledttm4175-webserver. -
Confirm that the build was successful by checking
docker images. Then launch an instance of thettm4175-webserverand attach to it (cf. steps 2 and 4 in the Docker basics part). -
Within the container,
- Find out its IP address using tools you already know.
- Navigate to the
/var/wwwdirectory and confirm that the files you specified in theCOPYpart of the Dockerfile are present. - As in the web server exercise, launch a web server on port 80.
- Open a web browser in your VM (not on your laptop) and navigate to the IP address of the container. What do you see?
-
Stop and remove all running containers. Can you find a way of doing it with a single command? Search online!