Docker Commands Cheat Sheet: The 40 Commands You Actually Use

Docker Commands Cheat Sheet: The 40 Commands You Actually Use

Docker’s command line is small enough to learn in a day and deep enough to surprise you for a year. On most days you will use the same ten commands: build an image, run a container, read its logs, stop it, remove it. The other thirty matter exactly when something goes wrong — a disk filling up, a port already in use, a container that exits the moment it starts — and that is when a cheat sheet earns its keep.

This guide covers the Docker commands you will actually use, grouped by the job you are doing: checking your setup, working with images, running containers, managing volumes and networks, driving Compose stacks, and cleaning up. Every command comes with the flags that matter, a short explanation of what it does, and the mistake people make with it most often. Commands are current for Docker Engine 29.10.1 and Compose v5.8.1, verified 5 Dec 2026. If you are new to containers, start with our Kubernetes tutorial for beginners for the mental model, then work through our Docker tutorial for beginners before treating this page as your reference.

Checking Your Docker Setup

Before you build or run anything, confirm Docker is installed, the daemon is reachable, and you know which version you are talking to. Version mismatches cause a surprising share of confusing errors: a flag that works on your laptop may not exist on an older server, and a Compose file written for v2 syntax will not parse on a machine still running the old standalone binary.

docker version
docker info
docker system df
docker context ls
docker --help
docker run --help

docker version prints the client and server versions separately, which is exactly the split you need when something behaves differently on two machines. If the server section is missing, the daemon is not running or not reachable — start it before doing anything else. docker info goes further: it shows how many containers and images exist, which storage driver is in use, how much memory the daemon can see, and whether the experimental features are on. Run it whenever a machine feels slow or strange; it is the fastest way to see the state of the whole installation.

docker system df shows how much disk space images, containers and volumes are using, and how much of it is reclaimable. This is the first command to run when a build fails with “no space left on device”, because Docker’s disk usage is invisible to a plain df -h until you know where to look. docker context ls lists the daemon endpoints your client knows about — local, remote over SSH, or a cloud host — and tells you which one is active. If a command seems to run against the wrong machine, a stale context is usually the reason.

Every command has built-in help, and using it is faster than searching the web. docker --help lists the top-level commands, and docker run --help lists every flag for that one command, with a short description of each. For the full flag list of any command, the official Docker CLI reference is the authority. The common mistake here is running Docker commands as a user who is not in the docker group and getting “permission denied” errors: either add your user to the group with sudo usermod -aG docker $USER and log back in, or prefix commands with sudo.

Images: Build, Tag, Pull and Push

An image is a layered, read-only template, and a container is a running instance of one. Every line in a Dockerfile that changes the filesystem adds a layer, and Docker caches those layers, which is why a second build is usually far faster than the first. Understanding layers is the key to understanding why builds are fast or slow, and why an image is large or small.

docker build -t my-app .
docker build -t my-app:1.2 .
docker build --no-cache -t my-app .
docker build -f docker/Dockerfile.prod .
docker build --build-arg NODE_ENV=production -t my-app .
docker build --target builder -t my-app:builder .

docker build -t my-app . builds the Dockerfile in the current directory — the trailing dot is the build context, the set of files Docker sends to the daemon — and tags the result my-app:latest. Tag a real version as well, because “latest” on your laptop and “latest” in production can quietly be two different images. --no-cache ignores the layer cache and rebuilds everything, which is what you want when a package install keeps returning stale results. -f points at a Dockerfile in a non-standard location, --build-arg passes a value into an ARG instruction, and --target builds only up to a named stage in a multi-stage Dockerfile — useful for building a test image without shipping the test tools.

The most common build mistake is a bloated build context. Docker sends the entire context directory to the daemon, so a stray node_modules folder or a gigabyte of test fixtures makes every build slow. A .dockerignore file excludes files the image does not need, the same way .gitignore does. The second most common mistake is forgetting the dot: docker build -t my-app with no context fails, because Docker does not know which files to build from.

docker images
docker pull nginx:1.27
docker pull postgres
docker tag my-app yourname/my-app:1.2
docker push yourname/my-app:1.2
docker rmi my-app:1.2
docker history my-app
docker save my-app > my-app.tar
docker load < my-app.tar

docker images lists the images on your machine with their tags, sizes and ages. docker pull fetches an image from a registry — always pin a version tag in production, because an unpinned postgres today may not be the same image tomorrow. docker tag gives a local image the registry path it needs before you can push it, and docker push uploads it; you must be logged in first with docker login. docker rmi deletes an image, and refuses if a container still references it — remove the container first, or pass -f and accept the consequences. Use docker history to find the layer that made an image large. docker save and docker load move images between machines without a registry, handy for air-gapped environments.

One more image mistake worth naming: pushing an image tagged only latest and then wondering why a deployment did not change. Registries and orchestrators cache by tag, and latest is often treated as already present. Versioned tags — 1.2.3, a Git commit hash, a build number — make every deployment unambiguous and every rollback a one-line change.

Running Containers: The docker run Flags That Matter

docker run is the command that turns an image into a running container, and most of Docker’s day-to-day power lives in its flags. The flags fall into a few groups: how the container runs, how it talks to the network, what data it can see, and what limits apply to it. Learn these and you can read almost any docker run line you meet in the wild.

docker run -d -p 8080:80 --name web nginx:1.27
docker run -it --rm ubuntu:24.04 bash
docker run -d -e DATABASE_URL=postgres://db:5432/app my-app
docker run -d --env-file .env my-app
docker run -d -v app-data:/data -v $(pwd)/config:/app/config:ro my-app
docker run -d --restart unless-stopped --memory=512m --cpus=1 my-app

-d detaches the container so it runs in the background and your terminal comes back; without it, the container holds your terminal and stopping it with Ctrl+C stops the container too. -p 8080:80 publishes the container’s port 80 on the host’s port 8080 — the order is host first, container second, and reversing them is one of the most common Docker mistakes in existence. --name gives the container a readable name instead of a random one, which makes every later command shorter. -it combines interactive input with a terminal, which is what you want for a shell session; --rm deletes the container when it exits, perfect for throwaway shells and one-off jobs.

Environment variables come in two flavors: -e KEY=value for one-offs, and --env-file .env for a whole file of them. Volumes work the same way: -v app-data:/data mounts a named volume, while -v $(pwd)/config:/app/config:ro bind-mounts a host directory read-only. The :ro suffix matters — it stops a container from writing into configuration it should only read.

--restart unless-stopped restarts the container if it crashes or the daemon restarts, but respects a manual docker stop. --memory=512m and --cpus=1 cap the resources a container can consume, so one runaway process cannot starve everything else on the host. The mistake to avoid here is running containers without any restart policy in production and discovering at 3 a.m. that a crashed container stayed crashed.

Managing, Inspecting and Debugging Containers

Running a container is the easy part. The daily work is watching it, getting inside it, reading its output, and restarting it when it misbehaves. These commands are the ones you will reach for in an incident, so they are worth learning before the incident happens rather than during it.

docker ps
docker ps -a
docker logs -f --tail 100 web
docker logs --since 2026-12-05T09:00 web
docker exec -it web bash
docker exec web cat /etc/nginx/nginx.conf
docker stop web
docker start web
docker restart web
docker rm web
docker rm -f web

docker ps lists running containers; add -a to include stopped ones, which is where you find the container that exited five minutes ago. docker logs -f follows a container’s output live, like tail -f, and --tail 100 limits it to the last hundred lines — always use --tail on a busy container, or your terminal will drown in output. --since limits logs to a time window, which is what you want when you already know roughly when the problem started.

docker exec -it web bash opens a shell inside a running container, which is how you poke around a live service, check its files, and test its network. The plain docker exec web cat /etc/nginx/nginx.conf form runs one command without a shell. Exec needs a running container: it cannot attach to a stopped one, so start it first or inspect the image with docker run instead.

docker stop sends SIGTERM and waits ten seconds for a clean shutdown before sending SIGKILL; applications that ignore SIGTERM will always take the full ten seconds to stop, which is a sign the application needs a signal handler, not a sign Docker is slow. docker restart stops and starts in one step. docker rm deletes a stopped container, and rm -f stops and deletes a running one — useful, but destructive, so double-check the name first.

docker inspect web
docker inspect -f '{{ .NetworkSettings.IPAddress }}' web
docker stats
docker stats --no-stream
docker top web
docker cp web:/app/logs ./logs
docker cp ./config.yaml web:/app/config.yaml

docker inspect prints the full JSON description of a container: its configuration, its mounts, its network settings, and its state. It is long, so the -f flag with a Go template extracts just the field you want — the IP address above is the classic example. docker stats shows live CPU, memory and network usage per container, and --no-stream takes a single snapshot instead of updating forever. docker top lists the processes running inside a container. docker cp copies files in both directions, which is how you pull logs or a generated file out of a container without committing to a volume first. The mistake here is copying a file into a container and expecting it to survive a rebuild: containers are ephemeral, and anything you copy in by hand disappears with the container.

Volumes and Networks

Containers are ephemeral by design: delete one and everything inside it goes with it. Volumes are how data survives, and networks are how containers talk to each other. Both are simple concepts with a few sharp edges, and most of the edges come from confusing the three ways Docker gives a container access to files.

docker volume ls
docker volume create app-data
docker volume inspect app-data
docker volume rm app-data
docker volume prune
docker run -d -v app-data:/data postgres:16
docker run -d --mount source=app-data,target=/data postgres:16

A named volume like app-data is managed by Docker and lives outside any single container, so it survives container deletion and can be attached to a replacement. A bind mount maps a host directory into the container, which is right for development — edit a file on the host and the container sees it immediately — and for configuration files. The third option, an anonymous volume, is created automatically when an image declares a VOLUME and you do not name one; it works, but it is hard to find later and easy to orphan, so name your volumes explicitly.

--mount is the newer, more explicit syntax: it spells out the source and the target and refuses ambiguous input, while -v is shorter and older. Either works; use --mount in scripts and documentation where clarity matters, and -v at the keyboard. The expensive mistake is docker volume prune on a host with data you care about: it deletes every volume not currently attached to a container, with one confirmation prompt standing between you and data loss. Run docker volume ls first, and never prune volumes on a machine you have not backed up.

docker network ls
docker network create app-net
docker network inspect app-net
docker network connect app-net web
docker network disconnect app-net web
docker network rm app-net
docker run -d --network app-net --name api my-app
docker run -d --network host my-app

Every container on the same user-defined network can reach the others by name — a container called api is reachable at http://api:8080 from any container on the same network — because Docker runs an embedded DNS server for user-defined networks. Containers on the default bridge network do not get this: they can only reach each other by IP address, which changes on every restart. That is the single most important networking fact in Docker, and the reason to create a network for anything with more than one container.

docker network connect and disconnect attach and detach a running container without restarting it, which is handy when you need to give a container temporary access to a service on another network. --network host removes network isolation entirely: the container shares the host’s network stack, so it can bind any port directly. Use it deliberately, not by default. The common mistake is exposing a database port with -p on a host that also runs the application, then wondering why connections arrive from the wrong place: containers on the same network should talk to each other by name, and only the public-facing service needs a published port.

Docker Compose Commands

Compose turns a multi-container application into a single file and a handful of commands. Instead of memorizing a long docker run line for each service, you describe the services, networks and volumes once in a compose.yaml file, and Compose creates and wires them together. The modern command is docker compose with a space — the Compose v2 plugin — not the old hyphenated docker-compose binary, which is retired. The Compose documentation covers the full file format and every command in detail.

docker compose up -d
docker compose up -d --build
docker compose ps
docker compose logs -f
docker compose logs -f api
docker compose exec api bash
docker compose stop
docker compose start
docker compose restart api
docker compose down
docker compose down -v

docker compose up -d starts every service in the file in detached mode, creating the networks and volumes it needs. Add --build to rebuild images first — Compose does not rebuild just because a Dockerfile changed, so forgetting this flag means your edits never reach the container. docker compose ps shows the state of the stack’s containers, and docker compose logs -f follows all of them at once; pass a service name to follow just one.

docker compose exec api bash opens a shell in the api service, mirroring docker exec but addressing the service by name instead of a container ID. docker compose stop and start pause and resume the stack without deleting anything, while restart api restarts a single service. docker compose down stops the stack and removes its containers and networks, and down -v also deletes its named volumes. That -v is the dangerous one: it is the right flag when you want a clean slate for local development, and the wrong flag on any machine holding data you want to keep. Our Docker Compose tutorial walks through a complete multi-service stack if you want the full worked example behind these commands.

docker compose pull
docker compose build
docker compose config
docker compose up -d --scale worker=3
docker compose run --rm worker python process_jobs.py

docker compose pull fetches the latest images for services that use prebuilt images, and docker compose build rebuilds the ones built from Dockerfiles — running both before up is a good deployment habit. docker compose config prints the merged configuration after all files and environment variable interpolation, which is how you check that a variable actually resolved to the value you expected before you start the stack. --scale worker=3 starts three copies of a service, useful for background workers that do not need published ports. docker compose run --rm starts a one-off container from a service definition — a migration, a data job, a shell — and removes it when it exits. The mistake here is using run for a service that needs the stack’s ports: run does not publish the service’s ports by default, so add --service-ports when you need them.

Cleaning Up and Troubleshooting

Docker accumulates disk usage quietly: every build leaves layers, every stopped container keeps its filesystem, and every unused image sits there until you remove it. A host that ran fine for months can suddenly fail builds with no space left, and the fix is a handful of prune commands — used with care, because they delete things.

docker container prune
docker image prune
docker image prune -a
docker volume prune
docker network prune
docker system prune
docker system prune -a --volumes
docker system df -v

docker container prune removes stopped containers, docker image prune removes dangling images — layers with no tag — and image prune -a removes every image not used by a container, which is far more aggressive. docker volume prune and docker network prune remove unused volumes and networks; the volume one is the dangerous one, as noted above. docker system prune is the all-in-one: stopped containers, unused networks, dangling images. Add -a to include all unused images and --volumes to include volumes, and you have the nuclear option — everything not currently in use is gone. Run docker system df -v first to see exactly what is using space, so you prune with knowledge rather than hope.

When a container will not start, work through the same sequence every time. First, docker ps -a to see whether it is running, exited, or restarting in a loop. Then docker logs for the application’s own error, and docker inspect for the exit code and the last state. Then check the host: docker system df for disk pressure, docker info for daemon health. Most failures fall into a short list:

docker ps -a
docker logs web
docker inspect web --format '{{ .State.ExitCode }}'
docker system df
docker events --since 1h

“Cannot connect to the Docker daemon” means the daemon is not running, or your user cannot reach its socket — start the service or fix the group membership. “Port is already allocated” means another container or a host process owns the port: find it with docker ps or ss -tlnp and pick a different host port. “No space left on device” is the prune case above. “Manifest unknown” or “pull access denied” means the image name, tag, or registry credentials are wrong — check the spelling, the tag, and docker login. A container that exits immediately with code 1 usually has an application error in its logs; exit code 137 means the kernel killed it for using too much memory, so raise --memory or fix the leak. docker events --since 1h shows the daemon’s recent activity — creates, starts, stops, kills — which is how you reconstruct what happened to a container nobody admits to touching.

Frequently Asked Questions

How many Docker commands do I actually need to learn?

About ten cover daily work: build, run, ps, logs, exec, stop, rm, images, pull and push. The rest of this guide is for the weekly and monthly jobs — volumes, networks, Compose, cleanup — and for the day something breaks. Learn the ten first, then add the others as you need them.

What is the difference between docker run and docker exec?

docker run creates a new container from an image and starts it; docker exec runs a command inside a container that is already running. Use run to start services and exec to inspect or debug them. A common confusion is trying to exec into a stopped container — it cannot work, because there is no running process to attach to.

Why does docker system prune not free all my disk space?

By default, docker system prune removes stopped containers, unused networks and dangling images, but it leaves volumes and untagged images alone. Volumes hold your data, so Docker is conservative about them. Add -a to remove all unused images and --volumes to remove unused volumes — and check docker system df -v first so you know what you are about to delete.

Should I use docker compose or docker-compose?

Use docker compose, with a space. It is the Compose v2 plugin, built into the Docker CLI, and it is the version that receives updates. The hyphenated docker-compose was the old standalone binary and is retired. The commands are mostly the same, so switching is a matter of habit, not relearning.

How do I find out why a container keeps exiting?

Run docker ps -a to confirm it exited, then docker logs for the application’s error message, then docker inspect for the exit code. Code 1 is usually an application error; code 137 means the container was killed for exceeding its memory limit. If the logs are empty, the application may log to a file inside the container instead of standard output.

Conclusion

A cheat sheet is only useful if the commands on it are the ones you reach for, and Docker’s are unusually stable: the build, run, log, exec, stop and prune commands in this guide have worked the same way for years, and the flags that matter — -d, -p, -v, --rm, --name — have not changed. What changes is the version underneath them, so check docker version when a flag behaves differently than you expect, and keep this page bookmarked for the commands you use once a month rather than once a day.

The fastest way to make these commands stick is to use them on a real project: build an image for an application you own, run it with a named volume and a user-defined network, put it behind Compose, and then deliberately break it and fix it with the commands you have just read. That practice is what turns a reference into reflex.

Scroll to Top