Docker Cheatsheet
Container Lifecycle
Section titled “Container Lifecycle”# Run a container (pulls image if not local)docker run nginx:alpine
# Common run flagsdocker run \ --name my-app \ # assign a name -d \ # detached (background) -p 8080:80 \ # host:container port mapping -e NODE_ENV=production \ # environment variable --rm \ # auto-remove on exit --restart unless-stopped \ # restart policy nginx:alpine
# Interactive shell in a new containerdocker run -it --rm ubuntu:24.04 bash
# Start / stop / restart existing containersdocker start my-appdocker stop my-app # SIGTERM → SIGKILL after grace perioddocker restart my-app
# Kill immediately (SIGKILL, no grace period)docker kill my-app
# Pause / unpause (freezes process, keeps memory)docker pause my-appdocker unpause my-app
# Remove a stopped containerdocker rm my-app
# Remove a running container forcefullydocker rm -f my-app
# Remove all stopped containersdocker container pruneExec & Logs
Section titled “Exec & Logs”# Open a shell in a running containerdocker exec -it my-app bashdocker exec -it my-app sh # for Alpine / minimal images
# Run a one-off commanddocker exec my-app cat /etc/os-release
# Stream logsdocker logs -f my-app
# Last 50 lines with timestampsdocker logs --tail 50 --timestamps my-app
# Logs since a point in timedocker logs --since 1h my-appInspecting Containers
Section titled “Inspecting Containers”# List running containersdocker ps
# List all containers (including stopped)docker ps -a
# Full JSON metadatadocker inspect my-app
# Extract a specific field (jq or Go template)docker inspect -f '{{ .NetworkSettings.IPAddress }}' my-app
# Resource usage (live)docker stats
# Stats snapshot (no stream)docker stats --no-stream
# List running processes inside a containerdocker top my-app
# Port mappingsdocker port my-appCopying Files
Section titled “Copying Files”# Host → containerdocker cp ./config.yaml my-app:/app/config.yaml
# Container → hostdocker cp my-app:/var/log/app.log ./app.logImage Management
Section titled “Image Management”# Pull an imagedocker pull nginx:alpine
# Pull by digest (immutable)docker pull nginx@sha256:abc123...
# List local imagesdocker imagesdocker image ls
# Build from Dockerfile in current directorydocker build -t my-app:1.0 .
# Build with a non-standard Dockerfiledocker build -f Dockerfile.prod -t my-app:prod .
# Pass a build argumentdocker build --build-arg APP_VERSION=1.2 -t my-app:1.2 .
# Force a full rebuild (bypass cache)docker build --no-cache -t my-app:1.0 .
# Tag an existing imagedocker tag my-app:1.0 myregistry.io/team/my-app:1.0
# Remove an imagedocker rmi my-app:1.0
# Remove all dangling (untagged) imagesdocker image prune
# Remove all unused images (not just dangling)docker image prune -a
# Show layer history and sizesdocker history my-app:1.0
# Full JSON metadatadocker inspect my-app:1.0
# Save image(s) to a TAR archive (preserves layers + tags)docker save -o my-app.tar my-app:1.0
# Load images from a TAR archivedocker load -i my-app.tarRegistry
Section titled “Registry”# Login to Docker Hubdocker login
# Login to a private registrydocker login myregistry.azurecr.io
# Push an imagedocker push myregistry.io/team/my-app:1.0
# Pull from a private registry (must be logged in)docker pull myregistry.io/team/my-app:1.0
# Logoutdocker logout myregistry.ioVolumes & Mounts
Section titled “Volumes & Mounts”# Create a named volumedocker volume create app-data
# List volumesdocker volume ls
# Inspect a volume (shows mount path on host)docker volume inspect app-data
# Run with a named volumedocker run -d -v app-data:/app/data my-app:1.0
# Run with a bind mount (host path → container path)docker run -d -v "$(pwd)/data":/app/data my-app:1.0
# Read-only bind mountdocker run -d -v "$(pwd)/config":/app/config:ro my-app:1.0
# Temporary in-memory filesystem (tmpfs)docker run --tmpfs /tmp my-app:1.0
# Remove a volumedocker volume rm app-data
# Remove all unused volumesdocker volume pruneNetworking
Section titled “Networking”# List networksdocker network ls
# Inspect a networkdocker network inspect bridge
# Create a custom bridge networkdocker network create my-net
# Create with a specific subnetdocker network create --subnet 172.20.0.0/16 my-net
# Run a container on a specific networkdocker run -d --network my-net --name db postgres:16
# Connect a running container to a networkdocker network connect my-net my-app
# Disconnect a container from a networkdocker network disconnect my-net my-app
# Remove a networkdocker network rm my-net
# Remove all unused networksdocker network pruneDocker Compose
Section titled “Docker Compose”# Start services (detached), build if neededdocker compose up -d --build
# Start a specific servicedocker compose up -d db
# Stop and remove containers + networksdocker compose down
# Also remove named volumesdocker compose down -v
# View running servicesdocker compose ps
# Stream logs for all servicesdocker compose logs -f
# Stream logs for one servicedocker compose logs -f api
# Execute a command in a running service containerdocker compose exec api sh
# Run a one-off command (new container, then remove)docker compose run --rm api python manage.py migrate
# Scale a servicedocker compose up -d --scale worker=3
# Rebuild images without startingdocker compose build
# Pull latest images for all servicesdocker compose pull
# Validate and view the merged configdocker compose configSystem & Maintenance
Section titled “System & Maintenance”# Disk usage summary (images, containers, volumes, cache)docker system df
# Verbose disk usage (per object)docker system df -v
# Remove all stopped containers, unused networks,# dangling images, and build cachedocker system prune
# Also remove unused images and volumes (aggressive)docker system prune -a --volumes
# Live event stream from the Docker daemondocker events
# Filter events by typedocker events --filter type=container --filter event=die
# Display Docker version infodocker version
# Display system-wide info (daemon config, runtime)docker infoQuick Reference: docker run Flags
Section titled “Quick Reference: docker run Flags”| Flag | Short | Meaning |
|---|---|---|
--detach | -d | Run in background |
--interactive | -i | Keep STDIN open |
--tty | -t | Allocate a pseudo-TTY (use with -i) |
--name | Assign a name to the container | |
--publish | -p | Map host:container port |
--publish-all | -P | Map all exposed ports to random host ports |
--env | -e | Set an environment variable |
--env-file | Load env vars from a file | |
--volume | -v | Mount a volume or bind mount |
--mount | Explicit mount syntax (preferred over -v) | |
--network | Connect to a specific network | |
--rm | Remove container automatically on exit | |
--restart | Restart policy (no, always, unless-stopped, on-failure) | |
--user | -u | Run as a specific user (uid:gid) |
--cpus | Limit CPU usage (e.g. --cpus 0.5) | |
--memory | -m | Limit memory (e.g. -m 512m) |
--read-only | Mount container root filesystem as read-only | |
--entrypoint | Override the image ENTRYPOINT | |
--platform | Target platform (e.g. linux/arm64) |
Quick Reference: Restart Policies
Section titled “Quick Reference: Restart Policies”| Policy | Behaviour |
|---|---|
no | Never restart (default) |
always | Always restart, including on daemon start |
unless-stopped | Always restart unless explicitly stopped by the user |
on-failure[:n] | Restart only on non-zero exit code; optional max retry count |