Skip to main content

Docker Commands Cheatsheet: Real Examples for Everyday Container Workflows

A practical Docker commands cheatsheet covering the commands developers actually run every day — with real terminal examples, debugging tips, and Compose shortcuts.

Published
#docker #devops #containers #cheatsheet

Docker Commands Cheatsheet: Real Examples for Everyday Container Workflows

Most Docker tutorials walk you through docker run hello-world and call it a day. Production containers are messier — dangling images, misnamed volumes, containers that won't stop. This cheatsheet covers the commands I actually run every week, with real input and output so you know what to expect.

Bookmark the interactive Docker Cheatsheet on Toolora if you want a searchable version with 80+ commands.


Starting and Stopping Containers the Right Way

The basics look simple until you need to name a container, map a port, and inject an environment variable all at once.

docker run -d \
  --name api \
  -p 8080:3000 \
  -e NODE_ENV=production \
  my-node-app:1.4.2

That -d flag detaches the process so your terminal is free immediately. Without --name, Docker generates something like jovial_banzai — fine for experiments, painful when you're scripting restarts.

To stop gracefully (SIGTERM → 10 s timeout → SIGKILL):

docker stop api

To force-kill immediately:

docker kill api

The remove-on-exit pattern is useful for one-shot tools:

docker run --rm -v "$(pwd)":/app node:20-alpine npm ci

The container disappears after npm ci finishes, leaving no cleanup work.


Image Management and Multi-Stage Build Sizes

Pulling the latest tag in CI is a common anti-pattern — latest changes without warning and can break a build days later. Pin versions:

docker pull postgres:16.3-alpine

Real example — before and after a multi-stage build:

Standard Go service image:

FROM golang:1.22
WORKDIR /app
COPY . .
RUN go build -o server .
$ docker images go-service:bloated
REPOSITORY     TAG       SIZE
go-service     bloated   922 MB

Multi-stage version:

FROM golang:1.22 AS builder
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 go build -o server .

FROM scratch
COPY --from=builder /app/server /server
ENTRYPOINT ["/server"]
$ docker images go-service:slim
REPOSITORY     TAG    SIZE
go-service     slim   8.3 MB

That is a 99% size reduction on a real service. Smaller images pull faster in CI — Google's own container best-practices guide cites a typical 30–60% reduction in deployment time when switching to distroless or scratch bases.

To remove all unused images (dangling layers included):

docker image prune -a --filter "until=72h"

I run this as a weekly cron on dev machines; without it, /var/lib/docker quietly fills up over months.


Debugging a Container That Won't Behave

I spent two hours last month on a container that worked on my laptop and failed in staging. The fastest diagnosis loop uses three commands:

1. Check recent logs:

docker logs --tail 100 --follow api

2. Get a shell inside the running container:

docker exec -it api /bin/sh

(Use /bin/bash for Debian-based images; Alpine and distroless images need /bin/sh or don't have a shell at all — for those, the docker debug command in Docker Desktop 4.27+ attaches a temporary debug toolkit.)

3. Inspect the full runtime config:

docker inspect api

Real output snippet (environment block):

"Env": [
    "NODE_ENV=production",
    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
]

That lets you confirm which environment variables actually reached the container — a copy-paste error in a .env file is invisible until you check here.

For CPU and memory use in real time:

docker stats --no-stream
CONTAINER ID   NAME  CPU %   MEM USAGE / LIMIT   NET I/O
a3f1e89c2b1a   api   0.12%   121MiB / 512MiB     4.2MB / 1.1MB

Volume and Network Commands Worth Memorizing

Named volumes survive container restarts and rebuilds. Create one explicitly:

docker volume create pgdata
docker run -d --name db -v pgdata:/var/lib/postgresql/data postgres:16.3-alpine

To back up a volume to a tar file without stopping the container:

docker run --rm \
  -v pgdata:/source:ro \
  -v "$(pwd)":/backup \
  busybox tar czf /backup/pgdata-$(date +%Y%m%d).tar.gz -C /source .

Networks control which containers can talk to each other. The default bridge network allows any container to reach any other by IP, which is too open for multi-tenant setups. Create an isolated network per stack:

docker network create app-net
docker run -d --name db --network app-net postgres:16.3-alpine
docker run -d --name api --network app-net -p 8080:3000 my-node-app:1.4.2

Now api can reach db by hostname (db:5432), but the database is not reachable from outside the network.


Compose Commands That Save Time Every Day

Docker Compose turns multi-container definitions into single commands. The commands developers use most often:

# Start everything, rebuild changed images, run in background
docker compose up -d --build

# Watch logs for specific services
docker compose logs -f api worker

# Run a one-off command inside a service without starting all dependencies
docker compose run --rm api node scripts/migrate.js

# Stop everything and remove containers (keeps volumes)
docker compose down

# Nuclear option — also removes volumes and images built by Compose
docker compose down -v --rmi local

The --build flag on up is worth making a habit. Without it, compose up uses the cached image even after you changed a Dockerfile, and you waste ten minutes wondering why the new code isn't running.

For shell scripting and curl-based healthchecks in Compose, the Bash Cheatsheet and cURL Cheatsheet cover the command patterns that pair naturally with container startup probes.


Quick Reference: The Ten Commands I Run Daily

| Task | Command | |------|---------| | List running containers | docker ps | | List all containers | docker ps -a | | Tail logs | docker logs -f <name> | | Shell into container | docker exec -it <name> sh | | Inspect config | docker inspect <name> | | Pull specific tag | docker pull <image>:<tag> | | Remove stopped containers | docker container prune | | Remove dangling images | docker image prune | | Check resource usage | docker stats --no-stream | | Rebuild and restart | docker compose up -d --build |

For a searchable, filterable version of this table with 80+ commands, annotations, and a Compose section, open the Docker Cheatsheet — every row is filterable by category so you can find network, volume, or exec commands without scrolling.


Made by Toolora · Updated 2026-06-22