Docker Commands Cheat Sheet
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:alpinedocker 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 webdocker rm <container>Remove one or more stopped containers.
-f force-remove running; -v also remove anonymous volumes
docker rm -f $(docker ps -aq)docker psList 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-webdocker 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 apidocker 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 webLogs & 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 ERRORdocker 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_productiondocker 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 detachdocker 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 ./pgdataInspection
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}}' webdocker statsLive 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 -20Image 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-alpinedocker imagesList 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 -20docker 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.0docker 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.0docker 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 loadReclaim 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:alpinedocker history <image>Show the layers / intermediate history of an image.
--no-trunc; --human; -q ids
docker history --no-trunc --human myapp:devNetwork
docker network lsList 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 mynetdocker 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 webdocker network rm / docker network pruneRemove one or more networks / clean up unused.
-f force; --filter 'until=24h'
docker network prune -fVolume & Storage
docker volume lsList volumes.
--filter dangling=true|driver|name; -q ids; --format
docker volume ls --filter dangling=true -qdocker 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 pgdatadocker volume rm / docker volume pruneRemove 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 upBuild (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 alwaysdocker compose downStop 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-orphansdocker compose ps / logsList 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 apidocker compose exec / runRun 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 migratedocker compose build / pull / pushBuild images / pull images from registry / push images to registry for a Compose project.
--no-cache; --pull; --quiet; --parallel
docker compose build --no-cache api workerdocker compose restart / stop / start / scaleLifecycle 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=4Registry & 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-stdindocker logout [server]Remove credentials for a registry from config.
docker logout && docker info 2>/dev/null | headdocker search <term>Search Docker Hub for images.
--filter stars=N; --limit N; --format
docker search --filter stars=100 --limit 5 postgresSystem & Cleanup
docker system dfShow docker disk usage (images, containers, volumes, build cache).
-v verbose
docker system df -v | sort -k7 -h | taildocker system pruneRemove 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 pruneClean 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 / useManage 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 psdocker infoDisplay 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 80Cheatsheet version 1.0.0
Covers Docker Engine 24.0+