Docker Commands Cheat Sheet
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 argumentsdocker 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 imagesRunning 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 namedocker 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 hostnamedocker 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 exclusiondocker 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 policyManaging 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 formatdocker 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 executiondocker 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 mappingsInteracting 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 commanddocker 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 timestampsdocker 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 informationdocker 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 changesImage 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 tarballPublishing 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 registrydocker 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 imagesVolume 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 volumesNetwork 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 networkSystem 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 eventsdocker 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 danglingDocker 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 onlydocker 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 servicesdocker 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 rootdocker 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 processesSecurity 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 myappTips 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 resourcesThis 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.