Docker Commands Cheat Sheet

Essential Docker CLI commands in one page: images, containers, volumes, networks, Compose V2, and cleanup.

Images

docker build -t myapp:1.0 .                            # Build image from Dockerfile in current dir
docker build -f Dockerfile.prod -t myapp:1.0 .         # Build using a different Dockerfile
docker build --no-cache -t myapp:1.0 .                 # Build ignoring all cached layers
docker build --pull -t myapp:1.0 .                     # Always pull the latest base image first
docker build --target builder -t myapp:dev .           # Build only up to a named build stage
docker build --build-arg NODE_ENV=prod -t myapp:1.0 .  # Pass a build-time variable (Dockerfile ARG)
docker pull nginx:alpine                               # Download an image from a registry
docker push myname/myapp:1.0                           # Upload a tagged image to a registry
docker tag myapp:1.0 myname/myapp:1.0                  # Add a new name/tag to an existing image
docker images                                          # List local images
docker rmi myapp:1.0                                   # Remove a local image
docker history myapp:1.0                               # Show layer history of an image
docker save myapp:1.0 -o myapp.tar                     # Export image to a tar archive
docker load -i myapp.tar                               # Import image from a tar archive

Containers

docker run -d --name web -p 8080:80 nginx              # Run in background; map host:8080 -> container:80
docker run -d --name web -p 127.0.0.1:8080:80 nginx    # Bind port mapping to localhost only
docker run -it --rm ubuntu bash                        # Interactive shell; remove container on exit
docker run -d --name web -e NODE_ENV=prod --env-file .env myapp   # Set env vars / load from file
docker run -d --name db -v dbdata:/var/lib/mysql mysql:8          # Named volume mount
docker run -d --name web -v $(pwd)/html:/usr/share/nginx/html nginx  # Bind-mount host dir
docker run -d --name web --network mynet --restart unless-stopped nginx  # Join network + restart policy
docker run -d --name web --memory 512m --cpus 1.5 nginx             # Limit memory and CPU
docker ps                                              # List running containers
docker ps -a                                           # List all containers (incl. stopped)
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"      # Custom table columns
docker stop web                                        # Gracefully stop a container
docker kill web                                        # Force-kill a container
docker start web                                       # Start a stopped container
docker restart web                                     # Restart a container
docker rm web                                          # Remove a stopped container
docker rm -f web                                       # Force-remove a running container
docker exec -it web bash                               # Open interactive shell in a running container
docker exec web ls /app                                # Run a one-off command inside a container
docker exec -u root -it web sh                         # Exec as root (bypasses Dockerfile USER)
docker logs web                                        # Show container stdout/stderr logs
docker logs -f web                                     # Follow logs live
docker logs --tail 100 web                             # Show only the last 100 log lines
docker inspect web                                     # Full JSON details of a container
docker inspect --format '{{.NetworkSettings.IPAddress}}' web       # Extract one field via template
docker stats                                           # Live CPU/memory/network stats
docker stats --no-stream                               # One-shot stats snapshot
docker cp web:/app/logs ./logs                         # Copy files out of a container

Volumes

docker volume create dbdata                            # Create a named volume
docker volume ls                                       # List volumes
docker volume inspect dbdata                           # Show volume details
docker volume rm dbdata                                # Remove a volume
docker volume prune                                    # Remove all volumes not used by any container

Networks

docker network create mynet                            # Create a user-defined bridge network
docker network create --driver overlay mynet           # Create an overlay network (multi-host)
docker network ls                                      # List networks
docker network inspect mynet                           # Show network details (subnets, members)
docker network connect mynet web                       # Attach a running container to a network
docker network disconnect mynet web                    # Detach a container from a network
docker network rm mynet                                # Remove a network
docker network prune                                   # Remove networks not used by any container

Docker Compose

docker compose up -d                                   # Start all services in the background
docker compose up -d --build                           # Rebuild images before starting services
docker compose up -d --scale web=3                     # Run 3 replicas of the web service
docker compose up --watch                              # Start + auto-reload on file changes (dev mode)
docker compose down                                    # Stop and remove containers + networks
docker compose down -v                                 # Also remove named volumes (data loss!)
docker compose down --rmi local                        # Also remove images built by this project
docker compose ps                                      # Show status of project containers
docker compose logs -f                                 # Follow logs of all services
docker compose logs -f --tail 50 web                   # Follow logs of one service (last 50 lines)
docker compose exec web sh                             # Open a shell in the running web service
docker compose run --rm web python manage.py migrate   # One-off command in a new container
docker compose restart web                             # Restart one service
docker compose pull                                    # Pull latest images without starting
docker compose config                                  # Validate and print the resolved config

Cleanup

docker system df                                       # Show disk usage by images/containers/volumes
docker system prune                                    # Remove stopped containers, dangling images, unused networks + build cache
docker system prune -a                                 # Also remove ALL images not used by a container
docker system prune -a --volumes                       # Nuclear option: also remove unused volumes
docker image prune                                     # Remove dangling (untagged) images only
docker container prune                                 # Remove all stopped containers
docker volume prune                                    # Remove all volumes not used by any container
docker network prune                                   # Remove networks not used by any container
docker builder prune                                   # Remove unused build cache