Docker & Compose Cheat Sheet

Running, debugging, and cleaning up — the commands you'll actually type.

Docker has a large surface area and you use maybe fifteen commands of it regularly. Here's the working set, plus the two things that trip people up: getting inside a container to debug it, and reclaiming the disk space Docker quietly eats.

Running things

CommandWhat it does
docker run -d --name web -p 8080:80 nginxRun detached, named, host port 8080 → container port 80
docker run -it --rm ubuntu bashInteractive throwaway shell — --rm deletes it on exit
docker psWhat's running
docker ps -aEverything, including stopped — where "why is that name taken" gets answered
docker stop web / docker start webStop or start by name
docker rm webDelete a stopped container

Port mapping is always -p HOST:CONTAINER. Getting it backwards is the most common Docker mistake there is.

Debugging a container

CommandWhat it does
docker logs -f webFollow the logs. Start here, always.
docker logs --tail 100 webLast 100 lines
docker exec -it web shGet a shell inside a running container. Use bash if the image has it.
docker inspect webFull config — networks, mounts, env, IP
docker statsLive CPU/memory per container
docker diff webWhat files changed since the image — surprisingly useful
The debugging order that works: docker ps -a (is it even running, or crash-looping?) → docker logs (what did it say on the way down?) → docker exec (go look inside). Ninety percent of problems are answered by step two.

Images

CommandWhat it does
docker imagesWhat's on disk
docker pull nginx:1.27Fetch a specific tag — pin versions, don't rely on latest
docker build -t myapp:1.0 .Build from the Dockerfile here
docker rmi image_idDelete an image
docker history myapp:1.0See the layers — good for finding what bloated your image

Reclaiming disk space

Docker accumulates stopped containers, dangling images, unused volumes, and build cache until a disk fills up and something unrelated breaks.

CommandWhat it does
docker system dfWhat Docker is using, by category. Run this first.
docker system pruneRemove stopped containers, unused networks, dangling images
docker system prune -a --volumesAggressive — also removes unused images and volumes
Careful: --volumes deletes unused volumes, and "unused" includes the database volume of a container you happen to have stopped. Check docker volume ls before you run it on anything you care about.

Compose

CommandWhat it does
docker compose up -dStart the whole stack in the background
docker compose downStop and remove it (add -v to drop volumes too)
docker compose logs -f svcFollow one service's logs
docker compose psStatus of the stack
docker compose exec svc shShell into a service
docker compose up -d --buildRebuild images then start — after you change a Dockerfile
docker compose restart svcBounce one service

Note it's docker compose (a subcommand) on current versions, not the old standalone docker-compose binary. Both may work; the space is the modern one.

Want this as a printable sheet?

The Docker & Compose Cheat Sheet — one clean page, made to print and keep next to the keyboard. Instant download.

Get the sheet — $3 →

Pairs with the Linux commands sheet — most of what you do inside a container is plain Linux — and the ports reference when you're untangling what's bound where.