Join our Discord Server

Docker CLI Cheatsheet

← 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 versionsdocker versionPrints versions of the CLI and engine.
System infodocker infoEngine, storage driver, plugins, resources.
Run a containerdocker run <image>Create and start a container from an image.
List runningdocker psAdd -a to include stopped ones.
Stop a containerdocker stop <container>Sends SIGTERM, then SIGKILL after timeout.
Remove a containerdocker rm <container>Use -f to force-remove running ones.
List imagesdocker imagesLocal image cache.
Remove an imagedocker rmi <image>Fails if a container still references it.
View logsdocker logs <container>Add -f to follow, –tail N for last N lines.
Exec into containerdocker exec -it <container> shRun a command in a running container.
Inspect objectdocker inspect <id>Low-level JSON for containers, images, networks.
Live statsdocker statsCPU, memory, network, I/O per container.
Disk usagedocker system dfSpace used by images, containers, volumes, build cache.
Prune unused datadocker system pruneAdd –volumes to also remove unused volumes.

Useful flags

FlagExampleMeaning
-d, --detachdocker run -d nginxRun container in the background.
-itdocker run -it alpine shInteractive TTY for shells and REPLs.
--rmdocker run --rm alpine echo hiAuto-remove container on exit.
-pdocker run -p 8080:80 nginxPublish host:container port.
-vdocker run -v $(pwd):/app nodeBind-mount a host path into the container.
-edocker run -e NODE_ENV=prod appSet an environment variable.
--namedocker run --name api appAssign a stable container name.
--networkdocker run --network host nginxAttach 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
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 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
Container exits immediately

Read the last logs to see why.

docker logs --tail 100 <container>

Related cheatsheets


Last reviewed: 2026-06-15
Join our Discord Server