Cheatography
https://cheatography.com
Everyday Docker Engine 29 and Docker Compose v5 commands: containers, images, builds, volumes, networks, cleanup and system info, plus the pitfalls hit while testing them. Checked on Docker Engine 29.9, Compose v5.6 and Buildx v0.38.
Containers
docker run -d --name web -p 8080:80 nginx:1.30 |
Run in the background and map host port 8080 to container port 80 |
docker run -it --rm ubuntu:26.04 bash |
Interactive shell; container is removed when it exits |
docker ps |
List running containers |
docker ps -a |
List all containers, including stopped ones |
docker stop web / docker start web |
Stop / start a container |
docker restart web |
Restart a container |
docker rm web |
Remove a stopped container |
docker rm -f web |
Force-remove a running container |
docker logs -f web |
Follow a container's logs |
docker logs --tail 100 web |
Show only the last 100 log lines |
docker exec -it web sh |
Open a shell inside a running container |
docker cp web:/path/file . |
Copy a file from the container to the host |
docker inspect web |
Full container details as JSON |
docker stats |
Live CPU / memory / network usage |
docker top web |
Processes running inside the container |
docker port web |
Show port mappings |
docker update --restart unless-stopped web |
Change the restart policy of an existing container |
docker run --init ... |
Small init as PID 1, so stop signals are forwarded (no 10 s wait on docker stop) |
Names like web are examples; any container name or ID works.
Images
docker images |
List local images |
docker images --tree |
Show the platforms inside multi-platform images |
docker pull nginx:1.30 |
Pull an image |
docker tag myapp user/myapp:1.0 |
Add a new name or tag |
docker push user/myapp:1.0 |
Push to a registry |
docker rmi nginx:1.30 |
Remove an image |
docker history nginx:1.30 |
Show image layers |
docker buildx imagetools inspect nginx:1.30-alpine |
Platforms of a remote image, without pulling it |
docker save -o img.tar myapp |
Export an image to a file |
docker load -i img.tar |
Load an image from a file |
docker login / docker logout |
Log in to / out of a registry |
|
|
Builds
docker build -t myapp . |
Build with the Dockerfile in the current directory |
docker build -f Dockerfile.prod -t myapp . |
Use a different Dockerfile |
docker build --no-cache -t myapp . |
Build without the cache |
docker build --pull -t myapp . |
Pull the base image again |
docker build --target dev -t myapp:dev . |
Build only up to one stage of a multi-stage file |
docker build --build-arg VERSION=1.2 . |
Pass a build argument (never for passwords: it stays in the image history) |
docker build --secret id=token,env=TOKEN . |
Pass a secret without leaving it in the image |
docker build --progress plain . |
Show the full build output |
docker buildx build --platform linux/amd64,linux/arm64 -t user/app --push . |
Build for two platforms and push |
Volumes
docker volume create data |
Create a named volume |
docker volume ls |
List volumes |
docker volume inspect data |
Volume details (mount point on the host) |
docker volume rm data |
Remove a volume |
docker run -v data:/app/data myapp |
Mount a named volume |
docker run -v "$(pwd):/app" myapp |
Bind-mount the current directory |
Networks
docker network create appnet |
Create a user-defined network (containers reach each other by name) |
docker network ls |
List networks |
docker network inspect appnet |
Show the containers on a network |
docker run --network appnet --name db postgres:18 |
Start a container on a network |
docker network connect appnet web |
Attach a running container to a network |
docker network rm appnet |
Remove a network |
Common run options
-d |
Run in the background |
-it |
Interactive mode + terminal |
-p host:container |
Port mapping |
-v source:container-path |
Mount a volume or directory |
-e KEY=VALUE |
Environment variable |
--name |
Container name |
--rm |
Remove automatically when it exits |
--network |
Network to join |
--restart |
Restart policy (no, on-failure, always, unless-stopped) |
|
|
Docker Compose (v5)
docker compose up -d |
Start all services in the background |
docker compose up -d --build |
Rebuild images, then start |
docker compose up -d --wait |
Return only when all services are healthy |
docker compose up -d app |
Recreate / rebuild only one service |
docker compose down |
Stop and remove containers and networks |
docker compose down -v |
... and the volumes too (deletes data) |
docker compose ps |
Service status |
docker compose logs -f app |
Follow one service's logs |
docker compose exec app sh |
Shell in a running service container |
docker compose run --rm app npm test |
Run a one-off command |
docker compose pull |
Pull the latest images |
docker compose restart app |
Restart a service (does NOT apply a changed compose file: use up -d) |
docker compose watch |
Sync or rebuild when files change (development) |
docker compose --profile tools up -d |
Also start services in a profile |
docker compose config |
Print the final merged configuration |
Cleanup and disk usage
docker system df |
Disk usage by images, containers, volumes, cache |
docker container prune |
Remove stopped containers |
docker image prune -a |
Remove all unused images |
docker builder prune |
Clear the build cache |
docker builder prune --filter until=168h |
Clear only cache unused for 7+ days |
docker volume prune |
Remove unused anonymous volumes |
docker volume prune -a |
Remove ALL unused volumes, including named database volumes |
docker system prune |
General cleanup (volumes not included) |
docker system prune --volumes |
Also anonymous volumes (named ones are kept since Docker 23) |
System information
docker version |
Client and server versions |
docker info |
Engine configuration and storage details |
docker events |
Live event stream |
docker context ls |
Docker endpoints you can switch between |
Pitfalls found while testing
docker stop takes 10 seconds |
The program is PID 1 and ignores SIGTERM: handle it, or run with --init |
postgres:18 exits right after start |
Version 18 mounts /var/lib/postgresql, not /var/lib/postgresql/data |
Database healthy before it is ready |
Add -h 127.0.0.1 to pg_isready / mysqladmin ping |
Redis healthcheck passes with a wrong password |
redis-cli ping exits 0 on auth errors; add | grep -q PONG |
Changing MYSQL_ROOT_PASSWORD has no effect |
Used only on the first initialisation of the data volume |
Edited a bind-mounted file, container sees the old one |
Editors replace the file; mount the directory instead, or restart |
--env-file values include quotes |
docker run --env-file keeps quotes; Compose env_file strips them |
Secret deleted with rm is still in the image |
Every layer keeps it; ARG and ENV are visible too: use --secret |
exec format error |
CPU architecture mismatch: use --platform or build multi-platform |
Container named web resolves to 127.0.0.53 |
.web is a real top-level domain; pick another name |
Source and longer explanations
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets