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
| What | Command |
|---|---|
| Pull an image | docker pull nginx:1.27 |
| Pull by digest (immutable) | docker pull nginx@sha256:<digest> |
| List images | docker images |
Build from ./Dockerfile | docker build -t myapp:1.0 . |
| Build with another Dockerfile, no cache | docker build -f Dockerfile.prod --no-cache -t myapp:prod . |
| Build argument | docker build --build-arg VERSION=1.2 -t myapp . |
| Build for another platform | docker buildx build --platform linux/arm64 -t myapp . |
| Tag for a registry | docker tag myapp:1.0 registry.example.com/team/myapp:1.0 |
| Push | docker push registry.example.com/team/myapp:1.0 |
| Layers and the commands that made them | docker history --no-trunc myapp:1.0 |
| Remove an image | docker rmi myapp:1.0 |
| Save to / load from a tar file | docker 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
| Flag | Does |
|---|---|
-d | Run in the background (detached) |
-it | Interactive with a terminal, for shells |
--rm | Delete the container when it exits |
--name web | Fixed name instead of a random one |
-p 127.0.0.1:8080:80 | Publish container port 80 on host port 8080, localhost only (Networking) |
-v /srv/data:/data | Bind mount a host path |
-v appdata:/data | Named volume |
-v /etc/app.conf:/etc/app.conf:ro | Mount read-only |
-e KEY=value / --env-file .env | Environment variables |
--network appnet | Attach to a network |
--restart unless-stopped | Restart policy: no, on-failure[:N], always, unless-stopped |
-w /app | Working directory |
--entrypoint sh | Override the image’s entrypoint |
--user 1000:1000 | Run as this UID:GID instead of the image default |
--read-only | Read-only root filesystem (add --tmpfs /tmp for scratch space) |
--cap-drop ALL --cap-add NET_BIND_SERVICE | Drop all capabilities, add back only what’s needed |
--security-opt no-new-privileges | Block privilege escalation via setuid binaries |
--memory 512m --cpus 1.5 | Resource limits |
--pids-limit 200 | Limit processes (fork bomb protection) |
--log-opt max-size=10m | Log size limit for this container (Cleanup) |
--privileged | Full 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
| What | Command |
|---|---|
| Running containers | docker ps |
| All containers, also stopped | docker ps -a |
| Only IDs (for scripts) | docker ps -aq |
| Start / stop / restart | docker 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 signal | docker kill web, docker kill -s HUP web |
| Pause / unpause (freezes processes) | docker pause web, docker unpause web |
| Remove a stopped container | docker rm web |
| Remove a running container | docker rm -f web |
| Rename | docker rename web web-old |
| Change restart policy or limits live | docker update --restart always --memory 1g web |
| Exit code of the last run | docker inspect -f '{{.State.ExitCode}}' web |
Working with a running container
| What | Command |
|---|---|
| Shell inside | docker exec -it web sh (or bash) |
| As root, even if the image runs as a user | docker exec -it -u 0 web sh |
| One command | docker exec web cat /etc/os-release |
| Logs | docker logs web |
| Follow, last 100 lines, with timestamps | docker logs -f -n 100 -t web |
| Logs of the last hour | docker logs --since 1h web |
| Copy out / in | docker cp web:/etc/nginx/nginx.conf ., docker cp ./site web:/usr/share/nginx/html |
| Processes inside | docker top web |
| Live CPU, memory, network, I/O | docker stats (--no-stream for one snapshot) |
| Changed files vs the image | docker diff web |
| Published ports | docker port web |
| Events (start, die, oom, …) | docker events --since 30m |
| Full configuration as JSON | docker 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.
| What | Command |
|---|---|
| Start everything in the background | docker compose up -d |
| Rebuild images, then start | docker compose up -d --build |
| Recreate even if nothing changed | docker compose up -d --force-recreate |
| Start one service | docker compose up -d web |
| Stop and remove containers and networks | docker compose down |
| Same, plus named volumes (data is gone) | docker compose down -v |
| Status | docker compose ps |
| Logs of all or one service | docker compose logs -f, docker compose logs -f web |
| Shell in a service | docker compose exec web sh |
| One-off container of a service | docker compose run --rm web python manage.py migrate |
| Pull newer images, then apply | docker compose pull && docker compose up -d |
| Show the merged, resolved config | docker compose config |
| All Compose projects on the host | docker compose ls |
| Use another file / project name | docker compose -f prod.yaml -p shop up -d |
Registries and contexts
| What | Command |
|---|---|
| Log in | docker login registry.example.com |
| Log in from a script | echo "$TOKEN" | docker login registry.example.com -u ci --password-stdin |
| Log out | docker logout registry.example.com |
| List contexts (local and remote daemons) | docker context ls |
| Add a remote host over SSH | docker context create prod --docker "host=ssh://admin@prod01" |
| Switch / one command elsewhere | docker context use prod, docker --context prod ps |
| Version and daemon info | docker version, docker info |
Credentials from docker login land in ~/.docker/config.json, base64 encoded unless a credential helper is configured.
Gotchas
- Membership in the
dockergroup is root on the host.docker run -v /:/host -it alpine chroot /hostgives a root shell. The same applies to any container with/var/run/docker.sockmounted. latestis just a tag, not the newest version. Pin a version or a digest for anything that matters.docker stopsends SIGTERM to PID 1. If the entrypoint is a shell script that doesn’texecthe app, the app never sees the signal and is killed after the timeout.--initadds a tiny init process that forwards signals.- Data in the container’s writable layer is lost with
docker rm. Anything that must survive goes into a volume or bind mount. - Bind mounts hide what the image had at that path. Mounting an empty host folder over
/appgives an empty/app.