Docker Basics
Docker solved “works on my machine” by shipping your machine. It’s a good idea. People still find ways to make it hurt, and I’ve watched most of them happen.
Two words you need
Section titled “Two words you need”- Image: a read-only template. Built once, run many times.
nginx:1.27,postgres:17. - Container: a running (or stopped) instance of an image, plus a thin writable layer on top.
The writable layer dies with the container. Anything you want to keep goes in a volume. See Storage. I’ll keep repeating this until people stop losing databases.
Commands you’ll actually use
Section titled “Commands you’ll actually use”docker ps # running containersdocker ps -a # including the dead onesdocker logs -f --tail 100 web # follow logsdocker exec -it web sh # get a shell inside (bash if the image has it; many don't)docker inspect web # everything about it, as a wall of JSONdocker stats # live CPU/RAM per containerdocker imagesdocker pull nginx:1.27docker rm -f web # stop and removeRunning one by hand:
docker run -d --name web \ --restart unless-stopped \ -p 127.0.0.1:8080:80 \ -v /srv/web:/usr/share/nginx/html:ro \ nginx:1.27If you’re writing a docker run with more than three flags, stop. Write a Compose file. Shell history isn’t documentation.
Tags: please pin them
Section titled “Tags: please pin them”nginx:latest means “whatever was newest when this host last pulled.” That’s a different thing on every server and a different thing next week. Pin a version: postgres:17, nginx:1.27. For the truly paranoid (correct), pin the digest: nginx@sha256:....
A Dockerfile that won’t embarrass you
Section titled “A Dockerfile that won’t embarrass you”FROM python:3.13-slim
# Deps first, so this layer is cached until requirements changeWORKDIR /appCOPY requirements.txt .RUN pip install --no-cache-dir -r requirements.txt
COPY . .
# Don't run as rootRUN useradd --system --uid 10001 appUSER app
EXPOSE 8000CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]Notes from the trenches:
- Order layers from least to most frequently changing. Copy the dependency manifest, install, then copy source. Otherwise every code change reinstalls every dependency, and your CI bill notices.
- Add a
.dockerignore. Include.git,node_modules,.env. Especially.env. I’ve pulled secrets out of public images. Don’t make it easy. - Use exec form (
CMD ["a", "b"]), not shell form (CMD a b). Shell form runs under/bin/sh, which doesn’t forward SIGTERM, sodocker stopwaits 10 seconds and then kills your app mid-write. - Bind to
0.0.0.0inside the container. An app listening on127.0.0.1inside a container can’t be reached from outside it. Every time. Every single time someone asks.
Restart policies
Section titled “Restart policies”| Policy | Behavior |
|---|---|
no |
Default. Dies and stays dead. |
on-failure |
Restarts on non-zero exit. |
always |
Restarts always, including after you manually stopped it, once the daemon restarts. |
unless-stopped |
Like always, but respects you stopping it. Use this one. |
Make sure the daemon itself starts at boot: sudo systemctl enable --now docker. A restart policy can’t help if Docker never starts.