Command-first reference for the Docker CLI: the exact command and flags for containers, images,
builds, volumes, networks, Compose, registries and cleanup, starting with a lookup table. Same versions as Docker (Docker Engine 29.x, Compose
through docker compose), which explains the concepts, Compose files, networking and security in
depth; writing images is in Dockerfile.
Quick lookup
I want to…
Command
run a container in the background
docker run -d --name web -p 8080:80 nginx
get a throwaway shell in an image
docker run --rm -it alpine sh
list running / all containers
docker ps / docker ps -a
open a shell in a running container
docker exec -it web sh
read or follow logs
docker logs --tail 100 -f web
stop / remove a container
docker stop web / docker rm -f web
see why a container exited
docker inspect -f '{{.State.ExitCode}} {{.State.OOMKilled}}' web
see published ports
docker port web
copy a file out
docker cp web:/etc/nginx/nginx.conf .
watch CPU and memory
docker stats
change the restart policy
docker update --restart unless-stopped web
build an image
docker build -t app:dev .
see full build output
docker build --progress=plain .
build without cache
docker build --no-cache -t app:dev .
list / delete images
docker images / docker rmi app:old
tag and push
docker tag app:dev ghcr.io/acme/app:1.0 then docker push ghcr.io/acme/app:1.0
move an image to a host without a registry
docker save app:1.0 | ssh host docker load
list volumes / networks
docker volume ls / docker network ls
start / stop a Compose project
docker compose up -d / docker compose down
rebuild one Compose service
docker compose up -d --build --no-deps api
follow every Compose service's logs
docker compose logs -f
run a one-off Compose command
docker compose run --rm api bun run migrate
print the merged Compose file
docker compose config
log in to a registry
docker login ghcr.io
run commands on a remote host
docker --context web1 ps or DOCKER_HOST=ssh://user@host docker ps
see which daemon I'm talking to
docker context ls
see what uses disk
docker system df -v
clean up unused things
docker system prune
watch daemon events
docker events
check versions
docker version
Command anatomy
docker [global flags] <object> <command> [flags] [arguments]docker --context prod container ls -a --filter status=exited
Commands are grouped by object (management commands): container, image, volume,
network, builder / buildx, compose, context, system. The common ones also have older
top-level shortcuts, and both forms do the same thing.
files only, as one flat layer; no history or config; no volumes
Out
docker save -o app.tar app:1.4.2
docker export -o fs.tar web
In
docker load -i app.tar (gzip, bzip2, xz also accepted)
docker import fs.tar app:flat
Use for
moving images to an offline host
inspecting or flattening a filesystem
docker import --change 'CMD ["/app"]' fs.tar app:flat restores a command after a flatten.
save keeps an image as it is; export flattens what one container sees into plain files.
Build and buildx
docker build is an alias of docker buildx build, which uses BuildKit. The context is the
directory (or URL) whose files the Dockerfile can COPY; .dockerignore trims it.
Flag
Does
docker build .
build ./Dockerfile with the current directory as context
-t app:dev
name the result; repeat for more tags
-f docker/Dockerfile.prod
another Dockerfile
--target build
stop at a named stage
--build-arg VERSION=1.4.2
set an ARG
--secret id=npmrc,src=$HOME/.npmrc
file secret for RUN --mount=type=secret,id=npmrc
--secret id=gh,env=GH_TOKEN
secret from an environment variable
--ssh default
forward your SSH agent to RUN --mount=type=ssh
--no-cache
rerun every step
--no-cache-filter deps
rerun only the named stages
--pull
check for newer base images
--progress=plain
full output of every step (the default collapses finished steps)
--platform linux/amd64,linux/arm64
target platforms
--load / --push
put the result in local images / push it to the registry
docker buildx bake # the "default" groupdocker buildx bake api --print # resolved config as JSONdocker buildx bake --set '*.platform=linux/arm64'docker buildx bake --push
Volumes and networks
Command
Does
docker volume create pgdata
create a named volume
docker volume ls
list; -f dangling=true shows unused ones
docker volume inspect pgdata
driver, mount point, labels
docker volume rm pgdata
delete (refused while a container uses it)
docker volume prune
delete unused anonymous volumes
docker volume prune -a
delete unused named volumes too
docker run --rm -v pgdata:/v alpine ls -la /v
look inside a volume
# back up a volume to ./pgdata.tgz (stop writers first)docker run --rm -v pgdata:/v:ro -v "$PWD":/b alpine \ tar czf /b/pgdata.tgz -C /v .# restore into a volumedocker run --rm -v pgdata:/v -v "$PWD":/b alpine \ tar xzf /b/pgdata.tgz -C /v
Command
Does
docker network ls
list; bridge, host and none are built in
docker network create app
user-defined bridge: containers find each other by name
docker network create --subnet 10.20.0.0/24 app
choose the subnet
docker network create --internal backend
no route to the outside world
docker network inspect app
settings and attached containers
docker network connect app web
attach a running container; --alias db adds a DNS name
docker network disconnect app web
detach
docker network rm app
delete
docker network prune
delete networks no container uses
Compose CLI
docker compose reads compose.yaml (plus compose.override.yaml) in the current directory and
acts on the whole project, or on the services you name. File syntax is in
Docker.
Start and stop
Command
Does
docker compose up -d
create and start everything, detached
docker compose up -d --build
rebuild images first
docker compose up -d api
one service plus its depends_on
docker compose up -d --no-deps api
one service, leaving its dependencies alone
docker compose up -d --force-recreate
new containers even if nothing changed
docker compose up -d --remove-orphans
also remove containers of services deleted from the file
docker compose up -d -V
fresh anonymous volumes instead of reusing old ones
docker compose up --wait
return once services are running or healthy (CI)
docker compose up --watch
start and sync file changes (develop.watch)
docker compose up --exit-code-from tests
stop everything when tests exits, return its exit code
docker compose up -d --scale worker=3
run replicas
docker compose stop / start
stop / start containers without removing them
docker compose restart api
restart in place; does not apply edits to compose.yaml (use up -d)
docker compose down
stop and remove containers and networks
docker compose down -v
also the project's named and anonymous volumes: deletes data
docker compose down --rmi local
also images built by this project
Inspect
Command
Does
docker compose ps / ps -a
this project's containers; --services, -q, --format json
docker compose logs
every service, interleaved
docker compose logs -f --tail 50 api worker
follow some services
docker compose logs --since 10m -t
a time window, with timestamps
docker compose logs --no-log-prefix api
without the service-name prefix
docker compose top / stats / events
processes / resource use / event stream
docker compose images
images the containers use
docker compose port api 3000
host address mapped to port 3000
docker compose config
the merged, interpolated model; -q only validates
docker compose config --services
service names (--profiles, --images, --volumes)
docker compose ls
every Compose project on this daemon
docker compose version
Compose version
exec vs run
docker compose exec api sh
docker compose run --rm api sh
Container
the one already running
a new one built from the service definition
Service must be up
yes
no; starts its depends_on unless --no-deps
Ports
those up already published
not published unless --service-ports (or -p)
TTY
on by default; -T turns it off (CI, pipes)
same
Extras
-u root, -w /app, -e K=v, --index 2 for a replica
-e K=v, -v, --entrypoint sh, --build
Use for
shells, inspecting live state
migrations, tests, one-off scripts
Files, projects and profiles
Flag or variable
Does
-f compose.yaml -f compose.prod.yaml
merge files, later ones win; turns off the automatic compose.override.yaml
COMPOSE_FILE=compose.yaml:compose.prod.yaml
the same from the environment (; on Windows)
-p shop, COMPOSE_PROJECT_NAME=shop
project name (default: the directory name); prefixes containers, networks and volumes, e.g. shop-api-1
--project-directory ./deploy
base directory for relative paths
--env-file .env.prod
variables for ${…} interpolation
--profile debug, COMPOSE_PROFILES=debug,tools
enable services that have profiles:
--dry-run
show what a command would do
Build and pull
Command
Does
docker compose build
build every service that has build:
docker compose build --no-cache --pull api
one service, from scratch, with fresh base images
docker compose pull
pull images; --ignore-buildable skips services with build:
docker compose push
push the built images
docker compose watch
file sync and rebuilds, without the app logs
docker compose cp api:/app/out.txt .
copy files
Registries and login
Command
Does
docker login
Docker Hub; uses a device code you confirm in the browser
docker login -u alice
Docker Hub with a username and password or access token
docker login ghcr.io
another registry: the host name only
… | docker login REGISTRY -u USER --password-stdin
non-interactive (CI); keeps the secret out of history
Credentials live in ~/.docker/config.json. Without a credential helper they are stored there
base64-encoded, readable by anyone who can read the file; a helper keeps them in a keychain
instead. Each value below names a program called docker-credential-NAME on your PATH.
credsStore / credHelpers value
Stores or fetches credentials with
osxkeychain
macOS Keychain (the default on macOS)
wincred
Windows Credential Manager
pass
pass, GPG-encrypted files (Linux)
secretservice
the desktop keyring: GNOME Keyring, KWallet (Linux)
desktop
Docker Desktop, backed by the OS keychain
ecr-login
AWS ECR tokens on demand (amazon-ecr-credential-helper)
credsStore applies to every registry; credHelpers overrides it per registry. Docker Hub
rate-limits anonymous and free-plan pulls, so log in on build machines and CI runners.
Contexts and remote daemons
A context is a saved daemon endpoint plus settings. Docker Desktop, OrbStack and Colima each
register one (for example desktop-linux, orbstack, colima).
no context needed; overrides the current context when set
docker -H ssh://deploy@web1 ps
the same as a flag
docker context inspect web1
endpoint details
docker context rm web1
delete
docker context export web1 / import
move a context to another machine
The SSH user must be allowed to use the Docker socket on the remote host (member of the docker
group, or a rootless daemon). Add ControlMaster auto, ControlPath ~/.ssh/control-%C and
ControlPersist yes to that host in ~/.ssh/config so commands reuse one connection; see
SSH client config.
System and cleanup
Command
Shows
docker version
client and server versions; -f '{{.Server.Version}}' for one
Every prune asks first unless given --force, and takes --filter until=24h or
--filter label=… (volume prune: label only). Named volumes go only with docker volume prune -a,
docker volume rm or docker compose down -v.
Format and filter
Go templates
--format takes a Go template: fields are written {{.Field}}, table adds column headers
and \t separates columns, and json prints a value as JSON. On list commands,
--format json prints one JSON object per line, ready for jq. Template functions include
json, join, split, lower, upper, title, pad, truncate and println, plus Go's
range, index and if.