From point A to point B
Point A: you use Docker every day, but inside it is still a black box: you do not know what receives your commands, why one container cannot see another's processes or where your files end up. Point B: by the end of this chapter you will know the pieces of Docker Engine, control it remotely and safely, limit a container's CPU and memory and choose wisely between the container layer, a volume and a bind mount for your data.
The pieces of Docker Engine
When you install Docker on Linux, you install three pieces that work together:
- The daemon,
dockerd: a background process that manages Docker objects: images, containers, volumes and networks. - The REST API: the interface through which any program gives orders to the daemon. Locally it listens on a Unix socket,
/var/run/docker.sock. - The client,
docker: the command line you use. It does not create containers: it translates each command into an API call.
The client only talks to the API; the daemon does the work, backed by containerd and runc. Diagram: The House of Routing.
Underneath, the daemon delegates to containerd, which manages the container lifecycle, and to runc, which creates each one with kernel features. These are the same pieces Kubernetes uses.
docker version # two blocks: Client and Server (the daemon)
docker info # what the daemon knows: containers, storage, cgroups
Controlling a remote Docker without opening holes
Since the client only talks to an API, it can be on another machine. Older courses do this by exposing the API over TCP on port 2375 (docker -H tcp://server:2375 ps). Do not do it: that port has no encryption or authentication, and access to the Docker API is equivalent to being root on that server.
The recommended way is SSH, which reuses the keys and the user you already have:
# A one-off command against the remote server
docker -H ssh://admin@10.10.10.100 ps
# Or save it as a context and switch between machines
docker context create server --docker "host=ssh://admin@10.10.10.100"
docker context use server
docker run -d nginx:stable # runs on the remote server
docker context use default # back to your machine
If you need TCP, use port 2376 with TLS and client certificates.
docker group and the /var/run/docker.sock socket also give full control of the host. Add to the group only people who would administer the server anyway, and do not mount the socket inside containers unless you know exactly why.Isolation: the PID namespace
In chapter 1 you saw that Docker isolates containers with kernel namespaces. The easiest one to see is the PID namespace. On Linux, each process has a unique identifier, and the first one, PID 1, is the one that boots the system. A container has its own PID space: inside, its main process is PID 1; outside, to the host, it is just another process with a different number.
docker run -d --name sleeper busybox:1.37 sleep 3600
# Inside the container: sleep is PID 1 and sees nothing else on the system
docker exec sleeper ps
# From the host: the same process, with a different PID
docker top sleeper
ps -ef | grep "sleep 3600" # on Linux
It is the same process seen from two namespaces. That is why a container cannot see or send signals to the processes of other containers or of the host.
Resource limits with cgroups
Isolating is not limiting. By default, a container can consume all of the host's CPU and memory, and a single runaway container can starve the others. To prevent this, Docker uses kernel cgroups (control groups):
docker run -d --cpus=0.5 nginx:stable # at most half a CPU
docker run -d --cpus=0.25 --memory=128m nginx:stable # a quarter of a CPU and 128 MB
docker stats --no-stream # actual usage against each container's limit
- CPU: if the container wants more, it is slowed down. It is not stopped.
- Memory: if it exceeds the limit, the kernel kills the process (OOM kill). You will see it in
docker inspectas"OOMKilled": true.
requests and limits are exactly this: the kubelet translates each limit into cgroups on the node.Where Docker stores things
On Linux, everything Docker manages lives under /var/lib/docker. Take a look with root permissions:
sudo ls /var/lib/docker
# containers image network overlay2 volumes ...
docker info -f '{{.Driver}}' # the storage driver in use
docker system df # how much space images, containers and volumes take
How layers are stored depends on the storage driver. Docker picks the best one available for your system, and on Linux it is usually overlay2. Since Docker Engine 29, new installations use the containerd image store by default, so in docker info you may see overlayfs instead of overlay2. For your day-to-day work nothing changes. Volumes are still in /var/lib/docker/volumes.
aufs or devicemapper. They are no longer used in current Docker versions.Layers and copy-on-write
In chapter 4 you saw that an image is a stack of read-only layers. Those layers are shared: if ten images start from python:3.13-slim, the base is stored only once on disk, and each new image only adds the layers that did not exist yet.
When it creates a container, Docker adds one more layer on top, the only read-write one. Everything the container writes, logs, temporary files, data, goes to that layer.
The image layers never change; what the container writes goes to its own layer or, if it must last, to a volume. Diagram: The House of Routing.
What if the container modifies a file that comes with the image, such as the application code? Docker applies copy-on-write: it copies the file to the writable layer and modifies the copy. The image stays intact, and other containers from the same image do not see the change. To change the image you have to rebuild it with docker build.
The practical consequence: the writable layer is removed with the container. Try it:
docker run --name temp ubuntu:24.04 bash -c "echo hello > /note.txt"
docker diff temp # A /note.txt: added in the container layer
docker rm temp # and the note disappears with it
Volumes and bind mounts
For data to survive the container, it is stored outside it. There are two ways:
Volume: Docker manages the location
docker volume create mysql-data
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=changeme \
-v mysql-data:/var/lib/mysql mysql:8.4
docker volume ls
docker volume inspect mysql-data # Mountpoint: /var/lib/docker/volumes/mysql-data/_data
If you use a volume name in -v that does not exist, Docker creates it automatically.
Bind mount: you choose the folder
mkdir -p /opt/data/mysql
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=changeme \
-v /opt/data/mysql:/var/lib/mysql mysql:8.4
The difference is in the first term: a name is a volume; a path that starts with / (or ./) is a bind mount.
The explicit syntax: --mount
--mount does the same with key=value pairs. It is longer, but it leaves no room for doubt, and if a bind mount's folder does not exist, it returns an error instead of creating it empty:
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=changeme \
--mount type=volume,source=mysql-data,target=/var/lib/mysql mysql:8.4
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=changeme \
--mount type=bind,source=/opt/data/mysql,target=/var/lib/mysql mysql:8.4
| Volume | Bind mount | |
|---|---|---|
| Where the data lives | Wherever Docker decides (/var/lib/docker/volumes) | At the host path you choose |
| Managed with | docker volume | File system tools |
| Portability | High: does not depend on the host's structure | Low: the path has to exist on every machine |
| Typical use | Database and application data | Code under development, configuration files |
For application data, Docker recommends volumes. Use bind mounts when you need to see or edit the files from the host, for example your code while you develop.
Lab: MySQL with data that survives
Create a database, remove the container and check with a new container that the data is still there.
docker volume create mysql-data
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=changeme \
--mount type=volume,source=mysql-data,target=/var/lib/mysql \
--memory=512m mysql:8.4
# Wait a few seconds for it to start (docker logs -f mysql-db) and create data
docker exec mysql-db mysql -uroot -pchangeme -e \
"CREATE DATABASE thor; CREATE TABLE thor.routes (name VARCHAR(40)); INSERT INTO thor.routes VALUES ('modern-cloud');"
docker rm -f mysql-db # the container disappears...
docker run -d --name mysql-db2 -e MYSQL_ROOT_PASSWORD=changeme \
--mount type=volume,source=mysql-data,target=/var/lib/mysql mysql:8.4
docker exec mysql-db2 mysql -uroot -pchangeme -e "SELECT * FROM thor.routes;"
# modern-cloud ← ...but the data is still in the volume
# Clean-up
docker rm -f mysql-db2 && docker volume rm mysql-data
Repeat the exercise without --mount: when you recreate the container, the thor database no longer exists, because it lived in the writable layer.
Challenge
Answer before you open the solution. Getting it wrong here is the cheapest way to learn.
1. When you run docker run, which piece actually creates the container?
The daemon, dockerd, backed by containerd and runc. The client only sends the order to the API.
2. A colleague suggests opening port 2375 to manage Docker from their laptop. What do you suggest instead?
Use SSH: docker -H ssh://user@server or a docker context. Port 2375 has no encryption or authentication and gives root control of the server.
3. A container with --memory=128m tries to use 300 MB. What happens?
The kernel kills the process for running out of memory (OOM) and the container stops. With CPU, by contrast, it is only slowed down.
4. Inside a container you modify a file that came with the image. Does the image change?
No. Through copy-on-write, the file is copied to the container's writable layer and the copy is modified. The image is read-only.
5. What does -v data:/var/lib/mysql mount, and what does -v /data:/var/lib/mysql mount?
The first, a volume called data; the second, the host folder /data (bind mount).
What you take away
- Docker Engine = client, REST API and daemon; underneath, containerd and runc.
- For a remote Docker, SSH or TLS on 2376. Never the unprotected API on 2375.
- Namespaces isolate what a container sees; cgroups limit what it consumes.
--cpusslows down; exceeding--memorykills the process.- Image layers are read-only and shared; the container writes to its own layer, which dies with it.
- Data that must last: volumes for applications, bind mounts for editing from the host.
In the next chapter: networking in Docker, how containers talk to each other and to the outside world.
Official resources
- Docker overview: architecturedocs.docker.com
- Protect the Docker daemon socketdocs.docker.com
- Docker contextsdocs.docker.com
- Resource constraintsdocs.docker.com
- Storage driversdocs.docker.com
- Volumesdocs.docker.com
- containerd image storedocs.docker.com
Source and credits. Chapter written from Jesús Espinoza's study notes on KodeKloud's Docker for Absolute Beginners course (December 2024). Text drafted with AI assistance; original diagrams by The House of Routing. Technical details were checked against the official Docker documentation.