Skip to content
back to cheatsheets

Docker Commands Cheatsheet — Essential Docker & Compose Reference

· Reference

Docker packages applications and their dependencies into portable containers. This reference covers the commands you use daily for building images, managing containers, handling data with volumes, and orchestrating multi-container apps with Docker Compose.

Images

Images are read-only templates used to create containers.

# Build an image
docker build -t myapp:latest .                # Build from Dockerfile in current dir
docker build -t myapp:v1.0 -f Dockerfile.prod .  # Use a specific Dockerfile
docker build --no-cache -t myapp .            # Build without cache
docker build --target builder -t myapp .      # Multi-stage: build specific target
docker build --build-arg NODE_ENV=production -t myapp .  # Pass build arguments

# List images
docker images                                 # List all local images
docker images -q                              # List image IDs only
docker images --filter "dangling=true"        # List untagged images

# Pull and push
docker pull nginx:alpine                      # Pull from registry
docker push myrepo/myapp:latest               # Push to registry
docker tag myapp:latest myrepo/myapp:v1.0     # Tag an image

# Remove images
docker rmi myapp:latest                       # Remove an image
docker rmi $(docker images -q -f dangling=true)  # Remove dangling images
docker image prune                            # Remove unused images
docker image prune -a                         # Remove all unused images

# Inspect
docker image inspect myapp:latest             # Detailed image info
docker history myapp:latest                   # Show image layers

Containers

Containers are running instances of images.

# Run containers
docker run nginx                              # Run in foreground
docker run -d nginx                           # Run in background (detached)
docker run -d --name web nginx                # Run with a name
docker run -d -p 8080:80 nginx                # Map host:container ports
docker run -d -p 3000:3000 -p 3001:3001 app   # Map multiple ports
docker run -d -e NODE_ENV=production app       # Set environment variable
docker run -d --env-file .env app             # Load env vars from file
docker run -d -v /host/path:/container/path app  # Bind mount
docker run -d -v mydata:/data app             # Named volume
docker run -d --restart unless-stopped app     # Restart policy
docker run -it ubuntu bash                    # Interactive terminal
docker run --rm -it node:20 node              # Auto-remove when stopped
docker run -d --network mynet app             # Connect to a network
docker run -d --memory="512m" --cpus="1.5" app  # Resource limits

# List containers
docker ps                                     # Running containers
docker ps -a                                  # All containers (including stopped)
docker ps -q                                  # Container IDs only
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"  # Custom format

# Container lifecycle
docker start <container>                      # Start a stopped container
docker stop <container>                       # Graceful stop (SIGTERM)
docker restart <container>                    # Stop and start
docker kill <container>                       # Force stop (SIGKILL)
docker pause <container>                      # Freeze container
docker unpause <container>                    # Unfreeze container
docker rm <container>                         # Remove a stopped container
docker rm -f <container>                      # Force remove (even if running)
docker container prune                        # Remove all stopped containers

# Interact with running containers
docker exec -it <container> bash              # Open a shell
docker exec -it <container> sh                # Alpine/minimal images
docker exec <container> ls /app               # Run a single command
docker logs <container>                       # View logs
docker logs -f <container>                    # Follow logs (live)
docker logs --tail 100 <container>            # Last 100 lines
docker logs --since 1h <container>            # Logs from last hour
docker top <container>                        # Running processes
docker stats                                  # Live resource usage for all
docker stats <container>                      # Resource usage for one

# Copy files
docker cp file.txt <container>:/path/         # Host to container
docker cp <container>:/path/file.txt ./       # Container to host

# Inspect
docker inspect <container>                    # Full container details
docker inspect -f '{{.NetworkSettings.IPAddress}}' <container>  # Specific field
docker port <container>                       # Show port mappings

Volumes

Volumes persist data beyond container lifecycle.

# Create and manage
docker volume create mydata                   # Create a named volume
docker volume ls                              # List volumes
docker volume inspect mydata                  # Volume details
docker volume rm mydata                       # Remove a volume
docker volume prune                           # Remove unused volumes

# Use volumes
docker run -d -v mydata:/app/data app         # Named volume
docker run -d -v /host/path:/app/data app     # Bind mount
docker run -d -v mydata:/app/data:ro app      # Read-only mount

# Backup a volume
docker run --rm -v mydata:/data -v $(pwd):/backup \
  alpine tar czf /backup/data-backup.tar.gz -C /data .

# Restore a volume
docker run --rm -v mydata:/data -v $(pwd):/backup \
  alpine tar xzf /backup/data-backup.tar.gz -C /data

Networks

Networks let containers communicate with each other.

# Create and manage
docker network create mynet                   # Create bridge network
docker network create --driver overlay mynet   # Overlay network (Swarm)
docker network ls                             # List networks
docker network inspect mynet                  # Network details
docker network rm mynet                       # Remove a network
docker network prune                          # Remove unused networks

# Connect containers
docker network connect mynet <container>      # Add container to network
docker network disconnect mynet <container>   # Remove from network

# Run on a specific network
docker run -d --network mynet --name api app
docker run -d --network mynet --name db postgres
# 'api' container can reach 'db' at hostname 'db'

Docker Compose

Compose defines multi-container applications in a docker-compose.yml file.

# Lifecycle
docker compose up                             # Start all services
docker compose up -d                          # Start in background
docker compose up --build                     # Rebuild images first
docker compose up -d --scale web=3            # Scale a service
docker compose down                           # Stop and remove containers
docker compose down -v                        # Also remove volumes
docker compose down --rmi all                 # Also remove images

# Management
docker compose ps                             # List running services
docker compose logs                           # View all logs
docker compose logs -f web                    # Follow logs for one service
docker compose exec web bash                  # Shell into a service
docker compose run --rm web npm test          # Run a one-off command

# Build
docker compose build                          # Build all services
docker compose build --no-cache web           # Rebuild without cache
docker compose pull                           # Pull latest images

# Config
docker compose config                         # Validate and view config
docker compose config --services              # List service names

Example docker-compose.yml

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - DATABASE_URL=postgres://db:5432/myapp
    depends_on:
      - db
      - redis
    volumes:
      - ./src:/app/src
    restart: unless-stopped

  db:
    image: postgres:16-alpine
    volumes:
      - pgdata:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: myapp
      POSTGRES_PASSWORD: secret
    ports:
      - "5432:5432"

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

volumes:
  pgdata:

System Cleanup

# View disk usage
docker system df                              # Disk usage summary
docker system df -v                           # Detailed breakdown

# Clean up everything
docker system prune                           # Remove stopped containers, unused networks, dangling images
docker system prune -a                        # Also remove unused images
docker system prune -a --volumes              # Nuclear option: everything unused

# Selective cleanup
docker container prune                        # Remove stopped containers
docker image prune                            # Remove dangling images
docker volume prune                           # Remove unused volumes
docker network prune                          # Remove unused networks

Useful Patterns

# Quick database for development
docker run -d --name postgres \
  -e POSTGRES_PASSWORD=dev \
  -p 5432:5432 \
  -v pgdata:/var/lib/postgresql/data \
  postgres:16-alpine

# Quick Redis
docker run -d --name redis -p 6379:6379 redis:7-alpine

# Run a one-off command in a fresh container
docker run --rm -it node:20 node -e "console.log(process.version)"

# Check what's using disk space
docker system df -v

# Follow all container logs
docker compose logs -f --tail=50

Generate Dockerfiles with the Dockerfile Generator or Compose files with the Docker Compose Generator.

#Learn More