Docker logoDocker v24.0INTERMEDIATE

Docker

Docker cheat sheet with essential commands for containers, images, volumes, networks, Docker Compose, and orchestration examples.

10 min read
dockercontainersdevopsdeploymentvirtualization

Container Management

Running Containers

Start and run Docker containers with various options

bash
💡 Use -d for background containers
⚠️ Always name containers for easier management
📌 Use --rm for temporary containers
✅ Set resource limits in production

Container State Management

Control container lifecycle - start, stop, restart, and remove

bash
💡 docker ps -a shows stopped containers too
⚠️ Use -f flag carefully to force remove
📌 Container prune removes all stopped containers
✅ Always stop before removing unless using -f

Container Inspection & Logs

Debug and inspect running containers for troubleshooting

bash
💡 Use -f to follow logs in real-time
📌 docker exec -it for interactive sessions
⚡ Inspect with --format for specific fields
✅ Copy files without stopping container

Executing Commands in Containers

Run commands inside running containers with docker exec and copy files with docker cp

bash
💡 docker exec -it opens an interactive terminal — use sh for Alpine, bash for Debian/Ubuntu
⚡ Use docker cp to quickly grab log files or inject config without rebuilding
📌 docker stats shows live resource usage — essential for debugging memory/CPU issues
🟢 docker exec -d runs the command in the background without attaching to it
execcpdebug

Image Management

Building Images

Build Docker images from Dockerfiles with various options

bash
💡 Use .dockerignore to exclude files
⚡ BuildKit provides advanced features
📌 Multi-stage builds reduce image size
✅ Always tag images with versions

Image Operations

Manage Docker images - pull, push, tag, and remove

bash
💡 Use specific tags instead of latest
⚠️ Prune regularly to save disk space
📌 Save images for offline transfer
✅ Always login before pushing

Image Registry

Work with Docker registries and manage image distribution

bash
🔒 Always use HTTPS for production registries
💡 Tag images with registry URL
📌 Local registry useful for testing
✅ Implement authentication for private registries

Dockerfile

Write Dockerfiles to build custom images with all the key instructions

Dockerfile Instructions

The essential Dockerfile commands for building images

dockerfile
💡 Always use specific image tags (node:22-alpine) not "latest" — reproducible builds
⚡ Copy package.json first, then npm install, then copy source — maximizes Docker layer cache
📌 ARG is build-time only; ENV persists into the running container — know the difference
🟢 Run as a non-root USER in production — it only takes 2 lines and prevents privilege escalation
dockerfilebuildinstructions

Multi-Stage Builds

Use multiple FROM stages to create small, optimized production images

dockerfile
💡 Multi-stage builds keep dev tools (compilers, build deps) out of the final image
⚡ Go apps can use FROM scratch for the final stage — just the binary, ~10MB total
📌 COPY --from=stagename copies files from a previous stage into the current one
🟢 Use --target to build a specific stage: docker build --target build .
multi-stagebuildoptimization

.dockerignore

Exclude files from the build context to speed up builds and avoid leaking secrets

bash
💡 Without .dockerignore, Docker sends EVERYTHING in the directory to the build daemon
⚡ Always ignore node_modules — they get reinstalled with npm ci inside the image
📌 Ignore .env files to prevent secrets from being baked into the image
🟢 Same syntax as .gitignore — supports wildcards, negation (!important.md), and comments
dockerignorebuildsecurity

Volumes & Networks

Volume Management

Create and manage Docker volumes for persistent data storage

bash
💡 Named volumes persist data between containers
📌 Bind mounts for development, volumes for production
⚠️ Prune removes all unused volumes
✅ Backup important volumes regularly

Network Management

Configure Docker networks for container communication

bash
💡 Containers on same network can communicate by name
🔒 Use internal networks for security
📌 Bridge is default, overlay for Swarm
✅ Custom networks provide better isolation

Docker Compose

Compose Basics

Essential Docker Compose commands for multi-container applications

bash
💡 Use -d for background operation
⚡ --scale for horizontal scaling
📌 -f to specify custom compose files
✅ Always use down -v to clean volumes

Compose Configuration

Full compose.yml reference with depends_on, healthcheck, profiles, watch, and more

yaml
💡 depends_on with condition: service_healthy waits until the dependency passes its healthcheck
⚡ Compose Watch syncs files without rebuilding — use action: sync for source, action: rebuild for deps
📌 Profiles let you define optional services (debug tools, admin panels) that only start when requested
🟢 Use env_file to load .env files — keeps secrets out of compose.yml and version control

System & Cleanup

System Management

Monitor and manage Docker system resources and information

bash
💡 Regular pruning prevents disk issues
⚠️ Prune -a removes all unused images
📌 Use filters to control what gets pruned
✅ Monitor disk usage with system df

Debugging & Troubleshooting

Debug containers and resolve common Docker issues

bash
💡 nicolaka/netshoot has many network tools
🔍 Use --progress=plain for detailed build output
📌 Override entrypoint to debug failed containers
✅ Always check logs first when debugging