Docker cheatsheet
Images
| Command | Description |
|---|---|
docker pull nginx:1.25 | Download image from registry |
docker images | List local images |
docker build -t myapp:1.0 . | Build from Dockerfile in cwd |
docker rmi image_id | Remove image |
docker history myapp:1.0 | Image layer history |
docker inspect nginx:1.25 | Image metadata (JSON) |
Containers — run and lifecycle
| Command | Description |
|---|---|
docker run -d --name web -p 8080:80 nginx | Detached run, publish port |
docker run -it --rm alpine sh | Interactive shell, remove on exit |
docker ps | Running containers |
docker ps -a | All containers (including stopped) |
docker stop web | Graceful stop (SIGTERM) |
docker start web | Start stopped container |
docker rm web | Remove container |
docker restart web | Restart container |
Exec, logs, inspect
| Command | Description |
|---|---|
docker logs -f web | Follow container logs |
docker logs --tail 100 web | Last 100 log lines |
docker exec -it web sh | Shell inside running container |
docker exec web cat /etc/hosts | Run single command |
docker inspect web | Full container config/state |
docker top web | Processes inside container |
Volumes and mounts
| Command | Description |
|---|---|
docker volume ls | List volumes |
docker volume inspect vol | Volume mountpoint on host |
docker run -v /host/data:/data myapp | Bind mount |
docker run -v myvol:/data myapp | Named volume |
Networking
| Command | Description |
|---|---|
docker network ls | List networks |
docker network inspect bridge | Network details and IPs |
docker port web | Published port mappings |
ss -tlnp | grep 8080 | Confirm host port listening |
Compose
docker compose up -d # Start stack detached
docker compose ps # Service status
docker compose logs -f web # Follow service logs
docker compose down # Stop and remove containers
docker compose build # Rebuild images
docker compose exec web sh # Shell in service container
Daemon and disk
| Command | Description |
|---|---|
systemctl status docker | Daemon status |
docker info | Daemon summary, storage driver |
docker system df | Disk use: images, containers, volumes |
docker system df -v | Per-object size breakdown |
Cleanup and prune
Run docker system df first. Prune removes data you may still need —
especially volumes. Add -f to skip the confirmation prompt.
| Command | Description |
|---|---|
docker container prune | Remove all stopped containers |
docker rm CONTAINER | Remove one container (must be stopped) |
docker rm -f CONTAINER | Force-remove running container |
docker image prune | Remove dangling images (<none> tags) |
docker image prune -a | Remove images not used by any container |
docker rmi IMAGE | Remove one image by name or ID |
docker volume prune | Remove volumes not attached to a container |
docker volume rm VOL | Remove one named volume |
docker network prune | Remove unused custom networks |
docker builder prune | Clear build cache (BuildKit) |
docker system prune | Stopped containers + dangling images + unused networks |
docker system prune -a | Above + all unused images |
docker system prune -a --volumes | Above + unused volumes (destructive) |
docker compose down -v | Stop stack and remove compose volumes |
# Typical cleanup order (safest first)
docker container prune -f # exited / stopped containers
docker image prune -f # dangling build layers
docker image prune -a -f # images no container references
docker volume prune -f # only if data is disposable
docker network prune -f
docker builder prune -f # stale build cache
# List before bulk delete
docker ps -a --filter status=exited
docker images -f dangling=true
docker volume ls -f dangling=true
Useful inspect filters
docker inspect web | jq -r '.State.ExitCode'
docker inspect web | jq -r '.State.Status'
docker inspect web | jq -r '.NetworkSettings.Networks[].IPAddress'
docker inspect web | jq '.Config.Cmd'
Pro tips
- Container exited? Check
docker logsanddocker inspect .State.ExitCodefirst -p 8080:80maps host:container — swapping them is a common mistake- PID 1 must handle signals — bare shell scripts may ignore SIGTERM from
docker stop - Pin image tags in production —
latestchanges without warning - Disk full?
docker system df -vthencontainer prune,image prune -a,volume prune— see Cleanup section
Practice scenarios
Hands-on Docker scenarios on live Linux VMs: docker