Skilllibrary docker-containers
Author Dockerfiles, optimize multi-stage builds, write docker-compose services, configure health checks, reduce image size, and scan images for vulnerabilities. Use when creating or editing Dockerfiles, docker-compose.yml, container runtime configuration, or debugging container build/run issues. Do not use for container orchestration (prefer kubernetes/ECS skills) or application code changes unrelated to containerization.
install
source · Clone the upstream repo
git clone https://github.com/merceralex397-collab/skilllibrary
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/merceralex397-collab/skilllibrary "$T" && mkdir -p ~/.claude/skills && cp -r "$T/14-cloud-platform-devops/docker-containers" ~/.claude/skills/merceralex397-collab-skilllibrary-docker-containers && rm -rf "$T"
manifest:
14-cloud-platform-devops/docker-containers/SKILL.mdsource content
Purpose
Author production-quality Dockerfiles with multi-stage builds, write docker-compose service definitions, optimize image size and layer caching, configure container health checks, manage build arguments and runtime secrets, and scan images for security vulnerabilities.
When to use this skill
- Creating a new Dockerfile or editing an existing one.
- Writing or modifying a
for local development or production.docker-compose.yml - Optimizing a Docker image for size (reducing layers, choosing smaller base images).
- Implementing multi-stage builds to separate build-time and runtime dependencies.
- Adding
instructions or container health-check configurations.HEALTHCHECK - Debugging
failures, layer caching issues, or runtime container errors.docker build - Scanning Docker images for CVEs using
, Trivy, or Snyk.docker scout - Configuring
to exclude unnecessary files from the build context..dockerignore - Setting up Docker BuildKit features (cache mounts, secret mounts, SSH forwarding).
Do not use this skill when
- The task is about container orchestration (Kubernetes deployments, ECS task definitions, Nomad jobs) — prefer orchestration-specific skills.
- The change is purely application code with no Dockerfile or container configuration impact.
- The task involves cloud-provider container services (ECR, GCR, ACR) — prefer
oraws
for registry-specific commands.gcp - The focus is on CI/CD pipeline design — prefer
for deployment strategy.cloud-deploy
Operating procedure
- Identify the containerization target. Determine the application runtime (Node.js, Python, Go, Rust, Java), its dependency installation method, and its build/start commands.
- Choose the base image. Select the smallest suitable base:
variants for minimal size,alpine
variants for Debian compatibility, orslim
for production security. Pin to a specific tag (e.g.,distroless
), never usenode:20-alpine3.19
.latest - Write the .dockerignore. Exclude
,node_modules
,.git
, test files, local env files, and any files not needed in the build context. Place*.md
next to the Dockerfile..dockerignore - Implement multi-stage build. Stage 1 (
): install all dependencies and compile/build the application. Stage 2 (builder
): copy only the built artifacts and production dependencies from the builder stage. This eliminates build tools from the final image.runtime - Optimize layer ordering. Copy dependency manifests (
,package.json
,requirements.txt
) before source code. Rungo.mod
/npm ci
/pip install
as a separate layer so dependency installs are cached when only source code changes.go mod download - Configure build arguments and secrets. Use
for build-time variables (app version, build date). UseARG
(BuildKit) for sensitive build-time values (private registry tokens). Never embed secrets in--mount=type=secret
orENV
commands.RUN - Set the runtime user. Add
andRUN addgroup -S app && adduser -S app -G app
before theUSER app
. Never run containers as root in production.CMD - Add health checks. Add
. For compose, use theHEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD curl -f http://localhost:${PORT}/healthz || exit 1
key withhealthcheck
,test
,interval
, andtimeout
.retries - Write docker-compose.yml. Define services with
(context and Dockerfile path),build
,ports
,environment
(for local development bind mounts),volumes
(withdepends_on
for health-check ordering), andcondition: service_healthy
.networks - Build and test locally. Run
and verify the image size withdocker build -t app:local .
. Run the container and confirm the health check passes withdocker images app:local
.docker inspect --format='{{.State.Health.Status}}' <container> - Scan for vulnerabilities. Run
ordocker scout cves app:local
. Fix critical and high CVEs by updating base images or pinning patched package versions.trivy image app:local - Document image metadata. Add
instructions forLABEL
,org.opencontainers.image.source
, andorg.opencontainers.image.version
.org.opencontainers.image.description
Decision rules
- Use multi-stage builds for every production image — single-stage is only acceptable for simple scripts or development images.
- Use
base images unless the application requires glibc-specific dependencies (in that case, usealpine
).slim - Pin base image tags to specific versions, not
or major-only tags.latest - Use
to transfer only artifacts — never install build tools in the runtime stage.COPY --from=builder - Use
overnpm ci
for deterministic Node.js dependency installation.npm install - If the image exceeds 500MB, investigate — most production images should be under 200MB.
- Use BuildKit (
) for all builds — it enables cache mounts, secret mounts, and parallel stage execution.DOCKER_BUILDKIT=1 - Run containers as non-root unless the application explicitly requires root (and document why).
Output requirements
- Dockerfile — multi-stage, optimized layer order, pinned base image, non-root user, health check.
- docker-compose.yml — service definitions with health checks, proper depends_on ordering, and environment variable configuration.
- .dockerignore — excludes all unnecessary files from the build context.
- Image size report — final image size and base image used.
- Vulnerability scan result — summary of critical/high CVEs found and remediation status.
References
- Dockerfile best practices: https://docs.docker.com/build/building/best-practices/
- Multi-stage builds: https://docs.docker.com/build/building/multi-stage/
- Docker Compose specification: https://docs.docker.com/compose/compose-file/
- BuildKit documentation: https://docs.docker.com/build/buildkit/
- Docker Scout: https://docs.docker.com/scout/
references/preflight-checklist.md
Related skills
— ECR image registry, ECS task definitions, Fargate runtime.aws
— containerized deployment alternatives.vercel
— infrastructure-as-code for container registries and orchestration resources.terraform-iac
— runtime secret injection into containers.secret-management
Anti-patterns
- Using
as the base image tag — breaks reproducibility and caching.latest - Running
withoutapt-get update && apt-get install
and without cleaning the apt cache in the same layer.--no-install-recommends - Copying the entire source tree before installing dependencies — invalidates the dependency cache on every code change.
- Embedding secrets in
instructions — they persist in image layers and are visible viaENV
.docker history - Running as root in production containers.
- Using
whenADD
would suffice —COPY
has implicit tar extraction and URL download behavior that causes surprises.ADD - Ignoring
— large build contexts slow down builds and may leak sensitive files..dockerignore
Failure handling
- If
fails at adocker build
step, check the specific command's exit code and stderr. Common causes: missing package in the base image, network issues duringRUN
/apt-get
, or incorrectnpm install
.WORKDIR - If the image is unexpectedly large, use
to identify which layers contribute the most size. Check for unneeded build tools in the runtime stage.docker history <image> - If health checks fail after container start, verify the application is listening on the expected port and that the health endpoint exists. Check
for startup errors.docker logs <container> - If vulnerability scanning reports critical CVEs in the base image, update to the latest patched tag or switch to a distroless/alpine variant.
- If the task involves orchestration (Kubernetes, ECS, Swarm), redirect to the appropriate orchestration skill.