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 runfrom the CLI down torunc - Calling the Docker API directly over the Unix socket
- Inspecting containers through
ctrandrunc - Configuring
daemon.jsonwith log rotation andlive-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.
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:
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.
- Check that the daemon is running and see the versions and settings it reports. Version numbers will differ on your host:
sudo systemctl status docker --no-pagersudo systemctl status containerd --no-pager docker versiondocker 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:
sudo journalctl -u docker.service --no-pager -n 30- 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:
docker run -d --name trace-test nginx:1.27-alpine ps -eo pid,ppid,cmd --forest | grep -E "dockerd|containerd|nginx" | grep -v grepdocker top trace-testThe PID shown by docker top is the real host PID. A container is a process on the host kernel, not a separate machine.
- Call the Docker API directly through the socket, exactly as the CLI does. This needs permission to use the socket, so use
sudoif your user is not in thedockergroup:
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.
- 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:
docker events --filter container=trace-test docker stop trace-test && docker start trace-test- Look at the same container from the
containerdandruncside. Docker keeps its containers in themobynamespace ofcontainerd:
sudo ctr --namespace moby containers listsudo ctr --namespace moby tasks listsudo runc --root /run/docker/runtime-runc/moby listNotice that runc lists the container as running even though the runc process itself is not running. It records state on disk and exits.
- 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:
PID=$(docker inspect --format '{{.State.Pid}}' trace-test) sudo readlink /proc/$PID/ns/pid /proc/$PID/ns/net /proc/$PID/ns/mntreadlink /proc/self/ns/pid /proc/self/ns/net /proc/self/ns/mnt- 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:
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.jsonsudo systemctl restart dockerThis 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.
- Now prove that live restore works. Start a container, restart the daemon, and check that the container kept running:
docker run -d --name live-test nginx:1.27-alpinesudo systemctl restart dockerdocker 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:
sudo systemctl reload dockerRememberNot every
daemon.jsonsetting can be reloaded. Log driver options and default ulimits need a full restart, and live restore does not work in Swarm mode.
- Look at the Docker socket and see why it must be protected. Any container that mounts it can control every container on the host:
ls -l /var/run/docker.sockgetent 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.
- Clean up:
docker rm -f trace-test live-testsudo cp /etc/docker/daemon.json.bak /etc/docker/daemon.json 2>/dev/null || sudo rm -f /etc/docker/daemon.jsonsudo systemctl restart dockerProduction Best Practices & Common Pitfalls
Common MistakeTreating 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
pson the host. A kernel vulnerability or an over-privileged container therefore affects the whole host.
SecurityMembers of the
dockergroup 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.
TipSet
"live-restore": truein/etc/docker/daemon.jsonbefore running real workloads. Daemon upgrades and crashes then no longer stop your containers.
- Never mount
/var/run/docker.sockinto 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-filedriver does not rotate, so setmax-sizeandmax-fileor 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
dockerdlogs first, thencontainerd, thenrunc, 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 |