Docker & Compose Cheatsheet
Container lifecycle, image builds, multi-stage Dockerfiles, Compose v2 stacks, networking, volumes, logs, and safe cleanup commands on Docker 27+.
How it works
Images are immutable layered templates; containers are writable runtime processes from an image; volumes persist state across container replacement; networks wire service DNS.
Survival kit
The few commands to learn first.
42 commands
Run & Inspect Containers
Start containers, map ports, inspect logs, and attach interactive shells.
- Essential
Start an ephemeral interactive container and delete it on exit
docker run --rm -it <image> <shell>Build the command
docker run --rm -it <image> <shell>Values are inserted exactly as typed. Quote them yourself if they contain spaces or special characters.
When to use it
Best for throwaway debugging environments without leaving stopped containers behind.
Breakdown
- --rm
- Automatically remove container once it exits
- -it
- Interactive stdin (-i) + pseudo-TTY (-t)
Example
docker run --rm -it alpine:3.20 sh
Images & Multi-Platform Builds
Build, tag, inspect image layers, and manage local registries.
Docker Compose v2
Orchestrate multi-container local stacks cleanly with `docker compose`.
Volumes, Networks & Files
Persist database state, connect containers by service name, and copy files.
Disk Usage & Safe Cleanup
Measure disk consumption and prune unused objects without losing data.