Skip to main content

Understanding Docker Architecture

Learn how the docker CLI, dockerd, containerd and runc work together to start a container on Linux, and how to harden the daemon and socket.

44 Terms

Overview and What You Will Learn

Docker Engine is a client-server system: the docker CLI sends API requests to a background daemon called dockerd, which delegates container execution to containerd, which in turn uses a small per-container shim and the runc binary to create the isolated process. In this lab, you will watch each layer on a real Linux host, call the Docker API directly through its socket, prove that a container is just a process with namespaces, and configure the daemon for production.

  • Naming the components and what each one does
  • Tracing docker run from the CLI down to runc
  • Calling the Docker API directly over the Unix socket
  • Inspecting containers through ctr and runc
  • Configuring daemon.json with log rotation and live-restore
  • Understanding why the Docker socket is equivalent to root access

Why This Matters in Production

At a company like Grab, every host that runs containers has a Docker daemon on it. When a container will not start, a build hangs, or the security team asks about the Docker socket, the engineers who know which layer owns which job find the fault in minutes. Engineers who only know docker run are stuck. The same knowledge explains why Kubernetes removed its built-in Docker support in version 1.24 and talks to containerd directly, a change that confused many teams.

Core Principles

A docker run command passes through a chain of components, and each one has a single responsibility. The CLI and the daemon talk over a REST API on a Unix socket, the daemon talks to containerd over gRPC, and containerd launches runc through a shim.

flowchart TD CLI[docker CLI
sends API requests] -->|REST over socket| D[dockerd
images, networks, API] D -->|gRPC| C[containerd
container lifecycle] C --> S[containerd-shim
stays running] S --> R[runc
sets up isolation, exits] R --> P([Container process
for example nginx]) classDef client fill:#D1FAE5,color:#1E293B classDef daemon fill:#326CE5,color:#FFFFFF classDef runtime fill:#8B5CF6,color:#FFFFFF classDef proc fill:#F97316,color:#FFFFFF class CLI client class D,C daemon class S,R runtime class P proc

The components differ in how long they live:

Component Role Lifetime
docker CLI Sends API requests and prints results One command
dockerd Manages images, networks, volumes and the API Long-running service
containerd Manages container lifecycle and snapshots Long-running service
containerd-shim Keeps the container running and reports its exit One per container
runc Creates namespaces and cgroups, then starts the process Exits after start

The sequence for one docker run nginx looks like this:

sequenceDiagram participant CLI as docker CLI participant D as dockerd participant C as containerd participant R as shim and runc CLI->>D: Pull image if missing CLI->>D: Create container CLI->>D: Start container D->>C: Create and start task C->>R: Launch via shim R-->>C: Process running, runc exits C-->>D: Container running D-->>CLI: Container ID

runc follows the OCI runtime specification. It reads a root filesystem and a config.json, creates the PID, network, mount, UTS and IPC namespaces plus the cgroups, starts the entrypoint, and exits. The shim remains as the container's parent process, so the container survives a restart of containerd or dockerd, and the shim reports the exit code afterwards.

Which component pulls and stores images depends on the image store in use. With the classic store, dockerd handles images itself. With the containerd image store, containerd does. Either way, the actual running of the process is containerd, the shim and runc.

Kubernetes removed its dockershim component in version 1.24 and talks to a CRI runtime such as containerd directly. Images built with Docker still run unchanged, because they follow the OCI image format.

Detailed Step-by-Step Practical Lab

This lab needs a Linux host with Docker Engine, sudo access and jq. It starts a few containers and changes the daemon configuration, so use a test machine.

  1. Check that the daemon is running and see the versions and settings it reports. Version numbers will differ on your host:
Bash
sudo systemctl status docker --no-pager
sudo systemctl status containerd --no-pager
docker version
docker info | grep -E "Server Version|Storage Driver|Cgroup Driver|Live Restore|Runtimes"

Daemon logs are the first place to look when something fails to start:

Bash
sudo journalctl -u docker.service --no-pager -n 30
  1. Start a container and look at the process chain on the host. The container's main process has the shim as its parent, and the shim has been reparented to init:
Bash
docker run -d --name trace-test nginx:1.27-alpine
ps -eo pid,ppid,cmd --forest | grep -E "dockerd|containerd|nginx" | grep -v grep
docker top trace-test

The PID shown by docker top is the real host PID. A container is a process on the host kernel, not a separate machine.

  1. Call the Docker API directly through the socket, exactly as the CLI does. This needs permission to use the socket, so use sudo if your user is not in the docker group:
Bash
sudo curl --silent --unix-socket /var/run/docker.sock \
http://localhost/version | jq -r '.Version, .ApiVersion'
sudo curl --silent --unix-socket /var/run/docker.sock \
http://localhost/containers/json | jq -r '.[].Names[]'

The second command lists the same container names that docker ps shows.

  1. Watch the daemon's event stream while you stop and start the container. Run the first command in one terminal and the second in another:
Bash
docker events --filter container=trace-test
docker stop trace-test && docker start trace-test
  1. Look at the same container from the containerd and runc side. Docker keeps its containers in the moby namespace of containerd:
Bash
sudo ctr --namespace moby containers list
sudo ctr --namespace moby tasks list
sudo runc --root /run/docker/runtime-runc/moby list

Notice that runc lists the container as running even though the runc process itself is not running. It records state on disk and exits.

  1. Prove that the container is a process with its own namespaces. Compare the namespace IDs of the container's process with your own shell. Different IDs mean separate PID, network and mount views:
Bash
PID=$(docker inspect --format '{{.State.Pid}}' trace-test)
sudo readlink /proc/$PID/ns/pid /proc/$PID/ns/net /proc/$PID/ns/mnt
readlink /proc/self/ns/pid /proc/self/ns/net /proc/self/ns/mnt
  1. Configure the daemon for production. This command replaces /etc/docker/daemon.json, so it first backs up any existing file. Merge by hand if you already have custom settings:
Bash
sudo cp /etc/docker/daemon.json /etc/docker/daemon.json.bak 2>/dev/null || true
sudo tee /etc/docker/daemon.json > /dev/null << 'EOF'
{
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "3"
},
"default-ulimits": {
"nofile": {
"Name": "nofile",
"Hard": 64000,
"Soft": 64000
}
},
"live-restore": true
}
EOF
sudo dockerd --validate --config-file=/etc/docker/daemon.json
sudo systemctl restart docker

This first restart stops running containers, because live restore was not on yet, so trace-test stops. Log options apply only to containers created after the change.

  1. Now prove that live restore works. Start a container, restart the daemon, and check that the container kept running:
Bash
docker run -d --name live-test nginx:1.27-alpine
sudo systemctl restart docker
docker ps --filter name=live-test --format "{{.Names}} {{.Status}}"

The status still reads Up with the original uptime, because the daemon restarted while the shim kept the container alive. For later changes to reloadable settings you can send a reload instead of a restart:

Bash
sudo systemctl reload docker
Remember

Not every daemon.json setting can be reloaded. Log driver options and default ulimits need a full restart, and live restore does not work in Swarm mode.

  1. Look at the Docker socket and see why it must be protected. Any container that mounts it can control every container on the host:
Bash
ls -l /var/run/docker.sock
getent group docker
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
docker:27-cli docker ps --format "{{.Names}}"

The last command runs inside a container and still lists live-test and the other host containers. An attacker with that access could just as easily start a privileged container that mounts the host filesystem.

  1. Clean up:
Bash
docker rm -f trace-test live-test
sudo cp /etc/docker/daemon.json.bak /etc/docker/daemon.json 2>/dev/null || sudo rm -f /etc/docker/daemon.json
sudo systemctl restart docker

Production Best Practices & Common Pitfalls

Common Mistake

Treating containers as small virtual machines. A container is a Linux process on the host kernel, isolated by namespaces and cgroups, and it shows up in ps on the host. A kernel vulnerability or an over-privileged container therefore affects the whole host.

Security

Members of the docker group effectively have root on the host, because they can start a container that mounts the host filesystem. Add only trusted users, and on shared servers use rootless Docker or Podman, which do not need a root-owned daemon.

Tip

Set "live-restore": true in /etc/docker/daemon.json before running real workloads. Daemon upgrades and crashes then no longer stop your containers.

  • Never mount /var/run/docker.sock into untrusted containers. The socket is equivalent to root, so treat it like an SSH private key. If CI needs to build images, use a daemonless builder such as Buildah or Kaniko, or an isolated rootless builder.
  • Never expose the daemon over plain TCP. Port 2375 has no authentication. Use TLS on 2376 or reach remote hosts over SSH with docker context create NAME --docker "host=ssh://rahul@10.0.1.50".
  • Always rotate container logs. The default json-file driver does not rotate, so set max-size and max-file or a long-running container can fill the disk.
  • Remember that Docker Desktop is different. On macOS and Windows the daemon runs inside a Linux VM, so the socket, port forwarding and bind mounts all pass through that VM, which is why file-heavy mounts are slower than on native Linux.
  • Use named volumes for databases on Docker Desktop. Bind mounts cross the VM boundary and slow disk-heavy workloads.
  • Debug in layers. Check dockerd logs first, then containerd, then runc, because each layer owns different failures.

Quick Reference & Troubleshooting Commands

Command Description
sudo systemctl status docker containerd Check that both services are running
sudo journalctl -u docker.service -f Follow live daemon logs
docker info Show storage driver, runtimes and live restore state
docker events Stream daemon events as they happen
sudo ctr --namespace moby containers list List containers as containerd sees them
sudo runc --root /run/docker/runtime-runc/moby list List containers as runc sees them
sudo dockerd --validate --config-file=FILE Check a daemon config file before restarting
docker system df Show disk used by images, containers and volumes
Problem Cause Fix
Cannot connect to the Docker daemon Daemon is stopped or socket is missing Run sudo systemctl start docker
permission denied on the socket User is not in the docker group Add the user to the group, understanding it grants root
Daemon will not start after an edit Invalid JSON in daemon.json Run dockerd --validate and fix the file
Containers stop on daemon restart live-restore is off Enable it and restart once
Containers will not start though the daemon runs containerd is unhealthy Check and restart the containerd service
Disk keeps filling on build hosts Unused images and logs Run docker system prune and set log rotation

Resources

Explore More in Docker Fundamentals and Container Lifecycle

All 4 Topics

Frequently Asked Questions

Is Understanding Docker Architecture free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Understanding Docker Architecture topic cover?

Learn how the docker CLI, dockerd, containerd and runc work together to start a container on Linux, and how to harden the daemon and socket.

Found a mistake, an outdated step or a broken link?