Docker
easy 26 minDocker packages a program with its files into an image and runs it as an isolated process called a container.
See it run
Start a container step by step, reuse build layers, write into a private layer, wire networks and hit limits.
Operations
docker run
From the command to a running, isolated process.
Step 1 of 6
The command
Request received
docker run nginx becomes a request to the daemon.
Step 1: The command
CLI -> dockerd (REST)image missing: pull layers, check digestswritable layer, network, mountsdockerd -> containerd -> runcnamespaces, cgroups, start process 1- When
- Each start
- You leave with
- One container
runc is gone once the container runs. If dockerd stops, live-restore can keep containers running.
In the real world
The same docker doing real jobs. Pick one and step through it.
A database in a container without a volume.
Step 1 of 3
It works
A's layer
10,000 rows
B's layer
not used
image
database program
No volume
Rows are written into the container's own layer.
Step 1: It works
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Copying everything before installing
majorAny edit invalidates the install and all later steps.
WrongCOPY . .RUN npm ciRightCOPY package*.json ./RUN npm ciCOPY . .A secret in a layer
criticalRemoving it in a later step leaves it in the image.
WrongCOPY key.txt .RUN use key.txt && rm key.txtRightRUN --mount=type=secret,id=key \ use /run/secrets/keyData in the writable layer
criticalIt is deleted with the container.
Wrongdocker run postgres:18Rightdocker run -v pgdata:/var/lib/postgresql \ postgres:18No limits and no log rotation
majorOne container can take the host's memory or fill its disk.
Wrongdocker run appRightdocker run -m 512m --cpus=0.5 \ --log-opt max-size=10m --log-opt max-file=3 app
Interview tips
How to explain it
- A process with a private view.
- Shared layers, one thin layer.
- The kernel is shared.
Build well
- Seldom-changed first.
- Two stages.
- Pin, scan, rebuild.
Reach for it when
- The same build runs everywhere.
- Services differ in dependencies.
- Copies can be replaced.
Watch
- Exit code 137.
- CPU throttling.
- Published ports.
- Root and the Docker socket.