All cheatsheets

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-alpinecached
COPY package*.json ./cached
RUN npm cicached
COPY . .cached
RUN npm run buildcached

No 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).

Loading 3D…

35 entries

Containers9

docker run -d -p 8080:80 nginx

Run detached, mapping a port

docker run -it ubuntu bash

Run interactively with a shell

docker run --rm -v $PWD:/app -w /app node npm ci

Throwaway container, mount cwd

docker ps

List running containers

docker ps -a

List all containers (incl. stopped)

docker exec -it <id> sh

Open 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 ./local

Copy 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 images

List 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=production

Build-time arg vs runtime env

HEALTHCHECK CMD curl -f localhost/ || exit 1

Container health probe

Volumes & bind mounts2

docker run -v mydata:/var/lib/postgresql/data postgres

Named volume — Docker manages the data lifecycle

docker run -v $PWD:/app node

Bind mount — host path mirrored into container

Networks3

docker network create mynet docker run --network mynet --name api myapp docker run --network mynet --name db postgres

User-defined bridge network — containers reach each other by name

docker network ls

List networks

docker network inspect mynet

Show network details and connected containers

Compose5

docker compose up -d

Start services in the background

docker compose down -v

Stop services and remove volumes

docker compose logs -f <svc>

Follow a service's logs

docker compose exec <svc> sh

Shell 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: 5

compose.yaml: web waits for DB to be healthy

System & cleanup5

docker system df

Show Docker disk usage

docker system prune -a

Remove unused images, networks, build cache

docker volume prune

Remove unused volumes

docker stats

Live resource usage of containers

docker inspect <id>

Low-level details as JSON