Docker
Docker cheat sheet with essential commands for containers, images, volumes, networks, Docker Compose, and orchestration examples.
Container Management
Start and run Docker containers with various options
# Run container
docker run nginx
docker run -d nginx # Detached mode
docker run -it ubuntu bash # Interactive
docker run --name web nginx # Named container
# Port mapping
docker run -p 8080:80 nginx # Host:Container
docker run -P nginx # Random ports
# Volume mounting
docker run -v /host:/container nginx # Bind mount
docker run -v myvolume:/data nginx # Named volume
# Environment
docker run -e NODE_ENV=prod node # Single var
docker run --env-file .env node # From fileControl container lifecycle - start, stop, restart, and remove
# List containers
docker ps # Running only
docker ps -a # All containers
docker ps -q # Only IDs
# Start/Stop/Restart
docker start container_name
docker stop container_name
docker restart container_name
# Remove containers
docker rm container_name
docker rm -f container_name # Force remove
# Clean up
docker container prune # Remove stoppedDebug and inspect running containers for troubleshooting
# View logs
docker logs container_name
docker logs -f container_name # Follow
docker logs --tail 50 container_name # Last 50 lines
# Execute commands
docker exec container_name ls
docker exec -it container_name bash
# Inspect container
docker inspect container_name
# Copy files
docker cp container:/path/file local/path
docker cp local/file container:/path/Run commands inside running containers with docker exec and copy files with docker cp
# Run a command in a running container
docker exec my-container ls /app
# Open an interactive shell
docker exec -it my-container sh
docker exec -it my-container bash
# Copy files between host and container
docker cp my-container:/app/logs ./logs
docker cp ./config.yml my-container:/app/config.ymlImage Management
Build Docker images from Dockerfiles with various options
# Build image
docker build -t myapp .
docker build -t myapp:v1 .
docker build -f Dockerfile.dev -t myapp:dev .
# Build arguments
docker build --build-arg VERSION=1.0 -t myapp .
# Build options
docker build --no-cache -t myapp .
docker build --pull -t myapp .Manage Docker images - pull, push, tag, and remove
# List images
docker images
docker image ls
# Pull images
docker pull nginx
docker pull nginx:alpine
# Push images
docker push myregistry/myapp:v1
# Tag images
docker tag myapp:latest myapp:v1.0
docker tag myapp registry/myapp:latest
# Remove images
docker rmi image_name
docker image prune # Dangling imagesWork with Docker registries and manage image distribution
# Login to registry
docker login
docker login docker.io
docker login -u user -p pass registry.io
# Logout
docker logout
docker logout registry.io
# Search images
docker search nginx
docker search --limit 10 nginx
docker search --filter stars=100 nginx
# Registry operations
docker pull registry.io/namespace/image:tag
docker push registry.io/namespace/image:tag
# Local registry
docker run -d -p 5000:5000 --name registry registry:2
docker tag myapp localhost:5000/myapp
docker push localhost:5000/myappDockerfile
Write Dockerfiles to build custom images with all the key instructions
The essential Dockerfile commands for building images
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --production
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
CMD ["node", "server.js"]Use multiple FROM stages to create small, optimized production images
# Build stage
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Production stage — only the output
FROM node:22-alpine AS production
WORKDIR /app
COPY /app/dist ./dist
COPY /app/node_modules ./node_modules
CMD ["node", "dist/server.js"]Exclude files from the build context to speed up builds and avoid leaking secrets
# .dockerignore
node_modules
.git
.env
*.md
dist
.DS_StoreVolumes & Networks
Create and manage Docker volumes for persistent data storage
# Volume operations
docker volume create myvolume
docker volume ls
docker volume inspect myvolume
docker volume rm myvolume
docker volume prune
# Use volumes
docker run -v myvolume:/data nginx
docker run -v /host/path:/container/path nginxConfigure Docker networks for container communication
# Network operations
docker network create mynetwork
docker network ls
docker network inspect bridge
docker network rm mynetwork
docker network prune
# Container networking
docker run --network=mynetwork nginx
docker network connect mynetwork container
docker network disconnect mynetwork containerDocker Compose
Essential Docker Compose commands for multi-container applications
# Start services
docker compose up
docker compose up -d # Detached
docker compose up --build # Rebuild
# Stop services
docker compose down
docker compose down -v # Remove volumes
docker compose stop
# Logs and status
docker compose ps
docker compose logs
docker compose logs -f service
# Execute commands
docker compose exec service bash
docker compose run service commandFull compose.yml reference with depends_on, healthcheck, profiles, watch, and more
services:
app:
build: .
ports:
- "3000:3000"
depends_on:
db:
condition: service_healthy
environment:
DATABASE_URL: postgres://user:pass@db:5432/mydb
db:
image: postgres:17-alpine
volumes:
- db-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5
volumes:
db-data:System & Cleanup
Monitor and manage Docker system resources and information
# System info
docker info
docker version
docker system df
# Cleanup
docker system prune
docker system prune -a
docker container prune
docker image prune
docker volume prune
docker network prune
# Stats
docker stats
docker stats --no-streamDebug containers and resolve common Docker issues
# Debug running container
docker logs container_name
docker logs -f --tail 50 container_name
docker exec -it container_name sh
# Inspect details
docker inspect container_name
docker inspect image_name
# Debug build
docker build --no-cache --progress=plain .
# Check processes
docker top container_name
# File changes
docker diff container_name