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
docker run --rm -it alpine shStart an interactive throwaway container.
docker ps -aList all containers, including stopped ones.
docker logs -f <container>Tail logs from a running container.
docker exec -it <container> shOpen a shell inside a running container.
docker system pruneReclaim disk by removing unused data.
Common commands
| Task | Command | Description |
|---|---|---|
| Show client/server versions | | Prints versions of the CLI and engine. |
| System-wide info | | Engine, storage driver, plugins, resources. |
| Run a container | | Create and start a container from an image. |
| List running containers | | Add -a to include stopped ones. |
| Stop a container | | Sends SIGTERM, then SIGKILL after timeout. |
| Remove a container | | Use -f to force-remove running ones. |
| List images | | Local image cache. |
| Remove an image | | Fails if a container still references it. |
| View logs | | Add -f to follow, --tail N for last N lines. |
| Exec into container | | Run a command in a running container. |
| Inspect object | | Low-level JSON for containers, images, networks. |
| Live resource stats | | CPU, memory, network, I/O per container. |
| Disk usage | | Space used by images, containers, volumes, build cache. |
| Prune unused data | | Add --volumes to also remove unused volumes. |
Useful flags
| Flag | Example | Meaning |
|---|---|---|
| -d, --detach | | Run container in the background. |
| -it | | Interactive TTY for shells and REPLs. |
| --rm | | Auto-remove container on exit. |
| -p | | Publish host:container port. |
| -v | | Bind-mount a host path into the container. |
| -e | | Set an environment variable. |
| --name | | Assign a stable container name. |
| --network | | 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 testForce-remove every stopped container
docker container prune -fReclaim 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 :latest in production.
- Always set --name for long-lived containers so scripts can reference them.
- Use --rm for one-shot commands to keep the host clean.
- Treat containers as immutable: rebuild the image, don't docker exec to patch.
- Run docker system df weekly; prune build cache before disk fills up.
Troubleshooting
Cannot connect to the Docker daemon
Start Docker Desktop / engine, then re-check.
docker infoPort is already allocated
Find and stop the conflicting container.
docker ps --filter publish=8080No space left on device
Reclaim space from images, volumes, and build cache.
docker system prune -af --volumesContainer exits immediately
Read the last logs to see why.
docker logs --tail 100 <container>