Docker Commands Cheat Sheet: Complete Guide for 2026

Published 2026-02-04 • Updated 2026-02-04

Published February 5, 2026 • 16 min read

Docker revolutionized application deployment by making containers accessible and practical. Whether you're learning Docker basics or need a quick reference for advanced operations, this comprehensive cheat sheet covers every essential Docker command with real-world examples.

Installation & Setup

Check Docker Version

# Docker version
docker --version
docker version

# Docker Compose version
docker compose version

Docker Info

# System-wide information
docker info

# Show Docker disk usage
docker system df

Container Management

Running Containers

# Run container from image
docker run nginx

# Run in detached mode (background)
docker run -d nginx

# Run with name
docker run --name my-nginx -d nginx

# Run with port mapping (host:container)
docker run -p 8080:80 -d nginx

# Run with environment variables
docker run -e NODE_ENV=production -d my-app

# Run with volume mount
docker run -v /host/path:/container/path -d nginx

# Run and remove after exit
docker run --rm nginx

# Run interactively with bash
docker run -it ubuntu bash

# Run with resource limits
docker run --memory="512m" --cpus="1.0" -d my-app

# Complete example
docker run -d \
  --name my-web-server \
  -p 8080:80 \
  -e ENV=production \
  -v /data:/usr/share/nginx/html \
  --restart unless-stopped \
  nginx:latest

Listing Containers

# List running containers
docker ps

# List all containers (including stopped)
docker ps -a

# List with custom format
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"

# Show only container IDs
docker ps -q

# Show last created container
docker ps -l

Container Control

# Start stopped container
docker start container-name

# Stop running container
docker stop container-name

# Stop with timeout (default 10s)
docker stop -t 30 container-name

# Restart container
docker restart container-name

# Pause container
docker pause container-name

# Unpause container
docker unpause container-name

# Kill container (force stop)
docker kill container-name

# Remove container
docker rm container-name

# Remove running container (force)
docker rm -f container-name

# Remove all stopped containers
docker container prune

Container Inspection

# View container logs
docker logs container-name

# Follow logs (like tail -f)
docker logs -f container-name

# Show last 100 lines
docker logs --tail 100 container-name

# Show logs with timestamps
docker logs -t container-name

# Inspect container details
docker inspect container-name

# Show specific field
docker inspect -f '{{.State.Status}}' container-name

# Show container processes
docker top container-name

# Show container resource usage
docker stats container-name

# Show live stats for all containers
docker stats

Executing Commands in Containers

# Execute command in running container
docker exec container-name ls -la

# Open bash shell in container
docker exec -it container-name bash

# Execute as specific user
docker exec -u root container-name whoami

# Execute in working directory
docker exec -w /app container-name npm test

Copying Files

# Copy from host to container
docker cp /host/file.txt container-name:/path/in/container/

# Copy from container to host
docker cp container-name:/app/logs.txt /host/path/

# Copy directory
docker cp /host/directory container-name:/path/

Image Management

Working with Images

# List images
docker images

# List all images (including intermediate)
docker images -a

# Pull image from registry
docker pull nginx

# Pull specific version
docker pull nginx:1.21

# Pull from different registry
docker pull ghcr.io/username/repo:tag

# Search Docker Hub
docker search nginx

# Show image history
docker history nginx

# Inspect image details
docker inspect nginx

# Remove image
docker rmi nginx

# Remove image by ID
docker rmi abc123

# Force remove
docker rmi -f nginx

# Remove unused images
docker image prune

# Remove all unused images (not just dangling)
docker image prune -a

Building Images

# Build image from Dockerfile
docker build -t my-app:latest .

# Build with tag
docker build -t my-app:v1.0 .

# Build with build arguments
docker build --build-arg VERSION=1.0 -t my-app .

# Build without cache
docker build --no-cache -t my-app .

# Build from specific Dockerfile
docker build -f Dockerfile.prod -t my-app:prod .

# Build with target stage (multi-stage)
docker build --target production -t my-app:prod .

Tagging Images

# Tag image
docker tag my-app:latest my-app:v1.0

# Tag for registry
docker tag my-app:latest username/my-app:latest
docker tag my-app:latest ghcr.io/username/my-app:latest

Pushing Images

# Login to registry
docker login
docker login ghcr.io

# Push to registry
docker push username/my-app:latest

# Logout
docker logout

Saving & Loading Images

# Save image to tar file
docker save -o my-app.tar my-app:latest

# Save multiple images
docker save -o images.tar nginx redis postgres

# Load image from tar file
docker load -i my-app.tar

# Export container filesystem
docker export container-name > container.tar

# Import container filesystem as image
docker import container.tar my-app:from-export

Volumes

# List volumes
docker volume ls

# Create volume
docker volume create my-volume

# Inspect volume
docker volume inspect my-volume

# Remove volume
docker volume rm my-volume

# Remove all unused volumes
docker volume prune

# Use volume with container
docker run -v my-volume:/app/data -d my-app

# Mount host directory (bind mount)
docker run -v /host/path:/container/path -d my-app

# Read-only mount
docker run -v my-volume:/app/data:ro -d my-app

Networks

# List networks
docker network ls

# Create network
docker network create my-network

# Create network with subnet
docker network create --subnet=172.18.0.0/16 my-network

# Inspect network
docker network inspect my-network

# Connect container to network
docker network connect my-network container-name

# Disconnect container from network
docker network disconnect my-network container-name

# Remove network
docker network rm my-network

# Remove unused networks
docker network prune

# Run container on specific network
docker run --network my-network -d my-app

Docker Compose

Basic Commands

# Start services (detached)
docker compose up -d

# Start and rebuild
docker compose up -d --build

# Start specific service
docker compose up -d web

# Stop services
docker compose stop

# Stop and remove containers
docker compose down

# Stop and remove with volumes
docker compose down -v

# View running services
docker compose ps

# View logs
docker compose logs

# Follow logs
docker compose logs -f

# Logs for specific service
docker compose logs -f web

# Execute command in service
docker compose exec web bash

# Run one-off command
docker compose run web npm test

# Scale service
docker compose up -d --scale web=3

# Restart service
docker compose restart web

# Pull latest images
docker compose pull

# Validate compose file
docker compose config

Example docker-compose.yml

version: '3.8'

services:
  web:
    build: .
    ports:
      - "8080:80"
    environment:
      - NODE_ENV=production
    volumes:
      - ./app:/usr/src/app
    depends_on:
      - db
    restart: unless-stopped

  db:
    image: postgres:15
    environment:
      - POSTGRES_PASSWORD=secret
    volumes:
      - db-data:/var/lib/postgresql/data
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    restart: unless-stopped

volumes:
  db-data:

System Maintenance

# Remove all stopped containers
docker container prune

# Remove all unused images
docker image prune -a

# Remove all unused volumes
docker volume prune

# Remove all unused networks
docker network prune

# Clean everything (dangerous!)
docker system prune -a --volumes

# Show disk usage
docker system df

# Detailed disk usage
docker system df -v

Dockerfile Examples

Node.js Application

FROM node:18-alpine

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production

# Copy application
COPY . .

# Expose port
EXPOSE 3000

# Start application
CMD ["node", "server.js"]

Multi-Stage Build (Node.js)

# Build stage
FROM node:18-alpine AS builder

WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Production stage
FROM node:18-alpine

WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY --from=builder /app/dist ./dist

EXPOSE 3000
CMD ["node", "dist/server.js"]

Python Application

FROM python:3.11-slim

WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application
COPY . .

# Expose port
EXPOSE 8000

# Run application
CMD ["python", "app.py"]

.dockerignore Example

node_modules
npm-debug.log
.git
.gitignore
README.md
.env
.DS_Store
*.log

Advanced Commands

Container Resource Limits

# Memory limit
docker run -m 512m my-app

# CPU limit
docker run --cpus="1.5" my-app

# Memory and CPU
docker run -m 1g --cpus="2.0" my-app

# Memory reservation (soft limit)
docker run --memory-reservation="256m" my-app

# Update running container limits
docker update --memory="1g" container-name

Health Checks

# In Dockerfile
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD curl -f http://localhost/ || exit 1

# Run with healthcheck
docker run \
  --health-cmd="curl -f http://localhost/ || exit 1" \
  --health-interval=30s \
  --health-timeout=3s \
  --health-retries=3 \
  -d my-app

Security

# Run as non-root user
docker run -u 1000:1000 my-app

# Read-only filesystem
docker run --read-only my-app

# Drop capabilities
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE my-app

# Security options
docker run --security-opt=no-new-privileges:true my-app

Container Registry

# Run local registry
docker run -d -p 5000:5000 --name registry registry:2

# Tag image for local registry
docker tag my-app:latest localhost:5000/my-app:latest

# Push to local registry
docker push localhost:5000/my-app:latest

# Pull from local registry
docker pull localhost:5000/my-app:latest

Troubleshooting

Common Issues

# Container won't start
docker logs container-name
docker inspect container-name

# Check container events
docker events

# Check what processes are running
docker top container-name

# Monitor resource usage
docker stats container-name

# Check port mappings
docker port container-name

# Attach to running container (see output)
docker attach container-name

# View changes to container filesystem
docker diff container-name

Debugging Build Issues

# Build with verbose output
docker build --progress=plain -t my-app .

# Build without cache
docker build --no-cache -t my-app .

# Build to specific stage
docker build --target builder -t my-app:debug .

Best Practices

Image Building

Container Management

Security

Quick Reference Commands

Command Description
docker run -d nginx Run container in background
docker ps List running containers
docker stop <name> Stop container
docker logs -f <name> Follow container logs
docker exec -it <name> bash Open shell in container
docker build -t name . Build image from Dockerfile
docker images List images
docker compose up -d Start services with Compose
docker system prune Clean up unused resources
docker stats View resource usage

Learning Resources

Conclusion

Docker simplifies application deployment and ensures consistency across environments. Master these commands and you'll be able to:

Bookmark this cheat sheet and practice the commands. Docker becomes second nature with regular use. Start with the basics (run, ps, logs, exec) and gradually explore advanced features as your needs grow.

More Developer Tools

Check out our Git Commands Cheat Sheet and Best Developer Tools 2026 for more essential development references.

Related Posts

Back to Blog

Share This