Skip to content

Menu

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:17

Download one tag of an image from the registry without starting it

From the docs, not run by us
docker image ls

List local images with repository, tag, ID and size

From the docs, not run by us
docker tag myapp:dev registry.example.com/team/myapp:1.4

Add a second name to an existing image so it can be pushed to another registry

From the docs, not run by us
docker push registry.example.com/team/myapp:1.4

Upload a tagged image to the registry named in its tag

From the docs, not run by us
docker image rm myapp:dev

Remove a local tag, and the image layers if no other tag points at them

From the docs, not run by us
docker image history myapp:dev

Show each layer with the Dockerfile step that created it and its size

From the docs, not run by us
docker run -d --name web -p 8080:80 nginx

Start nginx in the background and publish container port 80 on host port 8080

Try it: docker-run-to-composeFrom the docs, not run by us
docker run --rm -it alpine sh

Open a throwaway shell that deletes its container when you exit

From the docs, not run by us
docker run -e NODE_ENV=production --env-file .env myapp

Pass one variable inline and the rest from a file

From the docs, not run by us
docker ps -a

List running and stopped containers; drop -a to see only running ones

From the docs, not run by us
docker stop web && docker start web

Stop the container gracefully (SIGTERM, then SIGKILL after the timeout) and start it again with the same settings

From the docs, not run by us
docker rm -f web

Kill and remove a container in one step

From the docs, not run by us
docker inspect web --format '{{.State.Status}}'

Read one field from the container's JSON metadata with a Go template

From the docs, not run by us
docker logs -f --since 10m web

Stream output from the last ten minutes and keep following new lines

From the docs, not run by us
docker logs -n 100 -t web

Show only the last 100 lines with an RFC3339 timestamp on each

From the docs, not run by us
docker exec -it web sh

Open an interactive shell inside a running container

From the docs, not run by us
docker exec -u root web id

Run a single command in the container as a different user

From the docs, not run by us
docker cp web:/etc/nginx/nginx.conf ./nginx.conf

Copy a file out of a container to the host (swap the arguments to copy in)

From the docs, not run by us
docker stats --no-stream

Print one snapshot of CPU, memory and network use per running container

From the docs, not run by us

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 pgdata

Create a named volume managed by Docker

From the docs, not run by us
docker run -v pgdata:/var/lib/postgresql/data postgres:17

Mount a named volume at the database's data directory

From the docs, not run by us
docker run --mount type=bind,src="$(pwd)",dst=/app node:22

Bind-mount the current folder with the explicit --mount syntax, which fails if the path is missing

From the docs, not run by us
docker volume ls

List volumes with their driver and name

From the docs, not run by us
docker volume rm pgdata

Delete a named volume and its data; fails while a container still uses it

From the docs, not run by us
docker network create appnet

Create a user-defined bridge network where containers resolve each other by name

From the docs, not run by us
docker run -d --network appnet --name db postgres:17

Start a container on that network so others can reach it as host db

From the docs, not run by us
docker network connect appnet web

Attach an already running container to another network

From the docs, not run by us
docker network inspect appnet

Print the network configuration, including attached containers, as JSON

From the docs, not run by us

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

Try it: Dockerfile LinterFrom the docs, not run by us
docker build --target build-env -t myapp:build .

Stop at a named stage of a multi-stage Dockerfile

From the docs, not run by us
docker build --build-arg NODE_VERSION=22 .

Set a value for an ARG declared in the Dockerfile

From the docs, not run by us
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

From the docs, not run by us
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

From the docs, not run by us
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

From the docs, not run by us
docker build --no-cache --progress=plain .

Rebuild every step and print full plain-text output for debugging

From the docs, not run by us

docker system prune removes stopped containers, unused networks, dangling images and unused build cache. It leaves volumes alone unless you add --volumes.

docker system df

Show how much disk images, containers, volumes and build cache use

From the docs, not run by us
docker image prune

Delete dangling (untagged) images only

From the docs, not run by us
docker builder prune

Clear the BuildKit build cache, often the largest item in system df

From the docs, not run by us
docker system prune -a --filter until=24h

Also remove every unused image, but only things created more than a day ago

From the docs, not run by us
docker volume prune

Delete unused anonymous volumes; named volumes stay unless you add -a

From the docs, not run by us

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 --build

Rebuild images that have a build section, then start all services in the background

Try it: Docker Compose GeneratorFrom the docs, not run by us
docker compose up --wait

Return only once services are running or report healthy, useful before tests in CI

From the docs, not run by us
docker compose up --watch

Start services and rebuild or sync them when source files change

From the docs, not run by us
docker compose logs -f api

Follow the logs of one service

From the docs, not run by us
docker compose exec api sh

Open a shell in the running container of a service

From the docs, not run by us
docker compose run --rm api npm test

Start a fresh one-off container for a service, run a command, then remove it

From the docs, not run by us
docker compose --profile debug up -d

Also start services tagged with the debug profile; COMPOSE_PROFILES=debug does the same

From the docs, not run by us
docker compose down

Stop and remove the project's containers and networks, keeping named volumes

From the docs, not run by us
docker compose down -v

Also delete the project's named and anonymous volumes, wiping database data

From the docs, not run by us
docker compose config

Print the merged, interpolated file so you can check variables and overrides

From the docs, not run by us

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 -v to restart cleanly and losing the local database.

    Use plain down (or restart) for a restart; -v deletes volumes, so reserve it for a deliberate reset.

  • Using localhost inside 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, .git and node_modules go to the builder.

    Add a .dockerignore that excludes those folders; builds get faster and secrets stay out of the context.

  • Passing tokens with --build-arg and finding them later in docker image history.

    Use --secret id=...,src=... with RUN --mount=type=secret, which keeps the value out of the image layers.

  • Running docker system prune -a --volumes -f on a shared or long-lived machine to free space.

    Check docker system df first and prune the biggest item on its own, usually docker builder prune.

Sources

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.

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe
Reviews and commentsShow

Comments

Sign in to leave a comment.

Loading comments…