Skip to main content
Docker CLI The commands you actually type every day. Following the official Docker CLI reference (as of 2026-06). For the concept basics, see Core concepts.

Container lifecycle

CommandPurposeExample
docker runCreate and start a new container from an imagedocker run -d --name web nginx:alpine
docker startStart a stopped containerdocker start web
docker stopGraceful stop via SIGTERM (10s grace by default)docker stop web
docker restartStop then start againdocker restart web
docker pause / unpauseFreeze / unfreeze all processesdocker pause web
docker rmRemove a stopped container (-f forces running)docker rm web
docker run -d --name web nginx:alpine   # create and start detached
docker stop -t 0 web                     # stop without waiting for SIGTERM
docker rm -f web                         # force-remove a running container

Key docker run flags

Flags decide how a container runs; these are the most used:
FlagPurposeExample
-dRun detached (background), prints only the container IDdocker run -d nginx:alpine
-itInteractive shell (-i keeps STDIN, -t allocates a TTY)docker run -it ubuntu bash
--nameName the container (unique per host)docker run --name web nginx
--rmAuto-remove on exit (and its anonymous volumes)docker run --rm alpine echo hi
-pPublish a port, HOST:CONTAINERdocker run -p 8080:80 nginx
--networkConnect to a networkdocker run --network mynet app
-v / --mountMount a volume or bind mountdocker run -v data:/app/data app
-e / --env-fileSet env vars / load from a filedocker run -e TZ=Asia/Taipei app
--restartRestart policy (see below)docker run --restart=always nginx
--gpusPass GPUs (needs the NVIDIA Container Toolkit)docker run --gpus all app nvidia-smi
-w / -uWorking dir / run identitydocker run -w /app -u 1000:1000 app
--memory / --cpusResource capsdocker run -m 512m --cpus 1.5 app
The four --restart policies:
ValueBehavior
no (default)No auto-restart
on-failure[:N]Restart only on a non-zero exit code, up to N retries
alwaysAlways restart; also comes back after the daemon restarts
unless-stoppedSame as always, but if you docker stop it manually, a daemon restart will not bring it back
The direction of -p is HOST:CONTAINER: left is your machine, right is inside the container. -p 8080:80 means “host 8080 into container 80”; reversed, you cannot connect. To bind only to localhost: -p 127.0.0.1:8080:80.

Inspect and debug

CommandPurpose
docker psList running containers (-a includes stopped, -q IDs only)
docker logsView logs (-f follow, --tail N, --since)
docker execRun a command in a running container (-it for a shell)
docker inspectFull JSON of a container / image (IP, mounts, env)
docker statsLive CPU / memory / network / block I/O
docker topProcesses running inside a container
docker cpCopy files between container and host
docker ps -a -q --filter status=exited        # IDs of all stopped containers
docker logs -f --tail 100 web                  # follow the last 100 lines
docker exec -it -u root web bash               # interactive shell as root
docker inspect --format='{{.NetworkSettings.IPAddress}}' web   # get the IP
docker stats --no-stream                       # one snapshot, no streaming
docker cp web:/app/logs/app.log ./app.log      # copy a file out
A “run it, check it, look inside, tear it down” flow looks like this:

Image commands

CommandPurpose
docker imagesList local images (-a includes intermediate layers)
docker pullPull an image from a registry
docker buildBuild an image from a Dockerfile (-t to name, -f for the file)
docker tagAdd a new tag
docker rmiRemove an image (-f forces)
docker historyEach layer’s build instruction and size
docker save / loadExport to tar / import (offline transfer)
docker pull nginx:1.27-alpine
docker build -f docker/Dockerfile.prod -t myapp:prod .
docker tag myapp:1.0 user/myapp:1.0
docker save myapp:1.0 | gzip > myapp.tar.gz     # compressed export
docker load < myapp.tar.gz                       # import
Full docker build usage is in Building a Dockerfile from scratch.

Cleanup commands

Images, containers, and volumes pile up. These are destructive, so be clear what each removes:
docker system df                      # disk usage and reclaimable space first
docker system df -v                   # per-object detail

docker container prune                # all stopped containers
docker image prune                    # only dangling images
docker image prune -a                 # every image not referenced by a container (incl. tagged)
docker volume prune                   # only anonymous volumes
docker network prune                  # unused custom networks

docker system prune                   # stopped containers + unused networks + dangling images + build cache
docker system prune -a                # images expand to "every unreferenced image"
docker system prune -a --volumes      # plus anonymous volumes, the most destructive
Cleanup is irreversible. Run docker system df first:
  • docker image prune -a / system prune -a removes all images not used by a container, including versions you may still want, forcing a re-pull.
  • --volumes clears only anonymous volumes; to clear named volumes (where database data often lives) you need docker volume prune -a, and the data cannot be recovered.
  • The built-in bridge / host / none networks are never pruned.

Multiple daemons and diagnostics

docker context ls                                    # list reachable Docker daemons
docker context create remote --docker "host=ssh://user@10.0.0.5"
docker --context remote ps                           # run one command against a remote daemon
docker version                                       # client and daemon versions, API version
docker info                                          # system info for the whole install

Deprecated items (as of 2026-06)

Old usageStatusReplacement
docker-compose (v1, Python)EOL, no longer maintaineddocker compose (v2, built-in CLI plugin)
docker run --linkDeprecateduser-defined network + DNS name resolution
DOCKER_BUILDKIT=0Can disable BuildKit but not recommendedUse BuildKit (default in Docker 23+)

Next

Reference: docs.docker.com/reference/cli/docker