docker

Docker Engine 24.0+

Quick reference for everyday Docker CLI commands: container lifecycle, image builds, logs / exec, networking, volumes, Compose, registry and housekeeping — with common options and practical examples.

46 commands

Container Lifecycle

docker run <image>

Create and start a new container from an image.

-d detached; -it interactive TTY; --name assign name; -p host:container publish port; -v mount volume; -e KEY=VAL env; --rm auto-remove on exit; --restart=always|unless-stopped|on-failure

docker run -d --name web -p 8080:80 -v $PWD:/usr/share/nginx/html:ro nginx:alpine
docker start|stop|restart <container>

Start, stop or restart an existing container (by name or id).

-t N seconds to wait before kill (default 10)

docker restart web && docker logs --tail 50 web
docker rm <container>

Remove one or more stopped containers.

-f force-remove running; -v also remove anonymous volumes

docker rm -f $(docker ps -aq)
docker ps

List containers (default: only running).

-a all; -q quiet (ids only); -s with size; --format go-template; --filter status|name=...

docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'
docker rename <c> <new>

Rename a container.

docker rename web-1 gateway-web
docker update <c>

Update a container's resource limits or restart policy at runtime.

--cpus N; --memory SIZE; --memory-swap SIZE; --restart policy

docker update --cpus 1.5 --memory 1g api
docker wait <c>

Block until the container stops, then print its exit code.

docker wait db && echo "db stopped"
docker top <c>

Show running processes inside a container (ps of the host PID namespace).

docker top web

Logs & Exec

docker logs <c>

Fetch logs of a container.

-f follow; --tail N; --since timestamp|duration; -t with timestamps; --details

docker logs -f --tail 200 -t api | grep ERROR
docker exec -it <c> <cmd>

Run a command inside a running container (commonly an interactive shell).

-i keep STDIN open; -t allocate a pseudo-TTY; -u user; -w workdir; -e KEY=VAL

docker exec -it -u postgres db psql -U postgres app_production
docker attach <c>

Attach local STDIN/STDOUT to a running container (same as `docker start -i`).

--sig-proxy proxy signals; --no-stdin

docker attach web && Ctrl-p Ctrl-q to detach
docker cp <c>:<src> <dest>

Copy files/folders between a container and the local filesystem.

-L follow symlink; -a archive mode

docker cp db:/var/lib/postgresql/data ./pgdata

Inspection

docker inspect <obj>

Return low-level info on Docker objects (container, image, volume, network).

-f go-template; --type container|image|network|volume; -s size

docker inspect -f '{{.NetworkSettings.IPAddress}}' web
docker stats

Live stream of CPU / memory / network / disk usage for running containers.

--no-stream one shot; -a all (incl stopped); --format; --no-trunc

docker stats --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}'
docker diff <c>

Inspect changes (added / changed / deleted) on a container's filesystem.

docker diff web | head -20

Image Operations

docker pull <image>[:tag]

Pull an image (or repository) from a registry.

-a all tagged images; --platform linux/amd64; -q quiet; --disable-content-trust

docker pull --platform linux/amd64 nginx:1.27-alpine
docker images

List images on the local daemon.

-a all (incl intermediate); -q ids only; --digests; --filter dangling=true

docker images --format '{{.Repository}}:{{.Tag}}\t{{.Size}}' | sort -k2 -h | tail -20
docker build <path>

Build an image from a Dockerfile.

-t name:tag; -f Dockerfile path; --no-cache; --build-arg KEY=VAL; --target stage; --progress=plain; --platform

docker build -t myapp:dev --build-arg NODE_ENV=development --progress=plain .
docker tag <src> <dest>

Create a tag that points to an existing image (alias / push target).

docker tag myapp:dev registry.example.com/team/myapp:1.2.0
docker push <image>

Upload an image (or repository) to a registry.

--disable-content-trust; -a all tagged variants; --quiet

docker push registry.example.com/team/myapp:1.2.0
docker rmi <image>

Remove one or more images.

-f force (untag also when children); --no-prune keep untagged parents

docker rmi -f $(docker images --filter 'dangling=true' -q)
docker image prune / docker save / docker load

Reclaim space (filter dangling/older than Xh); save/load image to/from tar.

-a all; --filter 'until=24h'; -o file.tar; -i file.tar

docker image prune -af --filter 'until=72h' && docker save -o nginx.tar nginx:alpine
docker history <image>

Show the layers / intermediate history of an image.

--no-trunc; --human; -q ids

docker history --no-trunc --human myapp:dev

Network

docker network ls

List networks.

--filter driver=bridge|overlay; -q ids; --format

docker network ls --filter driver=bridge --format '{{.Name}}'
docker network create <name>

Create a user-defined bridge network for service discovery & DNS.

--driver bridge|overlay|macvlan; --subnet CIDR; --gateway; --ipv6; --internal

docker network create --driver bridge --subnet 10.20.0.0/24 mynet
docker network connect|disconnect <net> <c>

Attach / detach a running container to a network.

--ip IPV4; --ip6 IPV6; --alias hostname alias; --link container

docker network connect mynet web
docker network rm / docker network prune

Remove one or more networks / clean up unused.

-f force; --filter 'until=24h'

docker network prune -f

Volume & Storage

docker volume ls

List volumes.

--filter dangling=true|driver|name; -q ids; --format

docker volume ls --filter dangling=true -q
docker volume create <name>

Create a named volume (managed by Docker, out of container's UFS).

--driver local; --opt type=nfs; --opt o=addr; --opt device=:/path

docker volume create --driver local --opt type=nfs --opt o=addr=10.0.0.1 --opt device=:/exports/data pgdata
docker volume rm / docker volume prune

Remove one or more volumes / clean up unused.

-f force; --filter 'label=...'

docker volume rm $(docker volume ls -q --filter dangling=true)
docker volume inspect <vol>

Show low-level details of a volume (mountpoint, driver, options).

docker volume inspect pgdata --format '{{.Mountpoint}}'

Docker Compose

docker compose up

Build (if needed), create and start services from compose.yaml.

-d detached; --build force build; --force-recreate; --pull always|missing|never; -f file

docker compose -f compose.prod.yaml up -d --pull always
docker compose down

Stop and remove containers, networks; default also removes anonymous volumes.

--volumes named vols too; --rmi all|local; --remove-orphans; -t N timeout

docker compose down --rmi local --remove-orphans
docker compose ps / logs

List Compose services / view logs.

ps -q ids only; logs -f follow; --tail N; --since

docker compose ps --format json | jq '.[] | {Name, State}' && docker compose logs -f --tail 100 api
docker compose exec / run

Run a command in a running / new service container.

exec --user; exec -e VAR=val; run --rm removes container after

docker compose exec db psql -U postgres app && docker compose run --rm api npm run migrate
docker compose build / pull / push

Build images / pull images from registry / push images to registry for a Compose project.

--no-cache; --pull; --quiet; --parallel

docker compose build --no-cache api worker
docker compose restart / stop / start / scale

Lifecycle shortcuts for services; scale adjusts the number of replicas.

restart <svc>; stop <svc>; start <svc>; scale api=4 worker=2

docker compose restart api && docker compose up -d --scale worker=4

Registry & Hub

docker login [server]

Authenticate to a registry. Use `docker login` for Docker Hub.

-u user; -p password; --password-stdin

echo "$REGISTRY_TOKEN" | docker login ghcr.io -u myuser --password-stdin
docker logout [server]

Remove credentials for a registry from config.

docker logout && docker info 2>/dev/null | head
docker search <term>

Search Docker Hub for images.

--filter stars=N; --limit N; --format

docker search --filter stars=100 --limit 5 postgres

System & Cleanup

docker system df

Show docker disk usage (images, containers, volumes, build cache).

-v verbose

docker system df -v | sort -k7 -h | tail
docker system prune

Remove all stopped containers, dangling images, unused networks.

-a all images; --volumes also volumes; --filter 'until=24h'; -f force

docker system prune -af --volumes --filter 'until=72h'
docker builder prune

Clean the build cache (free disk space from old layers).

-af; --filter 'until=72h'; --keep-storage SIZE

docker builder prune -af --filter 'until=72h'
docker context ls / use

Manage Docker contexts — for multi-host / multi-daemon (incl remote contexts over SSH).

ls --format json; use myctx

docker context create myhost --docker host=ssh://user@server && docker context use myhost && docker ps
docker info

Display system-wide information; useful when debugging.

--format

docker info -f '{{.DriverStatus}}'
docker port <c>

List port mappings (host ↔ container) for a container.

docker port web 80

Related command cheatsheets

About Docker

Docker is an open-source container engine originally released by Solomon Hykes at PyCon 2013 and now stewarded by Docker Inc. and the Cloud Native Computing Foundation; the current stable line is Docker Engine 24.0+ (the runtime component), with `dockerd` orchestrating containers built from OCI-compliant images. Containers package an application together with its dependencies (libraries, configuration, runtime) into an image layered on a read-only base; layers are content-addressed by SHA-256, cached across builds, and shared between running containers. Docker's CLI covers the full lifecycle — `docker build`, `docker run`, `docker ps`, `docker exec`, `docker logs`, `docker compose` for multi-container apps, `docker network` and `docker volume` for plumbing, `docker push` / `docker pull` for registries — and is the lingua franca of container operations even on Kubernetes clusters. Use Docker locally to mirror production, in CI to run reproducible jobs, and as the substrate for Compose stacks; reach for Kubernetes when you need automated scheduling, self-healing, or rolling deploys across many nodes. Docker daemon binds to a local socket by default — secure it with TLS, and never expose it to the public internet.

Cheatsheet version 1.0.0