Docker packages applications into isolated containers that run the same way on a laptop, server, or CI pipeline — but the command line is where the power is. This Docker commands cheat sheet covers 70+ Docker commands organized by workflow — build, run, manage, compose, and cleanup — with copy-paste examples and flags explained, so the right command is found in seconds.
Based on the official Docker CLI reference, this guide goes beyond an alphabetical list: it explains the mental model (image vs container vs volume vs network), the daily 7-command loop, and the advanced patterns that save hours in production.
docker build -t app:1.0 . → docker run -d -p 3000:3000 app:1.0 → docker ps → docker logs -f → docker exec -it. For multi-container apps, use docker compose up -d. Find the full command for any task in the Docker commands cheat sheet below — organized by workflow with copy-paste examples.
Docker Mental Model — Image vs Container vs Volume vs Network
Confusion disappears once the four objects are clear.
- Image: Read-only blueprint built from a Dockerfile — e.g.,
myapp:1.0. Created withdocker buildordocker pull. One image can create many containers. - Container: Running instance of an image — e.g.,
a1b2c3 • Up 2h. Isolated with a writable layer. Created withdocker run, managed withdocker ps, exec, logs, stop, rm. - Volume: Persistent data that survives
docker rm— e.g.,mydata:/data. Managed withdocker volume ls, create, rmand mounted with-v. - Network: Connectivity between containers — e.g.,
bridge, myapp-net. Managed withdocker network ls, create, inspectand attached with--networkor-p host:containerfor host access.
Key lifecycle: build → run → ps → logs → exec → stop → rm. Stopped containers still use disk until removed; images persist until rmi.
Everyday Flow — 7 Commands That Cover 80%
docker build -t myapp:1.0 .
docker run -d -p 3000:3000 --name app myapp:1.0
docker ps -a # running + stopped
docker logs -f app --tail 100 # follow logs
docker exec -it app sh # shell inside
docker stop app && docker rm app
docker system prune -a # cleanup unused (check df first)
Essential Docker Commands — By Category (35 Commands)
1. Image — Build, List, and Share
docker build -t myapp:1.0 . # build from Dockerfile
docker images # or: docker image ls
docker rmi myapp:1.0 # remove image
docker pull nginx:alpine # download from registry
docker history myapp:1.0 # layers
docker tag myapp:1.0 myrepo/myapp:1.0 # retag for push
2. Container — Run and Manage
docker run -d -p 3000:3000 --name app myapp:1.0 # detached, port, name
docker ps # running only
docker ps -a # all incl. stopped
docker stop <id> # graceful stop
docker start <id> # restart stopped
docker rm <id> # remove (must be stopped, or -f)
docker rm -f <id> # force remove running
docker exec -it <id> sh # interactive shell (sh or bash)
docker logs -f <id> --tail 100
docker inspect <id> # full JSON config
3. Volume and Network — Persist and Connect
docker volume ls
docker volume create mydata
docker run -v mydata:/data myapp # mount volume
docker network ls
docker network create mynet
docker run --network mynet myapp # join network
docker network inspect mynet
docker volume rm mydata # delete (when unused)
4. Compose — Multi-Container Apps
For apps with web + db + cache, Compose replaces a dozen docker run flags with one file (docker-compose.yml) and one command:
docker compose up -d # start all services detached
docker compose down # stop + remove network
docker compose ps
docker compose logs -f # all services
docker compose exec web sh # shell in "web" service
docker compose build # rebuild images
docker compose down -v --remove-orphans # + volumes + orphans
5. Cleanup and System
docker system df # disk use
docker system prune # unused containers/networks
docker system prune -a # + unused images
docker image prune -a # unused images
docker container prune # stopped containers
docker volume prune # unused volumes (danger! deletes data)
docker builder prune # build cache
Check docker system df before pruning to see what will be freed. Never run volume prune on production without backup.
6. Registry — Share Images
docker login
docker pull nginx:alpine
docker push myrepo/myapp:1.0
docker search nginx
docker logout
docker buildx build --push -t myrepo/myapp:1.0 . # buildx + push multi-arch
Most Copied Combos
docker build -t myapp:1.0 . && docker run -d -p 3000:3000 myapp:1.0
docker exec -it <id> sh
docker compose up -d --build
docker logs -f --tail 50 <container>
docker system prune -af # clean all unused (confirm first!)
Advanced Docker — When Basics Aren't Enough
Buildx — Multi-Arch and Cache
docker buildx build --platform linux/amd64,linux/arm64 -t repo/app:1.0 . builds M1 and Intel images in one push. Add --push to push directly, or --target builder for multi-stage targets.
Debug and Inspect
docker inspect <id>— full JSON config ( mounts, env, network)docker stats— live CPU/memory per containerdocker events— real-time event stream for debugging lifecycle
Copy and Health
docker cp <id>:/app/log.txt ./anddocker cp ./config.json <id>:/app/— one-off file exchange without a volume--restart unless-stoppedondocker run(ordocker update) for auto-restart on reboot/failure, andHEALTHCHECKin the Dockerfile for orchestration
Common Recipes — Copy-Paste
# Rebuild & restart after code change
docker compose up -d --build
# Enter shell to debug
docker exec -it <container> sh # or bash
# Clean up safely
docker system df && docker system prune
# Follow logs live
docker logs -f --tail 100 <container>
Docker Cheat Sheet Quick Reference — Flags Explained
| Flag | Means | Example |
|---|---|---|
-d | Detached (background) | docker run -d nginx |
-p 3000:3000 | Host:container port | -p 8080:80 |
-it | Interactive + TTY | docker run -it ubuntu bash |
--rm | Remove after exit | docker run --rm alpine echo hi |
-v / --mount | Volume | -v mydata:/data |
-e | Env var | -e NODE_ENV=production |
--name | Container name | --name web |
-f / -a | Follow / all | docker logs -f, ps -a |
3 Safety Rules
- Don't prune on prod without
df— check disk first, confirm on staging - Use
-dfor services,-itfor debugging — detached for long-running, interactive for shells - Name containers (
--name) — easierlogsandexecthan IDs
FAQs About Docker Commands
What is the difference between docker image and docker container?
An image is a read-only template built from a Dockerfile; a container is a running instance of that image with a writable layer. One image can create many containers.
How do I run a Docker container in the background?
Use docker run -d for detached mode — e.g., docker run -d -p 3000:3000 --name app myapp:1.0. Check with docker ps and follow logs with docker logs -f app.
How do I get a shell inside a running container?
Use docker exec -it <container> sh (or bash if available). This opens an interactive shell without stopping the container.
What does docker compose up -d do?
It builds (if needed), creates, and starts all services defined in docker-compose.yml in detached mode, with a shared network and volumes. docker compose down stops and removes them.
How do I clean up unused Docker data?
Use docker system df to see usage, then docker system prune for unused containers/networks, -a to also remove unused images, and docker volume prune for volumes (caution: deletes data).
Why is my container not accessible on localhost?
Usually a port mapping or network issue: ensure -p host:container matches the app's internal port (e.g., app listens on 3000, map -p 3000:3000), and check docker logs and docker ps for the mapping.
Conclusion
Mastering Docker is about the workflow — image to container to logs to cleanup — not memorizing every flag. Start with the daily 7, add Compose for multi-container apps, and use the full cheat sheet for lookup by task. Keep this guide bookmarked for copy-paste commands from setup to production.