docker, updated

Docker cheat sheet: basic usage

The everyday Docker commands in one place: images, docker run flags, container lifecycle, exec and logs, Compose, registries and contexts.

Images

WhatCommand
Pull an imagedocker pull nginx:1.27
Pull by digest (immutable)docker pull nginx@sha256:<digest>
List imagesdocker images
Build from ./Dockerfiledocker build -t myapp:1.0 .
Build with another Dockerfile, no cachedocker build -f Dockerfile.prod --no-cache -t myapp:prod .
Build argumentdocker build --build-arg VERSION=1.2 -t myapp .
Build for another platformdocker buildx build --platform linux/arm64 -t myapp .
Tag for a registrydocker tag myapp:1.0 registry.example.com/team/myapp:1.0
Pushdocker push registry.example.com/team/myapp:1.0
Layers and the commands that made themdocker history --no-trunc myapp:1.0
Remove an imagedocker rmi myapp:1.0
Save to / load from a tar filedocker save -o myapp.tar myapp:1.0 and docker load -i myapp.tar

docker history --no-trunc shows every RUN, ENV and ARG of the build, including secrets someone passed as build arguments.

docker run

docker run -d --name web -p 127.0.0.1:8080:80 --restart unless-stopped nginx:1.27
FlagDoes
-dRun in the background (detached)
-itInteractive with a terminal, for shells
--rmDelete the container when it exits
--name webFixed name instead of a random one
-p 127.0.0.1:8080:80Publish container port 80 on host port 8080, localhost only (Networking)
-v /srv/data:/dataBind mount a host path
-v appdata:/dataNamed volume
-v /etc/app.conf:/etc/app.conf:roMount read-only
-e KEY=value / --env-file .envEnvironment variables
--network appnetAttach to a network
--restart unless-stoppedRestart policy: no, on-failure[:N], always, unless-stopped
-w /appWorking directory
--entrypoint shOverride the image’s entrypoint
--user 1000:1000Run as this UID:GID instead of the image default
--read-onlyRead-only root filesystem (add --tmpfs /tmp for scratch space)
--cap-drop ALL --cap-add NET_BIND_SERVICEDrop all capabilities, add back only what’s needed
--security-opt no-new-privilegesBlock privilege escalation via setuid binaries
--memory 512m --cpus 1.5Resource limits
--pids-limit 200Limit processes (fork bomb protection)
--log-opt max-size=10mLog size limit for this container (Cleanup)
--privilegedFull access to the host. Avoid; it is root on the host in practice
# Throwaway shell in an image
docker run --rm -it alpine sh

# Same, but override an image that has its own entrypoint
docker run --rm -it --entrypoint sh nginx:1.27

# One-off command with the current directory mounted
docker run --rm -v "$PWD":/work -w /work python:3.13 python script.py

# Hardened service
docker run -d --name api --read-only --tmpfs /tmp --user 1000:1000 \
  --cap-drop ALL --security-opt no-new-privileges --pids-limit 200 myapp:1.0

Container lifecycle

WhatCommand
Running containersdocker ps
All containers, also stoppeddocker ps -a
Only IDs (for scripts)docker ps -aq
Start / stop / restartdocker start web, docker stop web, docker restart web
Stop with a shorter timeout (default 10 s, then SIGKILL)docker stop -t 3 web
Kill immediately or send a signaldocker kill web, docker kill -s HUP web
Pause / unpause (freezes processes)docker pause web, docker unpause web
Remove a stopped containerdocker rm web
Remove a running containerdocker rm -f web
Renamedocker rename web web-old
Change restart policy or limits livedocker update --restart always --memory 1g web
Exit code of the last rundocker inspect -f '{{.State.ExitCode}}' web

Working with a running container

WhatCommand
Shell insidedocker exec -it web sh (or bash)
As root, even if the image runs as a userdocker exec -it -u 0 web sh
One commanddocker exec web cat /etc/os-release
Logsdocker logs web
Follow, last 100 lines, with timestampsdocker logs -f -n 100 -t web
Logs of the last hourdocker logs --since 1h web
Copy out / indocker cp web:/etc/nginx/nginx.conf ., docker cp ./site web:/usr/share/nginx/html
Processes insidedocker top web
Live CPU, memory, network, I/Odocker stats (--no-stream for one snapshot)
Changed files vs the imagedocker diff web
Published portsdocker port web
Events (start, die, oom, …)docker events --since 30m
Full configuration as JSONdocker inspect web (docker inspect)

docker diff lists files that were added (A), changed (C) or deleted (D) in the container’s writable layer. Useful to spot a dropped web shell or modified binaries.

Docker Compose

Run in the folder with compose.yaml (or docker-compose.yml), or point to it with -f.

WhatCommand
Start everything in the backgrounddocker compose up -d
Rebuild images, then startdocker compose up -d --build
Recreate even if nothing changeddocker compose up -d --force-recreate
Start one servicedocker compose up -d web
Stop and remove containers and networksdocker compose down
Same, plus named volumes (data is gone)docker compose down -v
Statusdocker compose ps
Logs of all or one servicedocker compose logs -f, docker compose logs -f web
Shell in a servicedocker compose exec web sh
One-off container of a servicedocker compose run --rm web python manage.py migrate
Pull newer images, then applydocker compose pull && docker compose up -d
Show the merged, resolved configdocker compose config
All Compose projects on the hostdocker compose ls
Use another file / project namedocker compose -f prod.yaml -p shop up -d

Registries and contexts

WhatCommand
Log indocker login registry.example.com
Log in from a scriptecho "$TOKEN" | docker login registry.example.com -u ci --password-stdin
Log outdocker logout registry.example.com
List contexts (local and remote daemons)docker context ls
Add a remote host over SSHdocker context create prod --docker "host=ssh://admin@prod01"
Switch / one command elsewheredocker context use prod, docker --context prod ps
Version and daemon infodocker version, docker info

Credentials from docker login land in ~/.docker/config.json, base64 encoded unless a credential helper is configured.

Gotchas

§ home
~/knubertus/home/
1,1 Top