Docker Management
When to Load
Load this skill when the task involves Dockerfile authoring, Compose stack design, container registry operations, multi-stage build optimization, production container patterns, or troubleshooting container runtimes.
Core Concepts
Dockerfiles
- Multi-stage builds: Separate build-time dependencies from runtime image —
FROM ... AS builderfor compilation, finalFROMfor minimal runtime. Drastically reduces image size - Layer optimization: Each
RUN,COPY,ADDcreates a layer. Order from least-to-most frequently changing (system deps first, app code last). CombineRUN apt-get update && apt-get install -y ... && rm -rf /var/lib/apt/lists/*in one RUN to avoid caching stale package lists - Best practices: Use specific base image tags (not
:latest),COPY --chownfor non-root user,HEALTHCHECKinstruction,LABELfor metadata,EXPOSEfor documentation,ENTRYPOINT+CMDpattern for flexible default commands,.dockerignorefor build context size
Docker Compose
- Service definition:
image(build locally or pull),build(context + Dockerfile + args),ports,volumes,environment,depends_on,healthcheck,restart,networks - Production concerns: Named volumes (not bind mounts for persistent data), network isolation (separate frontend/backend/db networks), restart policies (
unless-stopped), CPU/memory limits (deploy.resources), logging driver (json-filewith rotation, orjournald) - Multi-file compose:
docker-compose -f docker-compose.yml -f docker-compose.prod.yml configfor override layers,docker-compose.yml(base) +docker-compose.override.yml(dev, gitignored) by convention
Container Registries
- Registry operations:
docker tag,docker push,docker pull, authentication viadocker loginor credential helpers - Platforms: Docker Hub, GitHub Container Registry (GHCR), GitLab Container Registry, Amazon ECR, Google Artifact Registry, Azure Container Registry, self-hosted (Harbor, distribution)
- Tagging strategies:
git-shafor traceability,semverfor releases,latestas convenience pointer only; avoid:prod,:staging— use environment-specificvalues.yamlorchestration instead
Container Networking
- Network drivers:
bridge(default, per-host),host(no network isolation, best performance),overlay(swarm/multi-host),macvlan(physical IP per container),none(loopback only) - DNS resolution: Docker's embedded DNS (service name resolution for Compose, container name for custom networks),
--dnsand--dns-searchoverrides - Common issues: DNS caching, port conflicts on host, container-to-container vs external access, published port binding to all interfaces by default (use
127.0.0.1:PORT:PORTto restrict)
Pitfalls
- Build context bloating:
.dockerignoreis easy to forget. A node_modules-heavy context can send hundreds of MB to the Docker daemon every build. Always add.dockerignore. - Cache invalidation: A
COPY . .after adding one file invalidates every subsequent layer. Structure Dockerfiles so the most volatile content comes last. - Zombie processes without init: Containers run PID 1 by default. Signals don't propagate. Use
tinior--initflag for proper signal handling and zombie reaping. - Permission mismatches with bind mounts: Host UID/GID may not match container user. Use
DockerfileUSER directive consistently and consider--userflag for one-off debug containers. - Unpinned base images:
FROM node:latestbreaks tomorrow. Pin to SHA256 digest or specific semver:FROM node:20-slim@sha256:...