Docker Cheat Sheet: CLI, Buildx and Compose Commands
Checked against Docker CLI, Buildx and Docker Compose Docker CLI 29.10.1, Compose 5.8.1, Buildx 0.39.1 on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for developers who run containers every day and want the flags they keep looking up in one place. It was checked against the Docker CLI 29.10.1, Compose 5.8.1 and Buildx 0.39.1 references and release notes on 28 November 2026. Every row is marked untested because we could not run Docker in the environment used to write it, so each section links the official page we read instead.
When to use what
Use plain docker run for a one-off container you will throw away: a quick database for a test, or a tool image you call once. As soon as you need two containers that talk to each other, or you catch yourself pasting the same long run line twice, move it into a compose.yaml. Our docker run to Compose converter turns an existing command into a service block, and the Compose generator gives you a starting file.
For builds, docker build already runs through Buildx and BuildKit, so you only need to type docker buildx build when you want builder-specific features such as several platforms at once or registry cache export. Put a .dockerignore next to the Dockerfile before your first build; sending node_modules or .git to the builder is the most common reason a build is slow. The .dockerignore generator and the Dockerfile linter catch most of that, and the multi-stage builds guide explains how to keep the final image small.
For cleanup, prefer the narrow commands (docker image prune, docker builder prune) over docker system prune -a. The narrow ones only touch what you name, while the big one also deletes every image no container is using, which means a slow re-pull the next morning. Leave volumes out of any prune you have not read twice.
docker pull postgres:17Download one tag of an image from the registry without starting it
docker image lsList local images with repository, tag, ID and size
docker tag myapp:dev registry.example.com/team/myapp:1.4Add a second name to an existing image so it can be pushed to another registry
docker push registry.example.com/team/myapp:1.4Upload a tagged image to the registry named in its tag
docker image rm myapp:devRemove a local tag, and the image layers if no other tag points at them
docker image history myapp:devShow each layer with the Dockerfile step that created it and its size
docker run -d --name web -p 8080:80 nginxStart nginx in the background and publish container port 80 on host port 8080
docker run --rm -it alpine shOpen a throwaway shell that deletes its container when you exit
docker run -e NODE_ENV=production --env-file .env myappPass one variable inline and the rest from a file
docker ps -aList running and stopped containers; drop -a to see only running ones
docker stop web && docker start webStop the container gracefully (SIGTERM, then SIGKILL after the timeout) and start it again with the same settings
docker rm -f webKill and remove a container in one step
docker inspect web --format '{{.State.Status}}'Read one field from the container's JSON metadata with a Go template
docker logs -f --since 10m webStream output from the last ten minutes and keep following new lines
docker logs -n 100 -t webShow only the last 100 lines with an RFC3339 timestamp on each
docker exec -it web shOpen an interactive shell inside a running container
docker exec -u root web idRun a single command in the container as a different user
docker cp web:/etc/nginx/nginx.conf ./nginx.confCopy a file out of a container to the host (swap the arguments to copy in)
docker stats --no-streamPrint one snapshot of CPU, memory and network use per running container
Named volumes survive docker rm and docker compose down. Bind mounts point at a host path, so the data is whatever is in that folder.
docker volume create pgdataCreate a named volume managed by Docker
docker run -v pgdata:/var/lib/postgresql/data postgres:17Mount a named volume at the database's data directory
docker run --mount type=bind,src="$(pwd)",dst=/app node:22Bind-mount the current folder with the explicit --mount syntax, which fails if the path is missing
docker volume lsList volumes with their driver and name
docker volume rm pgdataDelete a named volume and its data; fails while a container still uses it
docker network create appnetCreate a user-defined bridge network where containers resolve each other by name
docker run -d --network appnet --name db postgres:17Start a container on that network so others can reach it as host db
docker network connect appnet webAttach an already running container to another network
docker network inspect appnetPrint the network configuration, including attached containers, as JSON
docker build is an alias of docker buildx build, so these flags work with either.
docker build -t myapp:dev .Build the Dockerfile in the current folder and tag the result
docker build --target build-env -t myapp:build .Stop at a named stage of a multi-stage Dockerfile
docker build --build-arg NODE_VERSION=22 .Set a value for an ARG declared in the Dockerfile
docker buildx build --platform linux/amd64,linux/arm64 -t user/app:1.0 --push .Build for two CPU architectures and push the multi-platform image to the registry
docker buildx build --secret id=npmrc,src=$HOME/.npmrc .Expose a secret file to RUN --mount=type=secret steps without writing it into a layer
docker buildx build --cache-to=type=registry,ref=user/app:cache --cache-from=type=registry,ref=user/app:cache .Store and reuse build cache in a registry, handy on CI runners that start empty
docker build --no-cache --progress=plain .Rebuild every step and print full plain-text output for debugging
docker system prune removes stopped containers, unused networks, dangling images and unused build cache. It leaves volumes alone unless you add --volumes.
docker system dfShow how much disk images, containers, volumes and build cache use
docker image pruneDelete dangling (untagged) images only
docker builder pruneClear the BuildKit build cache, often the largest item in system df
docker system prune -a --filter until=24hAlso remove every unused image, but only things created more than a day ago
docker volume pruneDelete unused anonymous volumes; named volumes stay unless you add -a
Name the file compose.yaml and start it with services:; no version: line. Profiles are documented at https://docs.docker.com/compose/how-tos/profiles/.
docker compose up -d --buildRebuild images that have a build section, then start all services in the background
docker compose up --waitReturn only once services are running or report healthy, useful before tests in CI
docker compose up --watchStart services and rebuild or sync them when source files change
docker compose logs -f apiFollow the logs of one service
docker compose exec api shOpen a shell in the running container of a service
docker compose run --rm api npm testStart a fresh one-off container for a service, run a command, then remove it
docker compose --profile debug up -dAlso start services tagged with the debug profile; COMPOSE_PROFILES=debug does the same
docker compose downStop and remove the project's containers and networks, keeping named volumes
docker compose down -vAlso delete the project's named and anonymous volumes, wiping database data
docker compose configPrint the merged, interpolated file so you can check variables and overrides
Worked example: Run a web app and Postgres with an optional admin UI
Say you have an API and a database, and you sometimes want pgAdmin. Put the admin UI behind a profile so it only starts when you ask:
# compose.yaml
services:
api:
build: .
ports: ["3000:3000"]
environment:
DATABASE_URL: postgres://app:app@db:5432/app
depends_on: [db]
db:
image: postgres:17
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app
volumes: [pgdata:/var/lib/postgresql/data]
pgadmin:
image: dpage/pgadmin4
profiles: [debug]
ports: ["5050:80"]
volumes:
pgdata:
docker compose up -d --build # starts api and db only
docker compose --profile debug up -d # adds pgadmin
docker compose logs -f api
docker compose down # pgdata survives
The first up builds the API image and starts two containers, because pgadmin has a profile and no profile is active. The API reaches the database at host db, since Compose puts every service on a shared project network and registers each service name in its DNS. The second command adds pgadmin on port 5050 without touching the running services. After down, the containers and network are gone but the pgdata volume is still there, so the next up finds your tables. Run down -v only when you want a clean database. To move this stack to a cluster later, try the Compose to Kubernetes converter; for a longer walkthrough, see Docker Compose for local development.
Common mistakes
Keeping
version: "3.8"at the top of a Compose file.Delete the line. Compose ignores it and warns, and the file is always checked against the current spec.
Running
docker compose down -vto restart cleanly and losing the local database.Use plain
down(orrestart) for a restart;-vdeletes volumes, so reserve it for a deliberate reset.Using
localhostinside one container to reach another service.Use the service or container name on a shared network, such as
db:5432; inside a container, localhost is the container itself.Building with no
.dockerignore, so the whole repo,.gitandnode_modulesgo to the builder.Add a
.dockerignorethat excludes those folders; builds get faster and secrets stay out of the context.Passing tokens with
--build-argand finding them later indocker image history.Use
--secret id=...,src=...withRUN --mount=type=secret, which keeps the value out of the image layers.Running
docker system prune -a --volumes -fon a shared or long-lived machine to free space.Check
docker system dffirst and prune the biggest item on its own, usuallydocker builder prune.
Related cheat sheets
Sources
- https://docs.docker.com/reference/cli/docker/
- https://docs.docker.com/reference/cli/docker/compose/
- https://docs.docker.com/reference/cli/docker/compose/up/
- https://docs.docker.com/reference/cli/docker/compose/down/
- https://docs.docker.com/compose/how-tos/profiles/
- https://docs.docker.com/reference/compose-file/version-and-name/
- https://docs.docker.com/reference/cli/docker/buildx/build/
- https://docs.docker.com/reference/cli/docker/system/prune/
- https://docs.docker.com/reference/cli/docker/volume/prune/
- https://docs.docker.com/reference/cli/docker/container/logs/
- https://docs.docker.com/reference/cli/docker/container/exec/
- https://docs.docker.com/reference/cli/docker/container/cp/
- https://docs.docker.com/reference/cli/docker/container/stats/
- https://docs.docker.com/reference/cli/docker/network/create/
- https://docs.docker.com/reference/cli/docker/image/history/
- https://github.com/docker/compose/releases
- https://github.com/docker/buildx/releases
- https://github.com/docker/cli/tags
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.