Docker Commands: Cheat Sheet with Examples
Docker commands cheat sheet: build, run and stop containers, ports and volumes, logs and exec, networks, Docker Compose and cleaning up disk space.
Docker commands grouped by job, each with a short explanation: pulling and building images, running containers with ports, environment variables and volumes, reading logs and opening a shell inside a container, networks, Docker Compose, and cleaning up the disk space Docker quietly fills. Parts in angle brackets, such as <image> or <container>, are what you replace; a container can be named by its name or the first few characters of its ID. Compose is shown as docker compose with a space, the current plugin, and the older docker-compose command takes the same options. The clean-up table is in order from safest to most thorough, and the last one deletes volumes, which hold databases and uploads. The kubectl Commands cheat sheet covers the next step, running containers on Kubernetes, and the Linux Command Cheatsheet covers the shell inside most images.
Images
| Command | What it does |
|---|---|
| docker pull <image> | Download an image, such as nginx or node:22 |
| docker images | List images on this machine (also docker image ls) |
| docker build -t <name>:<tag> . | Build an image from the Dockerfile in this folder |
| docker build -f Dockerfile.dev -t <name> . | Build from a different Dockerfile |
| docker tag <image> <user>/<image>:<tag> | Give an image another name, ready to push |
| docker login | Sign in to a registry (Docker Hub unless you name another) |
| docker push <user>/<image>:<tag> | Upload an image to a registry |
| docker rmi <image> | Delete an image |
| docker image prune | Delete dangling images (untagged leftovers from builds) |
| docker history <image> | Show the layers an image is made of and their sizes |
Running containers
| Command | What it does |
|---|---|
| docker run <image> | Start a container from an image |
| docker run -d --name web -p 8080:80 nginx | Run in the background, named web, with port 8080 on this machine sent to 80 inside |
| docker run -it ubuntu bash | Start a container with an interactive shell |
| docker run --rm <image> | Delete the container when it stops |
| docker run -e KEY=value <image> | Set an environment variable |
| docker run --env-file .env <image> | Set environment variables from a file |
| docker run -v "$(pwd)":/app <image> | Mount the current folder inside the container |
| docker run -v data:/var/lib/data <image> | Use a named volume that outlives the container |
| docker run --restart unless-stopped <image> | Start the container again after crashes and reboots |
| docker ps | List running containers |
| docker ps -a | List all containers, stopped ones too |
| docker stop <container> | Stop a container (start and restart work the same way) |
| docker rm <container> | Delete a stopped container (-f stops it first) |
| docker container prune | Delete all stopped containers |
Look inside and debug
| Command | What it does |
|---|---|
| docker logs <container> | Show a container's output |
| docker logs -f --tail 100 <container> | Follow the output, starting from the last 100 lines |
| docker exec -it <container> sh | Open a shell in a running container (bash if the image has it) |
| docker inspect <container> | Full details: settings, mounts, network, IP address |
| docker stats | Live CPU, memory and network use per container |
| docker top <container> | Processes running inside a container |
| docker cp <container>:/path ./local | Copy a file out of a container (or the other way round) |
| docker port <container> | Which ports are published |
Volumes and networks
| Command | What it does |
|---|---|
| docker volume ls | List volumes |
| docker volume create <name> | Make a volume |
| docker volume rm <name> | Delete a volume and its data |
| docker network ls | List networks |
| docker network create <name> | Make a network: containers on it can reach each other by name |
| docker run --network <name> <image> | Start a container on a network |
| docker network connect <network> <container> | Add a running container to a network |
Docker Compose
| Command | What it does |
|---|---|
| docker compose up | Start everything in compose.yaml (or docker-compose.yml) |
| docker compose up -d | Start in the background |
| docker compose up --build | Rebuild images first |
| docker compose down | Stop and remove the containers and networks |
| docker compose down -v | Also delete the volumes (their data is lost) |
| docker compose ps | List the project's containers |
| docker compose logs -f <service> | Follow one service's output |
| docker compose exec <service> sh | Open a shell in a running service |
| docker compose pull | Download newer images |
| docker compose restart <service> | Restart one service |
| docker compose config | Show the final configuration after variables are filled in |
Compose v2 is a plugin, so it's docker compose with a space. The older docker-compose command works the same way.
Clean up
| Command | What it does |
|---|---|
| docker system df | How much space images, containers and volumes use |
| docker system prune | Delete stopped containers, unused networks, dangling images and build cache |
| docker image prune -a | Delete every image no container uses |
| docker system prune -a --volumes | Delete everything unused, volumes included (careful: data in them is lost) |
Common use cases
- Remember the options for docker run (ports, volumes, environment)
- Debug a container that keeps stopping
- Free up disk space Docker is using
- Learn Docker and Compose for a course or a new job
How to use
- Find the table for what you're doing, such as Running containers or Clean up.
- Type a word in the filter, such as "logs" or "volume".
- Copy the command and replace the parts in angle brackets.
- Run docker ps afterwards to check what's running.
FAQ
What's the difference between an image and a container?
An image is the read-only package: the files, libraries and settings an app needs. A container is a running (or stopped) copy of an image with its own writable layer. One image can run as many containers.
Why does my container stop straight away?
A container lives only as long as its main process. If that process finishes or crashes, the container stops. docker logs <container> shows the output, and docker ps -a shows the exit code.
What does -p 8080:80 mean?
The first number is the port on your machine and the second is the port inside the container. With -p 8080:80, opening localhost:8080 reaches the web server listening on port 80 inside.
How do I keep data when a container is deleted?
Store it in a volume (-v name:/path) or a folder on your machine (-v /host/path:/path). Anything written only inside the container's own layer is lost when the container is removed.
Is docker system prune safe?
It removes stopped containers, unused networks, dangling images and build cache, but not volumes, so data is kept. Adding -a removes every image no container uses, and --volumes also deletes unused volumes and the data in them.
Related tools
- kubectl Commands: Kubernetes Cheat Sheetkubectl cheat sheet for Kubernetes: contexts and namespaces, get and describe, apply, scale and roll back…
- Git Commands: Cheat Sheet with ExamplesGit commands cheat sheet: set up, stage and commit, branch, merge and rebase, push and pull, history, stash…
- Linux Command CheatsheetCommon Linux terminal commands for file management and system administration.
- npm Commands: Cheat Sheet with Examplesnpm commands cheat sheet: install, update and remove packages, npm ci, run scripts and npx, audit and fix…
- Mac Keyboard ShortcutsmacOS keyboard shortcuts for productivity and navigation.
- MIME Type LookupMIME type reference for content type headers and file handling.
- YAML ValidatorIn Utilities