Docker Cheat Sheet
The commands I use for the handful of services that run as plain containers next to the Kubernetes cluster (media stack on the NAS, the odd test box).
Containers
| Task | Command |
|---|---|
| Run detached, named, restart on boot | docker run -d --name plex --restart unless-stopped -p 32400:32400 -v /data/media:/media plexinc/pms-docker |
| List running / all | docker ps / docker ps -a |
| Logs, follow, last 200 lines | docker logs -f --tail 200 plex |
| Shell inside | docker exec -it plex /bin/sh |
| Stop / start / restart | docker stop plex · docker start plex · docker restart plex |
| Remove (stopped) | docker rm plex |
| Resource usage | docker stats |
| Inspect IP and mounts | docker inspect plex | jq '.[0].NetworkSettings.Networks, .[0].Mounts' |
Images
| Task | Command |
|---|---|
| Pull / list | docker pull nginx:1.27 / docker images |
| Build from the current directory | docker build -t myapp:1.0 . |
| Tag and push | docker tag myapp:1.0 registry.lab/myapp:1.0 · docker push registry.lab/myapp:1.0 |
| Remove unused images | docker image prune -a |
| History / size | docker history myapp:1.0 |
Volumes and networks
| Task | Command |
|---|---|
| Named volume | docker volume create pgdata · -v pgdata:/var/lib/postgresql/data |
| Bind mount | -v /srv/app/config:/config:ro |
| List / inspect volumes | docker volume ls / docker volume inspect pgdata |
| User-defined bridge (containers resolve each other by name) | docker network create lab · --network lab |
| Host networking (no port mapping) | --network host |
| Inspect a network | docker network inspect lab |
Compose
A minimal compose.yaml with the pieces that matter:
services:
app:
image: ghcr.io/example/app:1.4
container_name: app
restart: unless-stopped
environment:
TZ: Europe/Budapest
PUID: "1000"
PGID: "1000"
ports:
- "8080:8080"
volumes:
- ./config:/config
- /data/media:/media:ro
networks: [lab]
healthcheck:
test: ["CMD", "wget", "-qO-", "http://localhost:8080/health"]
interval: 30s
timeout: 5s
retries: 3
networks:
lab:
external: true| Task | Command |
|---|---|
| Up (detached) / down | docker compose up -d / docker compose down |
| Pull new images and recreate | docker compose pull && docker compose up -d |
| Logs for one service | docker compose logs -f app |
| Recreate one service | docker compose up -d --force-recreate app |
| Validate / view merged config | docker compose config |
| Run a one-off command | docker compose run --rm app sh |
Clean-up
docker system df # what is using the disk
docker container prune # stopped containers
docker image prune -a # images not used by any container
docker volume prune # volumes not attached to any container (careful)
docker system prune -a --volumes # everything unusedWarning
docker volume prune deletes data that is not currently attached to a container. Stop a stack with docker compose down and its volumes are still attached only if you kept the service definitions; check docker volume ls first.
Handy one-liners
# IP of every running container
docker inspect -f '{{.Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -q)
# Containers not set to restart automatically
docker inspect -f '{{.Name}} {{.HostConfig.RestartPolicy.Name}}' $(docker ps -aq) | grep -v unless-stopped
# Copy a file out of a container
docker cp plex:/config/Preferences.xml ./Preferences.xmlLast updated on