Docker Commands: The Complete Cheatsheet
Docker has hundreds of commands and flags. You don't need to memorize them all — but you need fast access to the ones that matter. This is the reference I wish I had when I started.
Containers: The Basics
# Run a container (detached, with name)
docker run -d --name my-app -p 8080:80 nginx
# List running containers
docker ps
# List ALL containers (including stopped)
docker ps -a
# Stop / start / restart
docker stop my-app
docker start my-app
docker restart my-app
# Remove a stopped container
docker rm my-app
# Force remove a running container
docker rm -f my-appLogs and Debugging
# View logs
docker logs my-app
# Follow logs in real-time (like tail -f)
docker logs -f my-app
# Last 100 lines
docker logs --tail 100 my-app
# Open a shell inside a running container
docker exec -it my-app sh
# Run a one-off command
docker exec my-app cat /etc/nginx/nginx.confImages
# Build from Dockerfile
docker build -t my-app:latest .
# Build with no cache
docker build --no-cache -t my-app:latest .
# List images
docker images
# Remove an image
docker rmi my-app:latest
# Pull from registry
docker pull postgres:16-alpineVolumes
# Create a named volume
docker volume create my-data
# Run with volume
docker run -v my-data:/var/lib/postgresql/data postgres:16
# Bind mount (host directory)
docker run -v $(pwd):/app node:22-alpine
# List / remove volumes
docker volume ls
docker volume rm my-dataDocker Compose
# Start all services
docker compose up -d
# Stop and remove
docker compose down
# Rebuild and start
docker compose up -d --build
# View logs
docker compose logs -f
# Execute command in service
docker compose exec web shSystem Cleanup (the lifesaver)
# Show disk usage
docker system df
# Remove dangling images (safe)
docker image prune -f
# Remove ALL unused data (containers, images, volumes)
docker system prune -a --volumes
# Remove stopped containers
docker container pruneThat last section is critical. Docker accumulates cruft fast — old images, stopped containers, build cache. I've seen servers hit 100% disk because nobody ran docker system prune. Set up a weekly cron job.
Useful Flags
| Flag | Meaning |
|---|---|
-d | Detached (background) |
-it | Interactive + TTY (for shell access) |
-p 8080:80 | Map host port 8080 to container port 80 |
-v | Mount volume |
-e KEY=val | Set environment variable |
--rm | Remove container when it exits |
--name | Give the container a name |
Interactive reference: Docker Cheatsheet — searchable, filterable, click to copy.