Docker CLI Cheatsheet - Command Reference
All essential Docker commands organized by Image, Container, Network, Volume, Registry, Build Debug, and Compose. Copy and tweak on the spot during troubleshooting.
Back to Containers & OrchestrationImage 8
docker imagesList local images, add -a to include intermediate layers
docker pull nginx:alpinePull image with tag, defaults to latest if omitted
docker build -t app:1.0 .Build and tag from Dockerfile in current dir
docker rmi <image>Remove image, stop container first or use -f
docker tag app:1.0 reg/app:1.0Tag image for registry push
docker save -o app.tar app:1.0Export image as tar for offline transfer
docker load -i app.tarLoad image from tar file, useful in offline environments
docker pull alpine:3.20 --platform=linux/amd64Pull image for a specific platform, e.g. amd64 on ARM Mac
Container 9
docker ps -aList all containers including stopped
docker run -d -p 8080:80 --name web nginxRun detached, map port, name container
docker exec -it web shInteractive shell into running container
docker logs -f --tail 100 webFollow last 100 lines of container logs
docker stop web && docker rm webStop and remove container
docker inspect webFull container config JSON for network/mount debugging
docker start web && docker restart webStart or restart a stopped container
docker statsLive CPU/memory/network I/O stats for all containers
docker top webList running processes inside a container
Network & Volume 8
docker network lsList networks, check here first for inter-container access
docker network create appnetCreate custom bridge network for name-based discovery
docker network connect appnet webConnect a running container to a network
docker volume lsList volumes
docker run -v data:/var/lib/app appMount named volume for persistence
docker run -v $(pwd):/app appBind mount current dir, common in dev
docker volume create appdataCreate a named volume for shared data
docker volume pruneRemove all unused volumes
Registry 6
docker loginLog in to Docker Hub, required before push
docker login registry.example.comLog in to a private registry (Harbor/Registry)
docker push app:1.0Push image to remote registry, must tag first
docker pull ubuntu:22.04Pull Ubuntu 22.04 image from Docker Hub
docker search nginxSearch Docker Hub for official images
docker logoutLog out from the current registry
Build & Debug 7
docker build -t app:1.0 --no-cache .Force rebuild without cache layers
docker build -t app:1.0 --target=dev .Build only up to a specific multi-stage target
docker history app:1.0View image build history, layer sizes and commands
docker diff <container>Inspect filesystem changes in a container
docker cp app.conf web:/etc/nginx/conf.d/Copy local config file into a container
docker cp web:/var/log/nginx/access.log ./Copy a log file from a container to local
docker events --since 5mStream Docker daemon events in real time
Compose & Cleanup 9
docker compose up -dStart Compose project in background
docker compose logs -f svcFollow logs for a specific service
docker compose downStop and remove containers, networks; add -v for volumes
docker compose psList container status for all services in Compose project
docker compose restartRestart all or specified services
docker system dfCheck disk usage by images/containers/volumes
docker system prune -aClean all unused images and containers, use -a with caution
docker container pruneRemove all stopped containers
docker image prune -aRemove all unused images to free disk space
FAQ 5
Q: How to clean up all unused Docker resources?A: docker system prune -a removes all unused images, containers, networks, and build cache. Add --volumes to also remove volumes.
Q: How to view container logs?A: docker logs <container> shows full logs, add -f to follow, --tail 50 for last 50 lines, --since 5m for last 5 minutes.
Q: How to enter a running container?A: docker exec -it <container> /bin/bash (or /bin/sh). Exit with exit or Ctrl+D.
Q: How to copy files between host and container?A: docker cp <src> <container>:<dest> to copy in, docker cp <container>:<src> <dest> to copy out.
Q: What restart policies are available?A: --restart=no (default)/on-failure/always/unless-stopped. unless-stopped is common for production.
💡 Tips
- In docker run -p, it's host_port:container_port — reversing them blocks external access.
- If exec says "executable file not found", switch to sh: Alpine images usually lack bash.
- prune -a removes all images not used by any container — verify with docker images first on production.
- docker system df quickly shows disk usage — run it periodically to avoid /var/lib/docker filling up.
- docker compose restart is faster than down/up and does not rebuild networks or volumes — best for nginx config changes.