Containers with Docker and Kubernetes · Course book
Chapter 1: Containers from the Ground Up
"It works on my machine" is one of the oldest complaints in software. An application that runs perfectly on a developer's laptop fails on a server because a library is a different version, a setting is missing or the operating system differs. Docker solves this by packaging an application together with everything it needs into a container that runs the same way everywhere. This chapter explains what containers are and how they differ from virtual machines, the commands for running them, how images are built in layers, how containers connect to networks and storage, and how several containers are run together with Docker Compose.
Containers and virtual machines
Both virtual machines and containers let one physical computer run many isolated applications, but they draw the boundary in different places. A virtual machine simulates a complete computer, so each VM runs its own full operating system on top of a hypervisor. A container shares the host computer's operating system kernel and isolates only the application's processes, files and network view. The container holds just the application and the libraries it needs.
| Property | Virtual machine | Container |
|---|---|---|
| Contains | A full operating system plus the application | The application and its libraries |
| Typical size | Gigabytes | Megabytes to a few hundred megabytes |
| Start-up time | Minutes | Seconds or less |
| Isolation | Very strong: separate kernels | Strong, but the kernel is shared |
| Can run a different OS kernel | Yes, such as Windows on a Linux host | No: Linux containers need a Linux kernel |
The two are often combined: cloud providers rent virtual machines, and companies run many containers inside each one. Because Linux containers need a Linux kernel, Docker on Windows and macOS runs a small Linux virtual machine behind the scenes to host them.
Images and running containers
An image is a read-only template: the files, libraries and settings needed to run an application. A container is a running instance of an image, in the way that an object is an instance of a class. From one image you can start any number of identical containers. Images are shared through registries, the best known being Docker Hub, which hosts official images for common software such as databases and web servers.
A handful of commands cover everyday use.
docker run nginx # download the image if needed, then start a container
docker run -d nginx # run it in the background ("detached")
docker ps # list running containers (add -a for stopped ones too)
docker stop <id> # stop a container
docker rm <id> # remove a stopped container
docker images # list downloaded images
docker pull redis:7 # download a specific version of an image
docker exec -it <id> sh # open a shell inside a running container
The part after a colon is a tag, usually a version. Without one, Docker assumes latest, which can change without warning, so production systems name exact versions. A container lives only as long as the main process inside it: when that process finishes, the container stops. That is why docker run ubuntu exits immediately, since there is nothing for it to do, while docker run nginx keeps running, because the web server waits for requests.
Two more commands help when something goes wrong. docker logs shows everything a container has printed, which is usually the fastest way to find out why it stopped. docker inspect prints a detailed description of a container or image, including its environment variables, network addresses and mounted storage. Over time stopped containers and unused images pile up and fill the disk; docker system prune removes them in one step, after asking for confirmation.
Configuration is usually passed in as environment variables with -e, for example docker run -e APP_COLOR=blue myapp. The same image can then behave differently in testing and production without being rebuilt.
Building images in layers
You build your own images from a Dockerfile, a text file listing the steps.
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
FROM chooses a base image, WORKDIR sets the working folder, COPY brings files in from your computer, RUN executes a command while building and CMD sets the default command when a container starts. docker build -t myapp . turns the file into an image named myapp.
Each instruction produces a layer, a set of file changes stacked on the one before. Layers are cached: if an instruction and everything before it are unchanged, Docker reuses the stored layer instead of running the step again. Order therefore matters. In the file above, dependencies are installed before the rest of the code is copied, so editing app.py rebuilds only the last two layers, while the slow pip install step is reused. Reversing those lines would reinstall every dependency on every code change.
CMD and ENTRYPOINT both set what runs at start-up, with a difference. CMD is a default that the user can replace entirely by adding a command to docker run. ENTRYPOINT fixes the program, and anything the user adds becomes arguments to it. An image with ENTRYPOINT ["sleep"] and CMD ["5"] sleeps for 5 seconds by default, and docker run thatimage 10 sleeps for 10.
Once built, an image can be shared by pushing it to a registry. Its name then includes the registry and account, as in yourname/myapp:1.0, and docker push uploads it after you log in. Anyone with access can then pull and run exactly the same image. Companies often run a private registry so that their images are available to their own servers but not to the public. Because images are made of layers, pushing a new version uploads only the layers that changed, which keeps updates small.
Networking and storage
Containers run on a private network inside the host. To reach a web server in a container from outside, you publish a port, mapping a port on the host to a port in the container: docker run -p 8080:80 nginx makes the container's port 80 reachable on the host's port 8080. Several containers can each listen on port 80 inside, as long as each is mapped to a different host port. Containers attached to the same user-defined network can reach each other by name, so an application container can connect to a database container simply by calling it db.
A container's own files disappear when the container is removed. Data that must survive, such as a database's contents, belongs in a volume, storage managed by Docker that exists independently of any container: docker run -v dbdata:/var/lib/mysql mysql keeps the database files in a volume named dbdata. A bind mount instead maps a folder on your own computer into the container, which is handy during development because edits on your computer appear inside the container immediately.
Docker creates a few networks automatically. The default bridge network gives each container a private address on the host, the host network lets a container use the host's network directly with no isolation, and none gives it no network at all. For most applications, a user-defined bridge network, created with docker network create, is the right choice, because it adds the name-based lookup between containers that the default bridge lacks.
Several containers together: Compose, Swarm and beyond
Real applications have several parts: a web front end, an application server, a database, a cache. Starting each with a long docker run command is tedious and error-prone. Docker Compose describes them all in one YAML file, typically docker-compose.yml:
services:
web:
build: .
ports:
- "8080:5000"
environment:
- REDIS_HOST=cache
cache:
image: redis:7
docker compose up builds and starts both services, on a shared network where web can reach cache by name, and docker compose down stops and removes them. The whole environment becomes a file that anyone on the team can run with one command.
Compose runs everything on one machine. To spread containers across several machines, restart failed ones automatically and scale services up and down, you need a container orchestrator. Docker includes a simple one, Docker Swarm, which joins several hosts into a cluster and runs a requested number of copies of each service across them. The most widely used orchestrator, though, is Kubernetes, introduced in Chapter 4 of this course.
Check your understanding
- Give two differences between a container and a virtual machine.
- What is the difference between an image and a container?
- Why does docker run ubuntu stop immediately while docker run nginx keeps running?
- Why should a Dockerfile copy requirements.txt and install dependencies before copying the rest of the code?
- An image has ENTRYPOINT ["echo"] and CMD ["hello"]. What does docker run image goodbye print? (Answer: goodbye.)
- Which command makes a container's port 3000 available on the host's port 80? (Answer: docker run -p 80:3000 image.)
- Why should a database's files be stored in a volume?
This course book is available to read online only at tech-academy.chrysolites.org.