Containers and Containerization
Learning Objectives
- You know the terms containerization, container, and orchestration.
- You know of tools such as Docker, Docker Compose, and Podman.
- You can run a container with Docker and run a multi-container application with Docker Compose.
Containerization
Containerization refers to bundling application code and dependencies into a container image that can be run consistently across environments. One of the most popular containerization tools is Docker.
- Visit Docker’s official guidelines for Installing Docker Engine and install it to your computer.
You may also use Podman, which is a drop-in replacement for Docker. The main difference between Docker and Podman is that Docker requires root access, while Podman does not. The examples in this course have been tested with Docker.
Running Docker
Once you have installed Docker, you can verify the installation by running the command docker run hello-world. This command downloads a test image from Docker Hub — a centralized image repository — and runs it in a container on your computer. If the installation is successful, you should see a message that is similar to the following:
Hello from Docker!
This message shows that your installation appears to
be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from
the Docker Hub. (amd64)
3. The Docker daemon created a new container from that image
which runs the executable that produces the output you
are currently reading.
4. The Docker daemon streamed that output to the Docker
client, which sent it to your terminal.
To try something more ambitious, you can run an
Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Each Docker image has been built from a Dockerfile, which is a text file that contains instructions for building the image. We’ll create one in the next chapter.
Docker Compose
Docker is useful for running individual containers, but for multi-container applications, you need an orchestration tool. For Docker, the choice is Docker Compose, which allows defining and running multi-container applications.
- Visit Docker’s official guidelines for Installing Docker Compose and install it to your computer.
Once you have installed Docker Compose, you may proceed.
Follow along the example, creating the files and folders as instructed locally. At the end of the example, you are expected to submit the walking skeleton as a zip file to the course platform.
Now, open up a terminal and create a new directory called walking-skeleton to a directory of your choice (you could e.g. create a new folder called wsd-coursework and create the walking-skeleton folder into it). Inside the directory, create a file called compose.yaml with the following content:
services:
hello-world:
image: hello-world
The compose.yaml file is a configuration file for Docker Compose. The above configuration defines a service called hello-world that uses the hello-world image from Docker Hub (that we tried out earlier).
Save the file and run the command docker compose up --build in the same folder with the file. This command reads the configuration file and starts the services defined in it. In our case, the image hello-world is downloaded from Docker (if you did not run the example above) and it is run as a service called hello-world in a container.
The output should be similar to the following (the command is prefixed with $ to indicate that it is run in the terminal):
$ docker compose up --build
[+] Running 1/0
✔ Container walking-skeleton-hello-world-1
Attaching to hello-world-1
hello-world-1 |
hello-world-1 | Hello from Docker!
hello-world-1 | This message shows that your installation
appears to be working correctly.
hello-world-1 |
hello-world-1 | To generate this message, Docker took the
following steps:
hello-world-1 | 1. The Docker client contacted the Docker daemon.
hello-world-1 | 2. The Docker daemon pulled the "hello-world"
image from the Docker Hub. (amd64)
hello-world-1 | 3. The Docker daemon created a new container
from that image which runs the executable that
produces the output you are currently reading.
hello-world-1 | 4. The Docker daemon streamed that output to the
Docker client, which sent it to your terminal.
hello-world-1 |
hello-world-1 | To try something more ambitious, you can run an
Ubuntu container with:
hello-world-1 | $ docker run -it ubuntu bash
hello-world-1 |
hello-world-1 | Share images, automate workflows, and more with a
free Docker ID:
hello-world-1 | https://hub.docker.com/
hello-world-1 |
hello-world-1 | For more examples and ideas, visit:
hello-world-1 | https://docs.docker.com/get-started/
hello-world-1 |
hello-world-1 exited with code 0
Summary
In summary:
- Containerization packages an application and its dependencies into a lightweight, portable unit called a container.
- Docker is one of the most widely used platforms for building, deploying, and managing containers.
- Docker Compose is an orchestration tool that simplifies the process of defining and running applications that consist of multiple containers.
At this point, you should have a folder called walking-skeleton with a file called compose.yaml inside it. Within the folder walking-skeleton, the output from running the command tree --dirsfirst is similar to the following.
$ tree --dirsfirst
.
└── compose.yaml