// REF. 06 / CONTAINERS

Docker cheat sheet

Containers are the easy part. The hard part is remembering which of the eleven prune commands deletes your database. This mat covers the Docker CLI and docker compose (v2, no hyphen) as documented for Engine 29 and Compose v5, with a column for the Dockerfile instructions that decide whether your image is 80 MB or 2 GB. Every key is a subcommand; the docker prefix is implied.

Two habits that pay for the mat: write a .dockerignore before your first COPY . ., and build in stages so the compiler never ships. The HERE BE DRAGONS row at the bottom is short because the big one, system prune --volumes, is already in CLEANUP with a warning.

↓ Download the printable PDF free · letter size · no email needed

Run

run -d --name web nginx
start in the background, with a name
run -it --rm img sh
interactive shell, removed on exit
-p 8080:80
host port 8080 to container port 80
-v ./data:/app:ro
bind mount a host dir, read-only
-e KEY=val
set an env var; --env-file for many
--restart unless-stopped
come back after crashes and reboots

Containers

ps -a
all containers, stopped ones included
logs -f --tail 100 web
follow, starting from the last 100 lines
exec -it web sh
shell into a running container
stop web
SIGTERM, then SIGKILL after 10s
rm -f web
remove it, running or not
inspect web
everything about it, as json; -f templates

Images

build -t app:1.0 .
build from ./Dockerfile and tag it
build --target build .
stop at a named stage
images
local images; -f dangling=true for orphans
pull img@sha256:<digest>
pin by digest; tags move, digests don't
push reg/app:1.0
upload to a registry (tag it first)
image prune -a
drop every image no container uses

Dockerfile

FROM img:tag AS build
base image; AS names the stage
COPY src dst
files from build context (prefer over ADD)
COPY --from=build /a /b
take files from an earlier stage
ARG X=1 / ENV X=1
build-time only / baked into the image
RUN --mount=type=cache
keep package caches between builds
CMD ["bin","arg"]
default command, exec form; overridable
ENTRYPOINT ["bin"]
always runs; CMD becomes its arguments
HEALTHCHECK CMD <cmd>
exit 1 means unhealthy; --interval=30s

Compose

compose up -d --build
build, create, start, detached
compose down -v
stop and remove, named volumes too
compose logs -f web
follow one service's output
compose exec web sh
shell into a running service
compose run --rm web cmd
one-off command in a fresh container
compose watch
rebuild or sync when source files change
compose config
render the merged file; catches typos

Compose file

image: nginx:1.27
pull this image; or build: ./dir
ports: ["8080:80"]
host:container, quoted
volumes: [data:/app]
named volume; declare it top-level too
env_file: .env
load vars; environment: wins on clash
service_healthy
depends_on condition: wait for healthcheck
restart: unless-stopped
survives reboots, not an explicit stop

Volumes & networks

volume create data
named volume, managed by the engine
-v data:/app
mount it; ./path would be a bind mount
volume ls
list; -f dangling=true for unused ones
volume prune
unused anonymous volumes; -a named too
network create net
bridge; members resolve each other by name
network connect net web
join a running container to a network

Cleanup

system prune -a
unused containers, networks, images, cache
system prune --volumes
also anonymous volumes — (data too)
system df -v
what is eating the disk, item by item
builder prune
just the build cache

Here be dragons

:latest
not newest; just the tag nobody set
rm -f $(docker ps -aq)
everything, running or not, no questions
CMD npm start
shell form: sh is PID 1, signals get lost
no .dockerignore
COPY . . ships node_modules, .git, .env
Docker Field Manual Desk Mat

// KEEP IT UNDER YOUR KEYBOARD

Docker Field Manual Desk Mat

from $34 · printed to order in the USA

See the desk mat