Docker Commands Cheat Sheet

Docker Commands Cheat Sheet

Sufi Aurangzeb Hossain
April 1, 2026
16 min read
DockerDevOps

Docker Commands Reference: Complete Cheat Sheet

This comprehensive guide covers essential Docker commands from basic to advanced, organized by functionality with detailed explanations and practical examples. Whether you're a beginner or experienced developer, this reference will help you master Docker operations.

Building Images

Docker images are built from Dockerfiles and form the foundation of containerized applications.

docker build -t myapp:latest .                    # Build image with tag from current directory
docker build -f Dockerfile.dev -t myapp:dev .     # Build using specific Dockerfile
docker build --no-cache -t myapp:clean .          # Build without cache for clean rebuild
docker build --target builder -t myapp:builder .  # Build specific target in multi-stage build
docker build --build-arg VERSION=1.0 .            # Build with arguments
docker images                                   # List all images
docker image ls                                 # Alternative listing command
docker image inspect myapp:latest               # Detailed image information
docker image history myapp:latest               # Show image layers and build history
docker image prune -a                           # Remove all unused images

Running Containers

Containers are running instances of images. These commands control container execution and runtime behavior.

docker run nginx:alpine                         # Run container in foreground
docker run -d nginx:alpine                      # Run in detached mode (background)
docker run -it ubuntu:latest bash               # Run with interactive terminal
docker run --name webapp nginx:alpine           # Run with custom name
docker run -p 8080:80 nginx:alpine              # Map host port 8080 to container port 80
docker run -p 80:80 -p 443:443 nginx:alpine     # Multiple port mappings
docker run --network mynetwork nginx:alpine     # Connect to custom network
docker run --hostname myapp nginx:alpine        # Set container hostname
docker run -v /host/path:/container/path nginx  # Bind mount host directory
docker run -v myvolume:/app/data nginx          # Use named volume
docker run --mount type=bind,source=$(pwd),target=/app nginx  # Advanced mount
docker run -v $(pwd):/app -v /app/node_modules nginx  # Development mount with exclusion
docker run -e NODE_ENV=production myapp         # Set environment variable
docker run --env-file .env myapp                # Set variables from file
docker run --memory=512m myapp                  # Memory limit
docker run --cpus=1.5 myapp                     # CPU limit
docker run --restart=always myapp               # Auto-restart policy

Managing Containers

These commands help you monitor and control container lifecycle and status.

docker ps                                     # Show running containers
docker ps -a                                  # Show all containers (including stopped)
docker ps -q                                  # Show only container IDs
docker ps --filter "status=running"           # Filter by status
docker ps --filter "name=web"                # Filter by name
docker ps --format "table {{.Names}}\t{{.Status}}"  # Custom format
docker stop container_name                     # Gracefully stop container
docker stop $(docker ps -q)                   # Stop all running containers
docker kill container_name                    # Force immediate stop
docker restart container_name                 # Restart container
docker pause container_name                   # Pause container execution
docker unpause container_name                 # Resume container execution
docker rename old_name new_name               # Rename container
docker update --memory=1g container_name      # Update container resources
docker wait container_name                    # Block until container stops
docker port container_name                    # Show port mappings

Interacting with Containers

Commands for debugging, monitoring, and interacting with running containers.

docker exec -it container_name bash           # Open interactive shell
docker exec container_name ls -la /app        # Execute single command
docker exec -u root container_name bash       # Execute as specific user
docker exec -w /app container_name pwd        # Set working directory for command
docker logs container_name                    # View container logs
docker logs -f container_name                 # Follow logs (real-time)
docker logs --tail 100 container_name         # Show last 100 lines
docker logs --since 1h container_name         # Show logs from last hour
docker logs -t container_name                 # Show timestamps
docker stats                                 # Live resource usage for all containers
docker stats container_name                   # Resource usage for specific container
docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}"
docker top container_name                     # Show running processes in container
docker inspect container_name                 # Detailed container information
docker cp container_name:/path/to/file ./     # Copy file from container to host
docker cp ./file container_name:/path/        # Copy file from host to container
docker diff container_name                    # Show filesystem changes

Image Management

Commands for managing Docker images, tags, and registries.

docker tag myapp:latest myregistry/myapp:v1.0  # Tag image for registry
docker tag myapp:latest myapp:1.0.0           # Version tagging
docker rmi myapp:old                          # Remove specific image
docker rmi $(docker images -q)                # Remove all images (use carefully!)
docker image save myapp:latest > myapp.tar    # Save image to tar file
docker image load < myapp.tar                 # Load image from tar file
docker image import ./container.tar myapp     # Import from tarball
docker image export container_name > container.tar  # Export container to tarball

Publishing and Distribution

Commands for working with Docker registries and image distribution.

docker login                                 # Login to Docker Hub
docker login myregistry.example.com           # Login to private registry
docker logout                                # Logout from registry
docker push myregistry/myapp:v1.0            # Push image to registry
docker pull myregistry/myapp:v1.0            # Pull image from registry
docker search nginx                          # Search Docker Hub for images
docker search --filter "is-official=true" nginx  # Search official images

Volume Management

Docker volumes provide persistent storage for containers.

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

Network Management

Commands for managing Docker networks and container networking.

docker network ls                          # List networks
docker network create mynetwork            # Create custom network
docker network inspect mynetwork           # Network details
docker network connect mynetwork container # Connect container to network
docker network disconnect mynetwork container # Disconnect container
docker network rm mynetwork                # Remove network

System Information and Maintenance

Commands for Docker system management and cleanup.

docker info                                # System-wide information
docker version                             # Docker version information
docker system df                           # Docker disk usage
docker system events                       # Real-time Docker events
docker container prune                    # Remove all stopped containers
docker image prune                        # Remove unused images
docker volume prune                       # Remove unused volumes
docker network prune                      # Remove unused networks
docker system prune                       # Remove all unused data
docker system prune -a                    # Remove all unused images including dangling

Docker Compose Commands

Docker Compose simplifies multi-container application management.

docker compose up                         # Build and start all services
docker compose up -d                      # Start in detached mode
docker compose up --build                 # Force rebuild of images
docker compose up service1 service2       # Start specific services only
docker compose down                       # Stop and remove containers
docker compose down -v                    # Remove volumes as well
docker compose down --rmi all             # Remove images as well
docker compose stop                       # Stop services without removing
docker compose start                      # Start stopped services
docker compose restart                    # Restart services
docker compose ps                        # Show service status
docker compose logs                      # View logs from all services
docker compose logs -f                   # Follow logs
docker compose logs service_name         # View logs for specific service
docker compose exec service_name bash    # Execute command in service container
docker compose exec -u root service_name bash  # Execute as root
docker compose build                     # Build service images
docker compose build --no-cache          # Build without cache
docker compose pull                     # Pull service images
docker compose config                   # Validate and view compose file
docker compose top                      # Display running processes

Security and Best Practices Commands

Essential commands for securing your Docker environment.

docker scan myapp:latest                  # Security vulnerability scan
docker run --user 1000:1000 myapp        # Run as non-root user
docker run --read-only myapp             # Read-only filesystem
docker run --security-opt=no-new-privileges myapp
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE myapp

Tips and Best Practices

Development Workflow:

  • Use descriptive names for containers and images
  • Implement .dockerignore to exclude unnecessary files from build context
  • Use multi-stage builds to minimize final image size
  • Leverage layer caching by ordering Dockerfile instructions properly

Production Guidelines:

  • Always run containers as non-root users when possible
  • Use health checks for critical services
  • Set resource limits (memory, CPU) to prevent resource exhaustion
  • Keep containers stateless and use volumes for persistence
  • Regularly update base images and scan for vulnerabilities

Security Measures:

  • Scan images for vulnerabilities before deployment
  • Use signed images from trusted sources
  • Limit container capabilities using --cap-drop
  • Avoid using privileged mode unless absolutely necessary
  • Use secrets management for sensitive data instead of environment variables

Performance Optimization:

  • Use alpine-based images for smaller footprint
  • Combine RUN commands to reduce layers
  • Clean up package caches and temporary files in the same layer they're created
  • Use specific tags instead of 'latest' for production deployments

Quick Reference: Most Frequently Used Commands

docker build -t name .          # Build image
docker run -d -p 80:80 name    # Run container
docker ps                      # List containers
docker exec -it name bash      # Enter container
docker logs name               # View logs
docker compose up              # Start compose services
docker system prune            # Cleanup unused resources

This comprehensive reference serves as your go-to guide for Docker commands in both development and production environments. Bookmark this page for quick access to essential Docker operations and best practices.