Blog

Docker Cheat Sheet: Commands for Daily Container Work

The docker commands that matter for day-to-day development. Stop googling 'docker remove all containers' every week.

why docker commands are hard to remember

Docker has too many subcommands that sound almost the same. docker rm removes containers. docker rmi removes images. docker container prune removes stopped containers. docker system prune removes most of it. Getting those mixed up at the wrong moment is annoying, and the flag names are inconsistent enough that you end up re-reading --help more than you'd like.

Cleanup is the worst. You accumulate dangling images, exited containers, and unnamed volumes over weeks of development, and then the disk fills up at the worst time. The commands exist, but they don't stick because you don't run them often enough to build muscle memory.

Here are the two I look up most often.

Clean up dangling images and stopped containers without touching named volumes or running containers:

docker system prune
# add -a to also remove images not referenced by any container
docker system prune -a

Get a shell inside a running container to poke around:

docker exec -it <container_name> /bin/sh
# if the container has bash
docker exec -it <container_name> bash

For Compose-based projects, my usual loop is bring a service up, tail its logs, tear it down:

docker compose up -d          # start in background
docker compose logs -f api    # follow logs for one service
docker compose down -v        # stop and remove volumes

The -v on down is the part you forget. Without it, named volumes stick around, and the next up might start with stale data.

get the cheat sheet

The Docker cheat sheet has the rest: image building, networking, volume management, and the Compose commands you use daily. PDF and Markdown.