Docker Cheat Sheet: 100+ Commands with Real Examples
The practical guide to docker commands, Dockerfiles, and docker-compose you will actually use
Posted on February 20, 2026
· 20 minute read
Quick Answer
Core commands: docker ps (list containers), docker images (list images), docker run IMAGE (start container), docker build -t NAME . (build image), docker-compose up (start services). Image = template, Container = running instance. Use -d for detached mode, -p HOST:CONTAINER for ports, -v HOST:CONTAINER for volumes. Always use .dockerignore to speed up builds.
I have been using Docker for years across different projects. Every time I set up a new service, debug a container issue, or onboard someone to a project, I end up looking up the same commands.
This cheat sheet is the result of all those lookups. It covers the Docker commands you will actually use as a developer, with real examples. Not a documentation dump. Just the practical stuff.
Before diving into commands, understand the core concepts. Docker has three main components: images, containers, and registries.
Images vs Containers
An image is a read-only template. It contains everything needed to run an application: code, runtime, libraries, and dependencies. Think of it as a blueprint.
A container is a running instance of an image. It is isolated from other containers and the host system. Multiple containers can run from the same image.
The Docker daemon runs in the background and manages containers. The Docker CLI sends commands to the daemon through the API.
Image Management
Images are the foundation of Docker. You build them, pull them from registries, and use them to create containers.
Listing Images
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# List all images
docker images
# List images with more details
docker images --format"table {{.Repository}}\t{{.Tag}}\t{{.Size}}"# List only image IDs
docker images -q# Filter images by name
docker images nginx
# Show image history (layers)
docker history nginx:latest
Pulling Images
1
2
3
4
5
6
7
8
9
10
11
# Pull latest version
docker pull nginx
# Pull specific version
docker pull nginx:1.21
# Pull from different registry
docker pull registry.example.com/myapp:latest
# Pull without output
docker pull -q nginx
Building Images
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Build from Dockerfile in current directory
docker build -t myapp:latest .# Build with specific Dockerfile
docker build -f Dockerfile.prod -t myapp:prod .# Build without cache
docker build --no-cache-t myapp:latest .# Build with build arguments
docker build --build-argNODE_ENV=production -t myapp:latest .# Build and tag multiple tags
docker build -t myapp:latest -t myapp:1.0.0 .
Tagging Images
1
2
3
4
5
6
7
8
# Tag an existing image
docker tag myapp:latest myapp:1.0.0
# Tag for different registry
docker tag myapp:latest registry.example.com/myapp:latest
# Remove tag (does not delete the image)
docker rmi myapp:1.0.0
Removing Images
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Remove specific image
docker rmi nginx:latest
# Force remove (even if used by containers)
docker rmi -f nginx:latest
# Remove multiple images
docker rmi nginx:latest postgres:15
# Remove all unused images
docker image prune
# Remove all images (careful)
docker rmi $(docker images -q)
Inspecting Images
1
2
3
4
5
6
7
8
# Show image details
docker inspect nginx:latest
# Show specific field
docker inspect -f'{{.Config.Env}}' nginx:latest
# Show image size
docker images nginx:latest --format"{{.Size}}"
Container Lifecycle
Understanding the container lifecycle helps you manage containers effectively.
The docker run command combines create and start. It’s the most common command you will use.
Basic Run Commands
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Run container in foreground
docker run nginx:latest
# Run in detached mode (background)
docker run -d nginx:latest
# Run with name
docker run --name web nginx:latest
# Run and remove when stopped
docker run --rm nginx:latest
# Run with interactive terminal
docker run -it ubuntu:latest /bin/bash
Port Mapping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Map host port to container port
docker run -p 8080:80 nginx:latest
# Map to specific host IP
docker run -p 127.0.0.1:8080:80 nginx:latest
# Map random host port
docker run -p 80 nginx:latest
# Map multiple ports
docker run -p 8080:80 -p 8443:443 nginx:latest
# Map all ports
docker run -P nginx:latest
Environment Variables
1
2
3
4
5
6
7
8
9
10
11
# Set single environment variable
docker run -eNODE_ENV=production nginx:latest
# Set multiple environment variables
docker run -eNODE_ENV=production -eDEBUG=true nginx:latest
# Set from file
docker run --env-file .env nginx:latest
# Pass host environment variable
docker run -e HOME nginx:latest
Volume Mounting
1
2
3
4
5
6
7
8
9
10
11
# Mount host directory
docker run -v /host/path:/container/path nginx:latest
# Mount with read-only
docker run -v /host/path:/container/path:ro nginx:latest
# Mount named volume
docker run -v mydata:/container/path nginx:latest
# Mount anonymous volume
docker run -v /container/path nginx:latest
Resource Limits
1
2
3
4
5
6
7
8
9
10
11
# Limit memory
docker run -m 512m nginx:latest
# Limit CPU (1.5 cores)
docker run --cpus="1.5" nginx:latest
# Limit CPU shares (relative priority)
docker run --cpu-shares=512 nginx:latest
# Limit all resources
docker run -m 512m --cpus="1.0" nginx:latest
Common Run Patterns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Web server
docker run -d-p 8080:80 --name web nginx:latest
# Database with volume
docker run -d-p 5432:5432 \-v postgres_data:/var/lib/postgresql/data \-ePOSTGRES_PASSWORD=secret \--name db postgres:15
# Development with code mount
docker run -it-v$(pwd):/app \-p 3000:3000 \--name dev node:18 bash
# One-off command
docker run --rm ubuntu:latest ls /etc
Dockerfile Best Practices
A Dockerfile is a text file with instructions for building an image. How you write it affects build speed, image size, and security.
# Use specific tags, not 'latest'FROM node:18-alpine# Use non-root userRUN addgroup -g 1001 -S nodejs &&\
adduser -S nodejs -u 1001
USER nodejs# Scan for vulnerabilities (add to CI/CD)# docker scan myapp:latest# Use minimal base imagesFROM alpine:3.18# Do not store secrets in image# Use secrets management instead
For more on container security in production environments, see how Kubernetes manages container security with resource limits and security contexts.
Docker Compose
docker-compose manages multi-container applications with a single YAML file. It handles networking, volumes, and dependencies automatically.
# Start all services
docker-compose up
# Start in detached mode
docker-compose up -d# Start specific service
docker-compose up web
# Build and start
docker-compose up --build# Stop all services
docker-compose down
# Stop and remove volumes
docker-compose down -v# View logs
docker-compose logs
# Follow logs
docker-compose logs -f# View logs for specific service
docker-compose logs web
# Execute command in service
docker-compose exec web ls /app
# Scale service
docker-compose up --scaleweb=3
docker-compose Architecture
flowchart TB
Compose[docker-compose.yml]
subgraph Network["Default Network"]
W[web container]
A[api container]
D[db container]
end
subgraph Volumes["Volumes"]
V1[postgres_data]
end
Compose --> W
Compose --> A
Compose --> D
D --> V1
W -.->|http://api:3000| A
A -.->|postgres://db:5432| D
style Compose fill:#1a365d,stroke:#2b6cb0,color:#bee3f8
style W fill:#234e52,stroke:#319795,color:#b2f5ea
style A fill:#234e52,stroke:#319795,color:#b2f5ea
style D fill:#234e52,stroke:#319795,color:#b2f5ea
style V1 fill:#2d3748,stroke:#4a5568,color:#e2e8f0
# Connect container to network
docker network connect mynetwork mycontainer
# Disconnect from network
docker network disconnect mynetwork mycontainer
# Run container on specific network
docker run --network mynetwork nginx:latest
# Use container name as hostname
docker run --name api --network mynetwork nginx:latest
# Other containers can reach it at 'api'
# Named volume
docker run -v mydata:/app/data nginx:latest
# Bind mount
docker run -v /host/path:/container/path nginx:latest
# Read-only mount
docker run -v /host/path:/container/path:ro nginx:latest
# Multiple volumes
docker run -v data1:/data1 -v data2:/data2 nginx:latest
# Backup volume
docker run --rm-v mydata:/data -v$(pwd):/backup \
ubuntu:latest tar czf /backup/mydata.tar.gz -C /data .# Restore volume
docker run --rm-v mydata:/data -v$(pwd):/backup \
ubuntu:latest tar xzf /backup/mydata.tar.gz -C /data
Logs and Debugging
Logs are your first line of defense when debugging container issues.
Viewing Logs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# View logs
docker logs mycontainer
# Follow logs (like tail -f)
docker logs -f mycontainer
# Show last N lines
docker logs --tail 100 mycontainer
# Show logs with timestamps
docker logs -t mycontainer
# Show logs since timestamp
docker logs --since"2025-01-01T00:00:00" mycontainer
# Show logs until timestamp
docker logs --until"2025-01-01T23:59:59" mycontainer
Executing Commands
1
2
3
4
5
6
7
8
9
10
11
# Execute command in running container
docker exec mycontainer ls /app
# Get interactive shell
docker exec-it mycontainer /bin/bash
# Execute as specific user
docker exec-u root mycontainer whoami# Execute in specific working directory
docker exec-w /app mycontainer pwd
Inspecting Containers
1
2
3
4
5
6
7
8
9
10
11
# Show container details
docker inspect mycontainer
# Show specific field
docker inspect -f'{{.State.Status}}' mycontainer
# Show IP address
docker inspect -f'{{.NetworkSettings.IPAddress}}' mycontainer
# Show mounted volumes
docker inspect -f'{{.Mounts}}' mycontainer
Container Stats
1
2
3
4
5
6
7
8
9
10
11
# Show live resource usage
docker stats
# Show stats for specific container
docker stats mycontainer
# Show stats without streaming
docker stats --no-stream# Show stats in custom format
docker stats --format"table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}"
Resource Management
Docker allows you to limit container resources to prevent one container from consuming all host resources.
Memory Limits
1
2
3
4
5
6
7
8
# Limit memory
docker run -m 512m nginx:latest
# Limit memory and swap
docker run -m 512m --memory-swap 1g nginx:latest
# Disable swap
docker run -m 512m --memory-swap 512m nginx:latest
CPU Limits
1
2
3
4
5
6
7
8
# Limit CPU cores
docker run --cpus="1.5" nginx:latest
# Limit CPU shares (relative priority)
docker run --cpu-shares=512 nginx:latest
# Pin to specific CPUs
docker run --cpuset-cpus="0,1" nginx:latest
I/O Limits
1
2
3
4
5
# Limit read/write IOPS
docker run --device-read-iops /dev/sda:1000 nginx:latest
# Limit read/write bandwidth
docker run --device-read-bps /dev/sda:1mb nginx:latest
For production deployments, Kubernetes provides more advanced resource management. See the Kubernetes Cheat Sheet for resource limits, requests, and autoscaling.
Docker Hub and Registries
Docker Hub is the default public registry. You can also use private registries or cloud provider registries.
# Find what is using the port
docker ps | grep 8080
# Use different port
docker run -p 8081:80 nginx:latest
# Stop conflicting container
docker stop conflicting-container
Out of Disk Space
1
2
3
4
5
6
7
8
9
10
11
# Check disk usage
docker system df# Remove unused data
docker system prune
# Remove everything unused (careful)
docker system prune -a# Remove specific volumes
docker volume prune
Container Keeps Restarting
1
2
3
4
5
6
7
8
9
10
11
# Check restart policy
docker inspect -f'{{.HostConfig.RestartPolicy.Name}}' mycontainer
# View logs
docker logs mycontainer
# Check exit code
docker inspect -f'{{.State.ExitCode}}' mycontainer
# Run without restart policy to see error
docker run --restart=no myimage
Every developer working with modern applications needs Docker skills. The commands in this cheat sheet cover 90% of what you will do day to day. Start with the basics, use docker-compose for multi-container apps, and always set resource limits in production.
What Docker commands do you use most? Drop them in the comments below.