Cheatsheets
Docker
Everyday Docker & Compose commands for containers, images, and cleanup.
Visualize
Image layers & build cache
Change a file and watch which layers rebuild.
FROM node:20-alpinecachedCOPY package*.json ./cachedRUN npm cicachedCOPY . .cachedRUN npm run buildcachedNo changes → every layer is a cache hit. `docker build` is instant.
Docker — image layers & containers (3D)
An image is a stack of read-only layers; each container adds one thin writable layer on top (copy-on-write).
35 entries
Containers9
docker run -d -p 8080:80 nginxRun detached, mapping a port
docker run -it ubuntu bashRun interactively with a shell
docker run --rm -v $PWD:/app -w /app node npm ciThrowaway container, mount cwd
docker psList running containers
docker ps -aList all containers (incl. stopped)
docker exec -it <id> shOpen a shell in a running container
docker logs -f --tail 100 <id>Follow the last 100 log lines
docker stop <id> && docker rm <id>Stop and remove a container
docker cp <id>:/path ./localCopy files out of a container
Images7
docker build -t name:tag .Build an image from the Dockerfile
docker build --no-cache .Build without using the cache
docker imagesList local images
docker pull <image>Download an image from a registry
docker tag <src> <target>Add a tag to an image
docker push <image>Upload an image to a registry
docker history <image>Show an image's layer history
Dockerfile4
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
EXPOSE 3000
CMD ["npm", "start"]Minimal Node image (cache deps before source)
FROM node:22-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=builder /app/dist ./dist
EXPOSE 3000
CMD ["node", "dist/server.js"]Multi-stage build — small production image
ARG VERSION
ENV NODE_ENV=productionBuild-time arg vs runtime env
HEALTHCHECK CMD curl -f localhost/ || exit 1Container health probe
Volumes & bind mounts2
docker run -v mydata:/var/lib/postgresql/data postgresNamed volume — Docker manages the data lifecycle
docker run -v $PWD:/app nodeBind mount — host path mirrored into container
Networks3
docker network create mynet
docker run --network mynet --name api myapp
docker run --network mynet --name db postgresUser-defined bridge network — containers reach each other by name
docker network lsList networks
docker network inspect mynetShow network details and connected containers
Compose5
docker compose up -dStart services in the background
docker compose down -vStop services and remove volumes
docker compose logs -f <svc>Follow a service's logs
docker compose exec <svc> shShell into a running service
services:
web:
build: .
ports: ["3000:3000"]
depends_on:
db:
condition: service_healthy
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: secret
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
retries: 5compose.yaml: web waits for DB to be healthy
System & cleanup5
docker system dfShow Docker disk usage
docker system prune -aRemove unused images, networks, build cache
docker volume pruneRemove unused volumes
docker statsLive resource usage of containers
docker inspect <id>Low-level details as JSON