man docker

Docker Cheatsheet

The docker commands I use every day: pull and build images, run and debug containers, keep data in volumes, wire up networks, drive Compose stacks and clean up disk space. Every command has a real example you can copy.

Showing 50 of 50 commands

Images

11 commands

Pull, build, tag, ship and clean up the images your containers start from.

docker pull <image> beginner Download an image (and its layers) from a registry; Docker Hub is the default.

Example

bash
docker pull nginx:1.27-alpine
1.27-alpine: Pulling from library/nginx
Digest: sha256:2140dad235c130ac861018a4e13a6bc8aea3a35f3a40e20c1b060d51a7efd250
Status: Downloaded newer image for nginx:1.27-alpine
docker.io/library/nginx:1.27-alpine

Tip: Always pin a tag. Without one Docker pulls :latest, which changes under you between deploys.

docker build -t <name>:<tag> <path> beginner Build an image from the Dockerfile in <path> and give it a name and tag.

Example

bash
docker build -t blog-api:1.4.0 .
# Different Dockerfile name and a build argument
docker build -f Dockerfile.prod --build-arg NODE_ENV=production -t blog-api:1.4.0 .
[+] Building 18.4s (12/12) FINISHED
 => [internal] load build definition from Dockerfile
 => [3/6] RUN npm ci --omit=dev
 => exporting to image
 => => naming to docker.io/library/blog-api:1.4.0

Tip: Copy dependency manifests and install before COPY . . so code changes don't invalidate the slow install layer.

docker images beginner List the images stored locally with their tag, ID, age and size.

Example

bash
docker images
# Only one repository
docker images nginx
REPOSITORY   TAG           IMAGE ID       CREATED        SIZE
blog-api     1.4.0         5f1c2e9a8b31   2 minutes ago  182MB
nginx        1.27-alpine   a3a1f4e5c7d2   3 weeks ago    47.9MB
postgres     16            8e4b2a6d1f90   5 weeks ago    432MB

Tip: Same as docker image ls. Add -q to print only IDs, handy for piping into other commands.

docker rmi <image> beginner Delete one or more local images by name:tag or ID.

Example

bash
docker rmi blog-api:1.3.0
Untagged: blog-api:1.3.0
Deleted: sha256:9c0d7e2b4a61f3c8e5d1b7a9f2c4e6d8b0a1c3e5f7d9b1a3c5e7f9d1b3a5c7e9

Tip: Fails if a container (even a stopped one) still uses the image; remove the container first or add -f.

docker push <image> beginner Upload a local image to the registry named in its tag.

Example

bash
docker push ghcr.io/jeffxl/blog-api:1.4.0
The push refers to repository [ghcr.io/jeffxl/blog-api]
4f2a8c1d9e3b: Pushed
7b5e3d0c2a14: Layer already exists
1.4.0: digest: sha256:6d2e8f0a4c1b3e5d7f9a1c3e5b7d9f1a3c5e7b9d1f3a5c7e9b1d3f5a7c9e1b3d size: 1574

Tip: Run docker login for that registry first. Only layers the registry doesn't have yet are uploaded.

docker tag <source> <target> beginner Give an existing image another name; no data is copied, both names point to the same ID.

Example

bash
docker tag blog-api:1.4.0 ghcr.io/jeffxl/blog-api:1.4.0
docker tag blog-api:1.4.0 ghcr.io/jeffxl/blog-api:latest

Tip: The registry host in the tag decides where docker push sends the image.

docker image prune beginner Remove dangling images: untagged layers left behind by rebuilds.

Example

bash
docker image prune
# Also remove every image no container uses, older than a week
docker image prune -a --filter "until=168h"
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 1.27GB

Tip: Add -f in scripts and cron jobs to skip the confirmation prompt.

docker inspect <image> intermediate Print the full JSON metadata of an image: env, entrypoint, exposed ports, labels and layers.

Example

bash
docker inspect nginx:1.27-alpine
# Pull out one field with a Go template
docker inspect --format '{{.Config.ExposedPorts}}' nginx:1.27-alpine
map[80/tcp:{}]

Tip: --format saves you from scrolling through hundreds of lines of JSON; pipe to jq for anything nested.

docker history <image> intermediate Show the layers of an image, the instruction that made each one and its size.

Example

bash
docker history blog-api:1.4.0
IMAGE          CREATED         CREATED BY                                      SIZE
5f1c2e9a8b31   5 minutes ago   CMD ["node" "server.js"]                        0B
<missing>      5 minutes ago   COPY . . # buildkit                             1.2MB
<missing>      5 minutes ago   RUN npm ci --omit=dev # buildkit                 58.3MB
<missing>      3 weeks ago     /bin/sh -c #(nop)  CMD ["node"]                 0B

Tip: The fastest way to find which Dockerfile line makes an image fat. Add --no-trunc for full commands.

docker save -o <file>.tar <image> intermediate Export one or more images, with all layers and tags, into a tar archive.

Example

bash
docker save -o blog-api-1.4.0.tar blog-api:1.4.0
# Compressed on the fly
docker save blog-api:1.4.0 | gzip > blog-api-1.4.0.tar.gz

Tip: Useful for air-gapped servers or moving an image without a registry: scp the tar and docker load it.

docker load -i <file>.tar intermediate Import images from a tar archive created by docker save.

Example

bash
docker load -i blog-api-1.4.0.tar
# gzip archives load directly
docker load -i blog-api-1.4.0.tar.gz
Loaded image: blog-api:1.4.0

Tip: Not the same as docker import, which takes a flat filesystem export and loses history and metadata.

Containers

11 commands

Start, stop, enter, debug and remove running workloads.

docker run <image> beginner Create a container from an image and start it.

Example

bash
docker run -d --name web -p 8080:80 --restart unless-stopped nginx:1.27-alpine
# Throwaway interactive shell, removed on exit
docker run --rm -it alpine:3.20 sh
e3b7a1c94f2d8b6a0c5e1f3d7b9a2c4e6f8d0b1a3c5e7f9d2b4a6c8e0f1d3b5a

Tip: -d runs in the background, -p host:container publishes a port, --rm deletes the container when it exits.

docker ps beginner List running containers with their image, command, status and published ports.

Example

bash
docker ps
# Include stopped containers
docker ps -a
# Just names and status
docker ps --format "table {{.Names}}\t{{.Status}}"
CONTAINER ID   IMAGE               COMMAND                  CREATED         STATUS         PORTS                  NAMES
e3b7a1c94f2d   nginx:1.27-alpine   "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   0.0.0.0:8080->80/tcp   web

Tip: A container missing from docker ps but present in docker ps -a has exited; check its logs.

docker stop <container> beginner Stop a running container gracefully: SIGTERM first, SIGKILL after a timeout.

Example

bash
docker stop web
# Give a slow app 30 seconds to shut down
docker stop -t 30 web
web

Tip: If stop always takes the full 10 s, your process ignores SIGTERM; run it with exec form CMD or add --init.

docker start <container> beginner Start one or more stopped containers with their original settings.

Example

bash
docker start web
# Attach to the output while it starts
docker start -a web
web

Tip: start keeps the container's filesystem and config; to change ports or env you need a new docker run.

docker rm <container> beginner Delete stopped containers and their writable layer.

Example

bash
docker rm web
# Stop and delete in one go
docker rm -f web
# Delete every exited container
docker rm $(docker ps -aq --filter status=exited)
web

Tip: Data in named volumes survives docker rm; add -v only when you also want anonymous volumes removed.

docker exec -it <container> <cmd> beginner Run a command inside a running container, interactively with -it.

Example

bash
docker exec -it web sh
# One-off command, no TTY
docker exec web nginx -t
# As root in a container that runs as another user
docker exec -u 0 -it blog-api sh
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Tip: Alpine and distroless images have no bash; try sh, or use a debug sidecar when there is no shell at all.

docker logs <container> beginner Show what a container wrote to stdout and stderr.

Example

bash
docker logs web
# Follow live, starting from the last 100 lines, with timestamps
docker logs -f --tail 100 -t web
# Only the last 15 minutes
docker logs --since 15m blog-api
2026-10-29T17:02:11.482Z 172.17.0.1 - - "GET / HTTP/1.1" 200 615 "-" "curl/8.10.1"

Tip: Works on stopped containers too, which makes it the first stop when something exits right after start.

docker inspect <container> intermediate Print a container's full JSON state: IP, mounts, env, restart count, exit code and more.

Example

bash
docker inspect --format '{{.State.Status}} exit={{.State.ExitCode}} restarts={{.RestartCount}}' blog-api
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web
exited exit=137 restarts=4
172.17.0.2

Tip: Exit code 137 means SIGKILL, usually the OOM killer; check .State.OOMKilled to confirm.

docker stats intermediate Stream live CPU, memory, network and disk I/O for running containers.

Example

bash
docker stats
# One snapshot instead of a live view
docker stats --no-stream web blog-api
CONTAINER ID   NAME       CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O        PIDS
e3b7a1c94f2d   web        0.02%     6.1MiB / 7.66GiB      0.08%     18.4kB / 92.1kB   0B / 4.1kB       9
4c9d2e7f1a3b   blog-api   1.35%     148.3MiB / 512MiB     28.96%    2.1MB / 3.4MB     12.3MB / 0B      23

Tip: The LIMIT column shows the --memory you set; host RAM there means the container has no memory limit.

docker cp <src> <dest> intermediate Copy files or folders between a container and the host, in either direction.

Example

bash
# Container to host
docker cp web:/etc/nginx/nginx.conf ./nginx.conf
# Host to container
docker cp ./default.conf web:/etc/nginx/conf.d/default.conf
Successfully copied 2.56kB to /home/jeff/nginx.conf

Tip: Works on stopped containers as well, so you can rescue files from one that won't start.

docker commit <container> <image> advanced Save a container's current filesystem as a new image.

Example

bash
docker commit -m "debug tools installed" web nginx-debug:snapshot
sha256:b81f3c5d7e9a1b3c5d7f9e1a3b5c7d9f1e3a5b7c9d1f3e5a7b9c1d3f5e7a9b1c

Tip: Fine for capturing a broken container to investigate; for anything you ship, write a Dockerfile so the build is repeatable.

Volumes

6 commands

Keep data alive across container restarts and rebuilds.

docker volume create <name> beginner Create a named volume managed by Docker.

Example

bash
docker volume create pgdata
pgdata

Tip: You rarely need this: docker run -v pgdata:/path creates the volume on first use.

docker volume ls beginner List the volumes on this host.

Example

bash
docker volume ls
# Volumes no container references
docker volume ls -f dangling=true
DRIVER    VOLUME NAME
local     pgdata
local     wordpress_wordpress
local     wordpress_dbdata

Tip: Compose prefixes volume names with the project name, which is why you see wordpress_wordpress.

docker volume rm <name> beginner Delete one or more volumes and all the data in them.

Example

bash
docker volume rm pgdata
pgdata

Tip: There is no undo. Back the volume up first (see the docker run -v example) if the data matters.

docker volume inspect <name> intermediate Show a volume's driver, labels and where it lives on the host.

Example

bash
docker volume inspect pgdata
[
    {
        "CreatedAt": "2026-10-29T14:20:31Z",
        "Driver": "local",
        "Mountpoint": "/var/lib/docker/volumes/pgdata/_data",
        "Name": "pgdata",
        "Scope": "local"
    }
]

Tip: The Mountpoint is readable with sudo on Linux; on Docker Desktop it lives inside the VM.

docker volume prune beginner Remove local volumes that no container uses.

Example

bash
docker volume prune
# Include named volumes too, not just anonymous ones
docker volume prune -a
WARNING! This will remove anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
3f9a1c7e5b2d8f4a6c0e1b3d5f7a9c2e4b6d8f0a1c3e5b7d9f2a4c6e8b0d1f3a

Total reclaimed space: 412MB

Tip: A stopped container still counts as a user, so its volumes are kept; -a is the one that can hurt.

docker run -v <vol>:<path> <image> beginner Mount a named volume (or a host folder) into a container at <path>.

Example

bash
docker run -d --name db -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=secret postgres:16
# Bind-mount a host folder read-only
docker run -d -p 8080:80 -v "$PWD/site":/usr/share/nginx/html:ro nginx:1.27-alpine
# Back up a volume to a tar file in the current folder
docker run --rm -v pgdata:/data -v "$PWD":/backup alpine tar czf /backup/pgdata.tgz -C /data .

Tip: A name before the colon is a volume, a path (./ or /) is a bind mount. Add :ro whenever the container only reads.

Networks

7 commands

Let containers find and talk to each other by name.

docker network create <name> beginner Create a user-defined bridge network.

Example

bash
docker network create backend
docker run -d --name db --network backend -e POSTGRES_PASSWORD=secret postgres:16
docker run -d --name api --network backend -e DB_HOST=db blog-api:1.4.0
8d2f4a6c1e3b5d7f9a0c2e4b6d8f1a3c5e7b9d0f2a4c6e8b1d3f5a7c9e0b2d4f

Tip: On a user-defined network containers resolve each other by name (db, api); the default bridge has no DNS.

docker network ls beginner List networks with their driver and scope.

Example

bash
docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
4a1c7e9f2b3d   backend   bridge    local
9e2b5d8a1f4c   bridge    bridge    local
1f7c3a9e5b2d   host      host      local
6b4d0f2a8c1e   none      null      local
docker network connect <net> <container> intermediate Attach a running container to another network without restarting it.

Example

bash
docker network connect backend web
# With an extra DNS name on that network
docker network connect --alias proxy backend web

Tip: A container can sit on several networks, e.g. a reverse proxy on both frontend and backend.

docker network disconnect <net> <container> intermediate Detach a container from a network; it loses that interface immediately.

Example

bash
docker network disconnect backend web
docker network inspect <name> intermediate Show a network's subnet, gateway and the containers attached to it with their IPs.

Example

bash
docker network inspect backend
docker network inspect --format '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{println}}{{end}}' backend
db 172.20.0.2/16
api 172.20.0.3/16

Tip: When two containers can't reach each other, check here first that both are actually on the same network.

docker network rm <name> beginner Delete one or more networks.

Example

bash
docker network rm backend
backend

Tip: Fails while containers are still attached; disconnect or remove them first.

docker network prune beginner Remove every network that no container uses.

Example

bash
docker network prune -f
Deleted Networks:
old-project_default

Compose

7 commands

Run multi-container apps described in a compose.yaml file.

docker compose up beginner Create and start every service in the Compose file, plus its networks and volumes.

Example

bash
docker compose up -d
# Rebuild images first, then start
docker compose up -d --build
# Only one service and what it depends on
docker compose up -d api
[+] Running 4/4
 ✔ Network blog_default   Created
 ✔ Volume "blog_dbdata"   Created
 ✔ Container blog-db-1    Started
 ✔ Container blog-api-1   Started

Tip: Running up again after editing the file recreates only the services whose config changed.

docker compose down beginner Stop and remove the project's containers and networks.

Example

bash
docker compose down
# Also delete named volumes: this wipes the database
docker compose down -v
[+] Running 3/3
 ✔ Container blog-api-1  Removed
 ✔ Container blog-db-1   Removed
 ✔ Network blog_default  Removed

Tip: Volumes are kept unless you pass -v, so plain down is safe for your data.

docker compose ps beginner List the containers of the current Compose project and their state.

Example

bash
docker compose ps
NAME         IMAGE            COMMAND                  SERVICE   CREATED          STATUS                    PORTS
blog-api-1   blog-api:1.4.0   "node server.js"         api       3 minutes ago    Up 3 minutes              0.0.0.0:3000->3000/tcp
blog-db-1    postgres:16      "docker-entrypoint.s…"   db        3 minutes ago    Up 3 minutes (healthy)    5432/tcp
docker compose logs beginner Show the merged, color-coded output of all services, or just some.

Example

bash
docker compose logs -f --tail 50
docker compose logs -f api
api-1  | Server listening on :3000
db-1   | LOG:  database system is ready to accept connections
docker compose exec <service> <cmd> beginner Run a command in a service's running container, using the service name, not the container name.

Example

bash
docker compose exec db psql -U postgres
docker compose exec api sh

Tip: Use docker compose run --rm <service> <cmd> instead when you need a fresh one-off container, e.g. migrations.

docker compose build beginner Build or rebuild the images of services that have a build: section, without starting anything.

Example

bash
docker compose build
# Ignore the cache and refresh base images
docker compose build --no-cache --pull api

Tip: Follow with docker compose up -d to swap the running containers onto the new images.

docker compose config intermediate Validate the Compose file and print the final config with variables and overrides resolved.

Example

bash
docker compose config
# Just check it's valid, print nothing
docker compose config -q
# See what an override file changes
docker compose -f compose.yaml -f compose.prod.yaml config

Tip: The quickest way to see which .env value actually ended up in a service.

Registry

3 commands

Sign in to registries and find images to start from.

docker login <registry> beginner Authenticate to a registry; with no host it logs in to Docker Hub.

Example

bash
docker login
# Non-interactive, token from an environment variable (CI)
echo "$GHCR_TOKEN" | docker login ghcr.io -u jeffxl --password-stdin
Login Succeeded

Tip: Use --password-stdin in scripts; a password passed with -p ends up in shell history and process lists.

docker logout <registry> beginner Remove the stored credentials for a registry.

Example

bash
docker logout ghcr.io
Removing login credentials for ghcr.io

System

5 commands

Check the daemon, see what eats disk space and clean it up.

docker system df beginner Show how much disk images, containers, volumes and build cache use, and how much is reclaimable.

Example

bash
docker system df
# Per image, container and volume
docker system df -v
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          14        5         6.82GB    4.91GB (71%)
Containers      7         5         38.4MB    1.2MB (3%)
Local Volumes   6         4         2.37GB    412MB (17%)
Build Cache     62        0         3.14GB    3.14GB

Tip: Run this before pruning so you know where the space actually goes.

docker system prune beginner Remove stopped containers, unused networks, dangling images and build cache in one go.

Example

bash
docker system prune
# Also every unused image and unused volumes: the aggressive version
docker system prune -a --volumes
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - unused build cache

Are you sure you want to continue? [y/N] y
Total reclaimed space: 4.62GB

Tip: Volumes are only touched with --volumes; think twice before adding it on a server with databases.

docker info beginner Show daemon-wide details: storage driver, cgroup version, container counts, registries and warnings.

Example

bash
docker info
docker info --format '{{.ServerVersion}} {{.Driver}} cgroup v{{.CgroupVersion}}'
28.5.1 overlay2 cgroup v2

Tip: If this fails with "Cannot connect to the Docker daemon", the service is down or your user isn't in the docker group.

docker version beginner Print client and server (engine) versions and API versions.

Example

bash
docker version --format 'client {{.Client.Version}} / server {{.Server.Version}}'
client 28.5.1 / server 28.5.1
docker events advanced Stream what the daemon does in real time: containers starting, dying, OOM kills, image pulls.

Example

bash
docker events --filter type=container --filter event=die
# What happened in the last hour, then exit
docker events --since 1h --until 0s
2026-10-29T17:21:04.318922105Z container die 4c9d2e7f1a3b (exitCode=137, image=blog-api:1.4.0, name=blog-api)

Tip: Leave it running in a second terminal while reproducing a crash loop to see exactly when and why containers die.

↑ ↓ navigate Enter open Esc close