←Back to Guides
๐ŸณDocker & DevOpsโฑ๏ธ 13 min read•Updated October 2026

Docker & Containers Master Cheat Sheet: CLI Commands, Compose & Image Pruning

The complete practical reference for container lifecycle operations, multi-stage Dockerfile architecture, Docker Compose v2 commands, port mappings, and deep storage reclamation.

#Docker#DevOps#Containers#Docker Compose#Linux#Sysadmin#Microservices

1. Container Lifecycle Operations

Essential commands for running, inspecting, executing interactive shells inside, and monitoring running containers.
Run Detached Container with Port & Volume Mapping
docker run -d \
  --name my-web \
  --restart unless-stopped \
  -p 8080:80 \
  -v $(pwd)/html:/usr/share/nginx/html:ro \
  nginx:alpine

Spawns a restart-resilient container with host port binding and read-only volume mounting.

Open Interactive Bash/Sh Shell Inside Container
docker exec -it my-web sh

Drops into an interactive terminal session inside the running container.

Live Follow Container Logs with Timestamps
docker logs -f --tail 100 -t my-web

Streams the last 100 log lines with precise ISO timestamps.

Live Resource Usage Monitoring (CPU, RAM, Net I/O)
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"

Outputs a compact diagnostic table of active container resource consumption.

2. Multi-Stage Dockerfile Best Practices

Bloated Docker images waste disk space, slow down CI/CD deployments, and introduce security vulnerabilities. Use multi-stage builds and minimal base images: ```dockerfile # Stage 1: Build environment FROM node:20-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build # Stage 2: Minimal production runtime FROM nginx:alpine-slim COPY --from=builder /app/dist /usr/share/nginx/html USER 1001:1001 EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] ``` **Key Hardening Rules:** - Never run containers as `root`. Explicitly specify `USER node` or an unprivileged UID. - Always include a `.dockerignore` file excluding `node_modules`, `.git`, `.env`, and build caches. - Use specific immutable version tags instead of `:latest` for deterministic builds.

3. Docker Compose v2 Orchestration

Manage multi-container applications (database, API, cache) with clean Compose commands:
Launch Stack in Background & Build Images
docker compose up -d --build

Rebuilds updated Dockerfiles and boots all dependent services detached.

Tear Down Stack and Delete Named Volumes
docker compose down -v --remove-orphans

Stops containers, deletes networks, and cleans up persistent storage volumes.

Restart a Single Service in Compose
docker compose restart api

Restarts only the target service without disrupting other microservices.

4. Deep Storage Reclamation & Layer Pruning

Dangling images, stopped build containers, and orphan volumes silently consume tens of gigabytes of disk space on development workstations. Run this sequence to safely reclaim storage:
Inspect Docker Disk Footprint
docker system df

Displays space used by images, containers, local volumes, and build cache.

Deep System Prune (Images, Volumes & Build Cache)
docker system prune -af --volumes

Deletes all stopped containers, unused networks, unreferenced images, and orphan volumes.

Purge BuildKit Cache Specifically
docker builder prune -af

Frees gigabytes of cached intermediate layers accumulated during docker build commands.

๐ŸŽ“Explore More Guides

โœจ