~/cheat-sheets / docker
Docker cheat sheet
Field-tested Docker and Compose commands for running servers – searchable and copyable in one click. With warnings on commands that delete data.
Download as PDF (A4, free to share) ↓
38 commands
Images
List local images
docker imagesPull an image with a pinned tag
docker pull nginx:1.27Build an image from the Dockerfile in this folder
docker build -t myapp:1.0 .Retag an image for a registry
docker tag myapp:1.0 registry.example.com/myapp:1.0Delete an image
⚠ Careful: changes or deletes datadocker rmi myapp:1.0
Running & managing containers
List running containers
docker psList all containers including stopped ones
docker ps -aStart a detached container with a port mapping
docker run -d --name web -p 8080:80 nginxThrowaway container with an interactive shell
docker run --rm -it alpine shRestart the container after reboot or crash
docker run -d --restart unless-stopped nginxStop a container gracefully (SIGTERM, then SIGKILL)
docker stop webForce-stop and delete a container
⚠ Careful: changes or deletes datadocker rm -f web
Logs, exec & diagnostics
Last 100 log lines, then follow live
docker logs -f --tail 100 webOpen a shell in a running container
docker exec -it web shRun a single command in the container
docker exec web nginx -tCPU and RAM usage of all containers (snapshot)
docker stats --no-stream
Volumes & networks
Mount a named volume into a container
docker run -v data:/var/lib/mysql mariadbBack up a volume to a tar archive
docker run --rm -v data:/d -v $(pwd):/b alpine tar czf /b/data.tgz -C /d .Delete a volume – the data is gone
⚠ Careful: changes or deletes datadocker volume rm dataList networks
docker network lsCreate a user-defined bridge (DNS by container name)
docker network create appnet
Docker Compose
Start the stack in the background
docker compose up -dRebuild images and start the stack
docker compose up -d --buildFollow the logs of one service live
docker compose logs -f webPull new images, recreate changed services
docker compose pull && docker compose up -dShell inside a service container
docker compose exec web shStop the stack and remove containers and network
docker compose downLike down, but also deletes the volumes
⚠ Careful: changes or deletes datadocker compose down -v
Cleaning up with prune
Disk usage by images, containers, volumes
docker system dfDelete untagged (dangling) images
⚠ Careful: changes or deletes datadocker image pruneDelete all unused volumes – data loss possible
⚠ Careful: changes or deletes datadocker volume pruneDelete stopped containers, unused networks and dangling images
⚠ Careful: changes or deletes datadocker system pruneRadical: everything unused incl. images and volumes
⚠ Careful: changes or deletes datadocker system prune -a --volumes
Resource limits
Limit memory to 512 MB
docker run -d --memory 512m nginxLimit to 1.5 CPU cores
docker run -d --cpus 1.5 nginxChange the limits of a running container
docker update --memory 1g --memory-swap 1g webSet log rotation for a container
docker run -d --log-opt max-size=10m --log-opt max-file=3 nginxFind OOM kills from the last hour
docker events --since 1h --filter event=oom
No match.
New articles by e-mail
One e-mail per new article. No tracking, unsubscribe any time. As a thank-you after confirming: my “Server operations & patching” checklist (PDF).