← All cheatsheets
Docker CLI Cheatsheet
The core docker command for managing containers, images, and the local engine.
What it is
Use the Docker CLI for day-to-day work with containers and images: running workloads, inspecting state, viewing logs, cleaning up disk, and debugging.
Installation
Ships with Docker Desktop and Docker Engine. Verify with docker version and docker info.
Quick start
Start an interactive throwaway container.
List all containers, including stopped ones.
Tail logs from a running container.
Open a shell inside a running container.
Reclaim disk by removing unused data.
Common commands
| Task | Command | Description |
|---|---|---|
| Show versions | docker version | Prints versions of the CLI and engine. |
| System info | docker info | Engine, storage driver, plugins, resources. |
| Run a container | docker run <image> | Create and start a container from an image. |
| List running | docker ps | Add -a to include stopped ones. |
| Stop a container | docker stop <container> | Sends SIGTERM, then SIGKILL after timeout. |
| Remove a container | docker rm <container> | Use -f to force-remove running ones. |
| List images | docker images | Local image cache. |
| Remove an image | docker rmi <image> | Fails if a container still references it. |
| View logs | docker logs <container> | Add -f to follow, –tail N for last N lines. |
| Exec into container | docker exec -it <container> sh | Run a command in a running container. |
| Inspect object | docker inspect <id> | Low-level JSON for containers, images, networks. |
| Live stats | docker stats | CPU, memory, network, I/O per container. |
| Disk usage | docker system df | Space used by images, containers, volumes, build cache. |
| Prune unused data | docker system prune | Add –volumes to also remove unused volumes. |
Useful flags
| Flag | Example | Meaning |
|---|---|---|
-d, --detach | docker run -d nginx | Run container in the background. |
-it | docker run -it alpine sh | Interactive TTY for shells and REPLs. |
--rm | docker run --rm alpine echo hi | Auto-remove container on exit. |
-p | docker run -p 8080:80 nginx | Publish host:container port. |
-v | docker run -v $(pwd):/app node | Bind-mount a host path into the container. |
-e | docker run -e NODE_ENV=prod app | Set an environment variable. |
--name | docker run --name api app | Assign a stable container name. |
--network | docker run --network host nginx | Attach to a specific network. |
Real-world examples
Run nginx, publish port 8080
docker run -d --name web -p 8080:80 nginx:alpineMount your project into a node container
docker run --rm -it -v "$(pwd)":/app -w /app node:20 npm testReclaim disk aggressively
docker system prune -af --volumesRemoves unused images, networks, build cache, and volumes. Irreversible.
Best practices
- Pin image tags (e.g.
nginx:1.27-alpine) instead of:latestin production. - Always set
--namefor long-lived containers so scripts can reference them. - Use
--rmfor one-shot commands to keep the host clean. - Treat containers as immutable: rebuild the image, don't exec to patch.
- Run
docker system dfweekly; prune build cache before disk fills up.
Troubleshooting
Cannot connect to the Docker daemon
Start Docker Desktop / engine, then re-check.
Port is already allocated
Find and stop the conflicting container.
Container exits immediately
Read the last logs to see why.
Related cheatsheets
Last reviewed: 2026-06-15