Commands · Reference

🐳Docker Commands

Running, building, inspecting and cleaning up — plus the flags that keep a laptop from filling with dead layers.

Running containers

11 commands

run is create plus start. --rm on anything interactive, or you accumulate stopped containers forever.

CommandWhat it does Typical use
docker run --rm -itInteractive, removed on exitdocker run --rm -it ubuntu bash
docker run -d --nameDetached, with a name you can refer todocker run -d --name web nginx
docker run -p 8080:80Publish host:container. Add 127.0.0.1: to avoid exposing itdocker run -p 127.0.0.1:8080:80 nginx
docker run -v $PWD:/app:roBind-mount, read-onlydocker run -v $PWD:/app:ro node npm test
docker run -e / --env-fileEnvironment variablesdocker run --env-file .env api
docker run --memory=512m --cpus=1.5Resource limits — the cgroup settingsdocker run --memory=512m --cpus=1.5 api
docker run --initReal init as PID 1 — reaps zombies, forwards signalsdocker run --init api
docker run -u $(id -u):$(id -g)Run as your UID so bind-mounted files aren't root-owneddocker run -u $(id -u):$(id -g) -v $PWD:/w node
docker run --read-only --tmpfs /tmpImmutable root filesystem with writable /tmpdocker run --read-only --tmpfs /tmp api
docker run --restart=unless-stoppedRestart on failure and on boot, but honour a manual stopdocker run -d --restart=unless-stopped api
docker start / stop / restartLifecycle of an existing containerdocker stop -t 30 web

Images & building

10 commands

Layers are cached in order, so the least-changing steps go first. Copying source before installing dependencies invalidates the cache on every edit.

CommandWhat it does Typical use
docker build -t name:tag .Build and tagdocker build -t api:v2 .
docker build --targetStop at a named stage of a multi-stage builddocker build --target test -t api:test .
docker build --no-cacheIgnore the layer cachedocker build --no-cache -t api:v2 .
docker build --build-argPass a build-time variabledocker build --build-arg VERSION=2.1 -t api .
docker build --platform linux/amd64Cross-build — routine on Apple silicondocker build --platform linux/amd64 -t api .
docker buildx build --push --platform a,bMulti-arch image, built and pushed in one stepdocker buildx build --platform linux/amd64,linux/arm64 -t repo/api:v2 --push .
docker images --filter dangling=trueUntagged layers left by rebuildsdocker images --filter dangling=true
docker historyEvery layer and what it cost — finds the fat onedocker history api:v2
docker tag / docker pushRetag for a registry and uploaddocker tag api:v2 repo/api:v2 && docker push repo/api:v2
docker save / loadMove an image without a registrydocker save api:v2 | gzip > api.tgz

Inspecting & logs

12 commands

inspect returns the full JSON; the -f Go template pulls out one value without piping through jq.

CommandWhat it does Typical use
docker ps -aAll containers, including stopped onesdocker ps -a
docker ps --filter status=exitedFilter by state, name, label or ancestordocker ps --filter 'status=exited'
docker ps --format 'table {{.Names}}\t{{.Status}}'Only the columns you wantdocker ps --format 'table {{.Names}}\t{{.Status}}'
docker logs -f --tail 100Follow the last 100 linesdocker logs -f --tail 100 web
docker logs --since 10m -tTime-bounded with timestampsdocker logs --since 10m -t web
docker inspect -f '{{.State.Pid}}'One field out of the JSONdocker inspect -f '{{.State.Pid}}' web
docker inspect -f '{{.State.OOMKilled}}'Was it killed for memory?docker inspect -f '{{.State.OOMKilled}}' api
docker stats --no-streamCPU, memory and I/O per container, oncedocker stats --no-stream
docker exec -itShell in a running containerdocker exec -it web sh
docker topProcesses inside a container, from the host's viewdocker top web
docker diffWhat changed in the container's filesystem since it starteddocker diff web
docker cpCopy files in or out — works on stopped containers toodocker cp web:/etc/nginx/nginx.conf .

Networking & volumes

9 commands

The default bridge has no DNS between containers. A user-defined network does, which is why compose creates one.

CommandWhat it does Typical use
docker network lsNetworks that existdocker network ls
docker network createA user-defined bridge — gives you container-name DNSdocker network create appnet
docker network connectAttach a running container to another networkdocker network connect appnet web
docker network inspectSubnet, gateway and which containers are attacheddocker network inspect appnet
docker run --network=hostShare the host's network namespace. Linux onlydocker run --network=host api
docker volume create / lsNamed volumes — managed, unlike bind mountsdocker volume create pgdata
docker volume inspectWhere a volume actually lives on the hostdocker volume inspect pgdata
docker run -v pgdata:/var/lib/postgresql/dataMount a named volumedocker run -v pgdata:/var/lib/postgresql/data postgres
docker run --mount type=bind,src=,dst=,roExplicit form; fails loudly if the source is missingdocker run --mount type=bind,src=$PWD,dst=/app,ro node

Compose

9 commands

Compose v2 is docker compose — a subcommand, not the old docker-compose binary.

CommandWhat it does Typical use
docker compose up -dStart the stack detacheddocker compose up -d
docker compose up --build --force-recreateRebuild images and recreate containersdocker compose up -d --build
docker compose down -vStop and remove, including named volumes. Destroys datadocker compose down -v
docker compose logs -f svcFollow one servicedocker compose logs -f api
docker compose psStatus of the stack's containersdocker compose ps
docker compose execShell into a service by its compose namedocker compose exec db psql -U app
docker compose configRender the final merged config — resolves every override and vardocker compose config
docker compose -f a.yml -f b.ymlLayer an override file over a basedocker compose -f compose.yml -f compose.prod.yml up -d
docker compose run --rm svc cmdOne-off command in a service's environmentdocker compose run --rm api pytest

Cleanup

8 commands

Docker never reclaims anything on its own. On a build host this is the difference between a working disk and a 3am page.

CommandWhat it does Typical use
docker system dfWhat is using the space, by categorydocker system df -v
docker system pruneStopped containers, unused networks, dangling images, build cachedocker system prune
docker system prune -a --volumesEverything not currently in use. Read that twicedocker system prune -a --volumes
docker image prune -a --filter 'until=168h'Images unused for a week — a safe cron jobdocker image prune -a --filter 'until=168h' -f
docker builder pruneBuild cache only, leaving images alonedocker builder prune --keep-storage 10GB
docker volume pruneVolumes no container references. Check before runningdocker volume ls -f dangling=true
docker container pruneStopped containers onlydocker container prune -f
docker rm -f $(docker ps -aq)Remove every container, running or notdocker rm -f $(docker ps -aq)
← PreviousKubernetes Commands