Skip to content
Docker Cheat Sheet

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

TaskCommand
Run detached, named, restart on bootdocker run -d --name plex --restart unless-stopped -p 32400:32400 -v /data/media:/media plexinc/pms-docker
List running / alldocker ps / docker ps -a
Logs, follow, last 200 linesdocker logs -f --tail 200 plex
Shell insidedocker exec -it plex /bin/sh
Stop / start / restartdocker stop plex · docker start plex · docker restart plex
Remove (stopped)docker rm plex
Resource usagedocker stats
Inspect IP and mountsdocker inspect plex | jq '.[0].NetworkSettings.Networks, .[0].Mounts'

Images

TaskCommand
Pull / listdocker pull nginx:1.27 / docker images
Build from the current directorydocker build -t myapp:1.0 .
Tag and pushdocker tag myapp:1.0 registry.lab/myapp:1.0 · docker push registry.lab/myapp:1.0
Remove unused imagesdocker image prune -a
History / sizedocker history myapp:1.0

Volumes and networks

TaskCommand
Named volumedocker volume create pgdata · -v pgdata:/var/lib/postgresql/data
Bind mount-v /srv/app/config:/config:ro
List / inspect volumesdocker 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 networkdocker 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
TaskCommand
Up (detached) / downdocker compose up -d / docker compose down
Pull new images and recreatedocker compose pull && docker compose up -d
Logs for one servicedocker compose logs -f app
Recreate one servicedocker compose up -d --force-recreate app
Validate / view merged configdocker compose config
Run a one-off commanddocker 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 unused

Warning

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.xml
Last updated on