Skip to content

Docker Cheatsheet

Terminal window
# Run a container (pulls image if not local)
docker run nginx:alpine
# Common run flags
docker 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 container
docker run -it --rm ubuntu:24.04 bash
# Start / stop / restart existing containers
docker start my-app
docker stop my-app # SIGTERM → SIGKILL after grace period
docker restart my-app
# Kill immediately (SIGKILL, no grace period)
docker kill my-app
# Pause / unpause (freezes process, keeps memory)
docker pause my-app
docker unpause my-app
# Remove a stopped container
docker rm my-app
# Remove a running container forcefully
docker rm -f my-app
# Remove all stopped containers
docker container prune
Terminal window
# Open a shell in a running container
docker exec -it my-app bash
docker exec -it my-app sh # for Alpine / minimal images
# Run a one-off command
docker exec my-app cat /etc/os-release
# Stream logs
docker logs -f my-app
# Last 50 lines with timestamps
docker logs --tail 50 --timestamps my-app
# Logs since a point in time
docker logs --since 1h my-app
Terminal window
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Full JSON metadata
docker 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 container
docker top my-app
# Port mappings
docker port my-app
Terminal window
# Host → container
docker cp ./config.yaml my-app:/app/config.yaml
# Container → host
docker cp my-app:/var/log/app.log ./app.log

Terminal window
# Pull an image
docker pull nginx:alpine
# Pull by digest (immutable)
docker pull nginx@sha256:abc123...
# List local images
docker images
docker image ls
# Build from Dockerfile in current directory
docker build -t my-app:1.0 .
# Build with a non-standard Dockerfile
docker build -f Dockerfile.prod -t my-app:prod .
# Pass a build argument
docker 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 image
docker tag my-app:1.0 myregistry.io/team/my-app:1.0
# Remove an image
docker rmi my-app:1.0
# Remove all dangling (untagged) images
docker image prune
# Remove all unused images (not just dangling)
docker image prune -a
# Show layer history and sizes
docker history my-app:1.0
# Full JSON metadata
docker 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 archive
docker load -i my-app.tar

Terminal window
# Login to Docker Hub
docker login
# Login to a private registry
docker login myregistry.azurecr.io
# Push an image
docker 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
# Logout
docker logout myregistry.io

Terminal window
# Create a named volume
docker volume create app-data
# List volumes
docker volume ls
# Inspect a volume (shows mount path on host)
docker volume inspect app-data
# Run with a named volume
docker 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 mount
docker 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 volume
docker volume rm app-data
# Remove all unused volumes
docker volume prune

Terminal window
# List networks
docker network ls
# Inspect a network
docker network inspect bridge
# Create a custom bridge network
docker network create my-net
# Create with a specific subnet
docker network create --subnet 172.20.0.0/16 my-net
# Run a container on a specific network
docker run -d --network my-net --name db postgres:16
# Connect a running container to a network
docker network connect my-net my-app
# Disconnect a container from a network
docker network disconnect my-net my-app
# Remove a network
docker network rm my-net
# Remove all unused networks
docker network prune

Terminal window
# Start services (detached), build if needed
docker compose up -d --build
# Start a specific service
docker compose up -d db
# Stop and remove containers + networks
docker compose down
# Also remove named volumes
docker compose down -v
# View running services
docker compose ps
# Stream logs for all services
docker compose logs -f
# Stream logs for one service
docker compose logs -f api
# Execute a command in a running service container
docker compose exec api sh
# Run a one-off command (new container, then remove)
docker compose run --rm api python manage.py migrate
# Scale a service
docker compose up -d --scale worker=3
# Rebuild images without starting
docker compose build
# Pull latest images for all services
docker compose pull
# Validate and view the merged config
docker compose config

Terminal window
# 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 cache
docker system prune
# Also remove unused images and volumes (aggressive)
docker system prune -a --volumes
# Live event stream from the Docker daemon
docker events
# Filter events by type
docker events --filter type=container --filter event=die
# Display Docker version info
docker version
# Display system-wide info (daemon config, runtime)
docker info

FlagShortMeaning
--detach-dRun in background
--interactive-iKeep STDIN open
--tty-tAllocate a pseudo-TTY (use with -i)
--nameAssign a name to the container
--publish-pMap host:container port
--publish-all-PMap all exposed ports to random host ports
--env-eSet an environment variable
--env-fileLoad env vars from a file
--volume-vMount a volume or bind mount
--mountExplicit mount syntax (preferred over -v)
--networkConnect to a specific network
--rmRemove container automatically on exit
--restartRestart policy (no, always, unless-stopped, on-failure)
--user-uRun as a specific user (uid:gid)
--cpusLimit CPU usage (e.g. --cpus 0.5)
--memory-mLimit memory (e.g. -m 512m)
--read-onlyMount container root filesystem as read-only
--entrypointOverride the image ENTRYPOINT
--platformTarget platform (e.g. linux/arm64)

PolicyBehaviour
noNever restart (default)
alwaysAlways restart, including on daemon start
unless-stoppedAlways restart unless explicitly stopped by the user
on-failure[:n]Restart only on non-zero exit code; optional max retry count