All cheatsheets
CoreBeginner

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 sh

Start an interactive throwaway container.

docker ps -a

List all containers, including stopped ones.

docker logs -f <container>

Tail logs from a running container.

docker exec -it <container> sh

Open a shell inside a running container.

docker system prune

Reclaim disk by removing unused data.

Common commands

TaskCommandDescription
Show client/server versions
docker version
Prints versions of the CLI and engine.
System-wide info
docker info
Engine, storage driver, plugins, resources.
Run a container
docker run <image>
Create and start a container from an image.
List running containers
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 resource 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

FlagExampleMeaning
-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:alpine

Mount your project into a node container

docker run --rm -it -v "$(pwd)":/app -w /app node:20 npm test

Force-remove every stopped container

docker container prune -f

Reclaim disk aggressively

docker system prune -af --volumes

Removes 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 info

Port is already allocated

Find and stop the conflicting container.

docker ps --filter publish=8080

No space left on device

Reclaim space from images, volumes, and build cache.

docker system prune -af --volumes

Container exits immediately

Read the last logs to see why.

docker logs --tail 100 <container>

Official Docker Docs references

Last reviewed: