Docker Commands Cheatsheet

Essential Docker commands for daily container management. Quick reference for building, running, and debugging containers.


Images

Build

 1# Build image from Dockerfile
 2docker build -t myapp:latest .
 3docker build -t myapp:v1.0.0 .
 4
 5# Build with build args
 6docker build --build-arg NODE_ENV=production -t myapp:latest .
 7
 8# Build with specific Dockerfile
 9docker build -f Dockerfile.prod -t myapp:prod .
10
11# Build without cache
12docker build --no-cache -t myapp:latest .
13
14# Build with target stage (multi-stage)
15docker build --target production -t myapp:prod .
16
17# Build for specific platform
18docker build --platform linux/amd64 -t myapp:latest .

List & Inspect

 1# List images
 2docker images
 3docker image ls
 4
 5# List all images (including intermediate)
 6docker images -a
 7
 8# Filter images
 9docker images --filter "dangling=true"
10docker images --filter "reference=myapp:*"
11
12# Inspect image
13docker inspect myapp:latest
14
15# View image history
16docker history myapp:latest
17
18# View image layers
19docker image inspect myapp:latest | jq '.[0].RootFS.Layers'

Pull & Push

 1# Pull image
 2docker pull nginx:latest
 3docker pull nginx:1.25-alpine
 4
 5# Pull from specific registry
 6docker pull ghcr.io/user/repo:tag
 7
 8# Push image
 9docker push myapp:latest
10
11# Tag image
12docker tag myapp:latest myregistry.com/myapp:latest
13docker tag myapp:latest myapp:v1.0.0
14
15# Login to registry
16docker login
17docker login ghcr.io -u username
18docker login registry.example.com

Remove

 1# Remove image
 2docker rmi myapp:latest
 3docker image rm myapp:latest
 4
 5# Remove multiple images
 6docker rmi image1 image2 image3
 7
 8# Remove dangling images
 9docker image prune
10
11# Remove all unused images
12docker image prune -a
13
14# Force remove
15docker rmi -f myapp:latest

Containers

Run

 1# Run container
 2docker run nginx
 3
 4# Run with name
 5docker run --name mynginx nginx
 6
 7# Run in background (detached)
 8docker run -d nginx
 9
10# Run with port mapping
11docker run -p 8080:80 nginx
12docker run -p 127.0.0.1:8080:80 nginx  # Bind to localhost only
13
14# Run with environment variables
15docker run -e NODE_ENV=production myapp
16docker run --env-file .env myapp
17
18# Run with volume mount
19docker run -v /host/path:/container/path myapp
20docker run -v myvolume:/data myapp  # Named volume
21
22# Run with bind mount (current directory)
23docker run -v $(pwd):/app myapp  # Linux/Mac
24docker run -v ${PWD}:/app myapp  # PowerShell
25
26# Run with network
27docker run --network mynetwork myapp
28
29# Run with restart policy
30docker run --restart unless-stopped myapp
31docker run --restart always myapp
32
33# Run with resource limits
34docker run --memory="512m" --cpus="1.5" myapp
35
36# Run with user
37docker run --user 1000:1000 myapp
38
39# Run with working directory
40docker run -w /app myapp
41
42# Run interactive with TTY
43docker run -it ubuntu bash
44
45# Run and remove after exit
46docker run --rm myapp
47
48# Run with all options combined
49docker run -d \
50  --name myapp \
51  -p 8080:80 \
52  -e NODE_ENV=production \
53  -v $(pwd):/app \
54  --network mynetwork \
55  --restart unless-stopped \
56  myapp:latest

List & Inspect

 1# List running containers
 2docker ps
 3
 4# List all containers (including stopped)
 5docker ps -a
 6
 7# List with specific format
 8docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
 9
10# Filter containers
11docker ps --filter "status=running"
12docker ps --filter "name=myapp"
13
14# Inspect container
15docker inspect mycontainer
16
17# View container logs
18docker logs mycontainer
19docker logs -f mycontainer  # Follow
20docker logs --tail 100 mycontainer  # Last 100 lines
21docker logs --since 10m mycontainer  # Last 10 minutes
22
23# View container stats
24docker stats
25docker stats mycontainer
26
27# View container processes
28docker top mycontainer
29
30# View container port mappings
31docker port mycontainer

Execute & Interact

 1# Execute command in running container
 2docker exec mycontainer ls -la
 3
 4# Execute interactive shell
 5docker exec -it mycontainer bash
 6docker exec -it mycontainer sh  # Alpine
 7
 8# Execute as specific user
 9docker exec -u root -it mycontainer bash
10
11# Execute with environment variable
12docker exec -e VAR=value mycontainer env
13
14# Attach to running container
15docker attach mycontainer
16
17# Copy files to/from container
18docker cp mycontainer:/path/to/file ./local/path
19docker cp ./local/file mycontainer:/path/to/

Control

 1# Start container
 2docker start mycontainer
 3
 4# Stop container
 5docker stop mycontainer
 6
 7# Stop with timeout
 8docker stop -t 30 mycontainer
 9
10# Restart container
11docker restart mycontainer
12
13# Pause container
14docker pause mycontainer
15
16# Unpause container
17docker unpause mycontainer
18
19# Kill container (force stop)
20docker kill mycontainer
21
22# Rename container
23docker rename oldname newname
24
25# Update container config
26docker update --restart=always mycontainer
27docker update --memory="1g" mycontainer

Remove

 1# Remove container
 2docker rm mycontainer
 3
 4# Force remove running container
 5docker rm -f mycontainer
 6
 7# Remove multiple containers
 8docker rm container1 container2 container3
 9
10# Remove all stopped containers
11docker container prune
12
13# Remove all containers (including running)
14docker rm -f $(docker ps -aq)

Volumes

 1# Create volume
 2docker volume create myvolume
 3
 4# List volumes
 5docker volume ls
 6
 7# Inspect volume
 8docker volume inspect myvolume
 9
10# Remove volume
11docker volume rm myvolume
12
13# Remove all unused volumes
14docker volume prune
15
16# Remove all volumes
17docker volume rm $(docker volume ls -q)
18
19# Backup volume
20docker run --rm -v myvolume:/source -v $(pwd):/backup alpine tar czf /backup/backup.tar.gz -C /source .
21
22# Restore volume
23docker run --rm -v myvolume:/target -v $(pwd):/backup alpine tar xzf /backup/backup.tar.gz -C /target

Networks

 1# Create network
 2docker network create mynetwork
 3
 4# Create network with subnet
 5docker network create --subnet=172.18.0.0/16 mynetwork
 6
 7# List networks
 8docker network ls
 9
10# Inspect network
11docker network inspect mynetwork
12
13# Connect container to network
14docker network connect mynetwork mycontainer
15
16# Disconnect container from network
17docker network disconnect mynetwork mycontainer
18
19# Remove network
20docker network rm mynetwork
21
22# Remove all unused networks
23docker network prune

System

 1# View Docker info
 2docker info
 3
 4# View Docker version
 5docker version
 6
 7# View disk usage
 8docker system df
 9
10# Clean up everything
11docker system prune
12
13# Clean up everything including volumes
14docker system prune -a --volumes
15
16# View events
17docker events
18
19# View events with filter
20docker events --filter "type=container"

Dockerfile Best Practices

 1# Use specific base image version
 2FROM node:18-alpine AS base
 3
 4# Set working directory
 5WORKDIR /app
 6
 7# Copy package files first (better caching)
 8COPY package*.json ./
 9
10# Install dependencies
11RUN npm ci --only=production
12
13# Copy application code
14COPY . .
15
16# Use non-root user
17RUN addgroup -g 1001 -S nodejs && \
18    adduser -S nodejs -u 1001
19USER nodejs
20
21# Expose port
22EXPOSE 3000
23
24# Health check
25HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
26  CMD node healthcheck.js
27
28# Start application
29CMD ["node", "server.js"]

Multi-Stage Build

 1# Build stage
 2FROM node:18-alpine AS builder
 3WORKDIR /app
 4COPY package*.json ./
 5RUN npm ci
 6COPY . .
 7RUN npm run build
 8
 9# Production stage
10FROM node:18-alpine AS production
11WORKDIR /app
12COPY package*.json ./
13RUN npm ci --only=production
14COPY --from=builder /app/dist ./dist
15USER node
16EXPOSE 3000
17CMD ["node", "dist/server.js"]

Go Multi-Stage Build

 1# Build stage
 2FROM golang:1.21-alpine AS builder
 3WORKDIR /app
 4COPY go.* ./
 5RUN go mod download
 6COPY . .
 7RUN CGO_ENABLED=0 GOOS=linux go build -o main .
 8
 9# Production stage
10FROM alpine:latest
11RUN apk --no-cache add ca-certificates
12WORKDIR /root/
13COPY --from=builder /app/main .
14EXPOSE 8080
15CMD ["./main"]

Docker Compose Quick Reference

 1version: '3.8'
 2
 3services:
 4  app:
 5    build:
 6      context: .
 7      dockerfile: Dockerfile
 8      target: production
 9    image: myapp:latest
10    container_name: myapp
11    ports:
12      - "8080:80"
13    environment:
14      - NODE_ENV=production
15      - DATABASE_URL=postgres://db:5432/mydb
16    env_file:
17      - .env
18    volumes:
19      - ./data:/app/data
20      - app-logs:/var/log
21    networks:
22      - mynetwork
23    depends_on:
24      - db
25      - redis
26    restart: unless-stopped
27    healthcheck:
28      test: ["CMD", "curl", "-f", "http://localhost/health"]
29      interval: 30s
30      timeout: 10s
31      retries: 3
32      start_period: 40s
33
34  db:
35    image: postgres:15-alpine
36    container_name: postgres
37    environment:
38      POSTGRES_DB: mydb
39      POSTGRES_USER: user
40      POSTGRES_PASSWORD: password
41    volumes:
42      - postgres-data:/var/lib/postgresql/data
43    networks:
44      - mynetwork
45    restart: unless-stopped
46
47  redis:
48    image: redis:7-alpine
49    container_name: redis
50    networks:
51      - mynetwork
52    restart: unless-stopped
53
54volumes:
55  postgres-data:
56  app-logs:
57
58networks:
59  mynetwork:
60    driver: bridge

Common Patterns

Development with Hot Reload

1# Node.js with nodemon
2docker run -d \
3  -v $(pwd):/app \
4  -v /app/node_modules \
5  -p 3000:3000 \
6  -e NODE_ENV=development \
7  myapp npm run dev

Database Container

 1# PostgreSQL
 2docker run -d \
 3  --name postgres \
 4  -e POSTGRES_PASSWORD=password \
 5  -e POSTGRES_DB=mydb \
 6  -v postgres-data:/var/lib/postgresql/data \
 7  -p 5432:5432 \
 8  postgres:15-alpine
 9
10# MySQL
11docker run -d \
12  --name mysql \
13  -e MYSQL_ROOT_PASSWORD=password \
14  -e MYSQL_DATABASE=mydb \
15  -v mysql-data:/var/lib/mysql \
16  -p 3306:3306 \
17  mysql:8
18
19# MongoDB
20docker run -d \
21  --name mongo \
22  -e MONGO_INITDB_ROOT_USERNAME=admin \
23  -e MONGO_INITDB_ROOT_PASSWORD=password \
24  -v mongo-data:/data/db \
25  -p 27017:27017 \
26  mongo:7
27
28# Redis
29docker run -d \
30  --name redis \
31  -v redis-data:/data \
32  -p 6379:6379 \
33  redis:7-alpine redis-server --appendonly yes

Debugging

 1# View container logs
 2docker logs -f --tail 100 mycontainer
 3
 4# Execute shell in container
 5docker exec -it mycontainer sh
 6
 7# View container processes
 8docker top mycontainer
 9
10# View container resource usage
11docker stats mycontainer
12
13# Inspect container configuration
14docker inspect mycontainer | jq '.[0].Config'
15
16# View container network settings
17docker inspect mycontainer | jq '.[0].NetworkSettings'
18
19# Copy logs from container
20docker cp mycontainer:/var/log/app.log ./app.log

Troubleshooting

 1# Container won't start
 2docker logs mycontainer
 3docker inspect mycontainer
 4
 5# Port already in use
 6docker ps -a | grep 8080
 7lsof -i :8080  # Find process using port
 8
 9# Permission denied
10# Run as root or add user to docker group
11sudo usermod -aG docker $USER
12
13# Out of disk space
14docker system df
15docker system prune -a --volumes
16
17# Container keeps restarting
18docker logs --tail 50 mycontainer
19docker inspect mycontainer | jq '.[0].State'
20
21# Network issues
22docker network inspect bridge
23docker exec mycontainer ping google.com
24
25# DNS issues
26docker run --dns 8.8.8.8 myapp
27
28# Can't connect to Docker daemon
29# Check if Docker is running
30sudo systemctl status docker
31sudo systemctl start docker

Notes

Best Practices:

  • ✅ Use specific image tags, not latest
  • ✅ Use multi-stage builds to reduce image size
  • ✅ Run containers as non-root user
  • ✅ Use .dockerignore to exclude unnecessary files
  • ✅ Minimize layers by combining RUN commands
  • ✅ Use health checks for production containers
  • ✅ Set resource limits (memory, CPU)
  • ✅ Use named volumes for persistent data
  • ✅ Use Docker Compose for multi-container apps

Security:

  • ✅ Scan images for vulnerabilities: docker scan myapp:latest
  • ✅ Don't store secrets in images
  • ✅ Use secrets management (Docker secrets, env files)
  • ✅ Keep base images updated
  • ✅ Use official images when possible

Related Snippets