Docker
When to Use
- Writing or reviewing a Dockerfile
- Setting up Docker Compose for local development
- Hardening an existing image (non-root, read-only filesystem, minimal base)
- Scanning an image or Compose config for security issues
- Selecting the right base image for a language/framework
Quick Start
/docker dockerfile # Write or improve a Dockerfile for this service
/docker compose # Generate or fix a Docker Compose stack
/docker harden # Harden an existing Dockerfile against security risks
/docker scan # Audit Dockerfile and Compose for security issues
No argument? Detect context automatically and suggest the most relevant mode.
Context
DETECTED PROJECT FILES:
!`ls Dockerfile* docker-compose* compose* .dockerignore 2>/dev/null; ls *.go go.mod package.json Cargo.toml pyproject.toml requirements.txt 2>/dev/null || echo "none detected"`
CURRENT DOCKERFILE:
!`cat Dockerfile 2>/dev/null || echo "No Dockerfile found"`
CURRENT COMPOSE:
!`cat docker-compose.yml compose.yml docker-compose.yaml compose.yaml 2>/dev/null | head -80 || echo "No Compose file found"`
Mode: dockerfile
Write or improve a multi-stage Dockerfile for the detected stack.
Steps:
- Detect language: Go (
go.mod), Node.js (package.json), Python (pyproject.toml/requirements.txt), Rust (Cargo.toml) - Select the correct asset template from
assets/dockerfiles/ - Customize for the project's entry point, build commands, and port
- Apply all items from the hardening checklist automatically
- Verify the result passes the security checklist in
references/dockerfile-security-checklist.md
Multi-stage pattern (required for production images):
# Stage 1: Build
FROM golang:1.24-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /app/server ./cmd/server
# Stage 2: Runtime
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=builder /app/server /server
EXPOSE 8080
USER nonroot:nonroot
ENTRYPOINT ["/server"]
Base image selection:
| Language | Dev/Debug | Production |
|---|---|---|
| Go | golang:1.24-alpine | gcr.io/distroless/static-debian12:nonroot |
| Node.js | node:22-alpine | node:22-alpine (strip devDeps) |
| Python | python:3.13-slim | python:3.13-slim (venv copy) |
| Rust | rust:1.80-alpine | gcr.io/distroless/cc-debian12:nonroot |
Layer ordering (cache efficiency):
- Base image
- System dependencies (
apt-get,apk add) - Dependency manifests (go.mod, package.json, requirements.txt)
- Dependency install (
go mod download,npm ci,pip install) - Source code (
COPY . .) - Build (
RUN go build,RUN npm run build)
Mode: compose
Generate or fix a Docker Compose stack for local development.
Steps:
- Identify required services from codebase (database connections, cache usage, queue consumers)
- Select matching template from
assets/compose/ - Apply health checks with
condition: service_healthyondepends_on - Add named volumes for persistent data (databases); bind mounts only for source code
- Generate
compose.override.ymlfor dev-only settings (volume mounts, debug ports) - Optionally generate
compose.ci.ymlfor CI service containers
Health check patterns:
services:
postgres:
image: postgres:16-alpine
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 10
start_period: 10s
app:
depends_on:
postgres:
condition: service_healthy
Profile usage (optional services):
services:
kafka:
image: confluentinc/cp-kafka:7.7.0
profiles: ["kafka", "full"]
Start with: docker compose --profile kafka up
Mode: harden
Apply security hardening to an existing Dockerfile.
Hardening checklist (applied automatically):
- Multi-stage build — no build tools in runtime image
- Non-root
USER— use named user, not numeric UID only -
--no-cacheonapk/apt-getinstall - Pin base image to digest or specific minor version (not
:latest) - No secrets in
ENVorARG— use--mount=type=secret(BuildKit) -
.dockerignoreexcludes.git,.env,node_modules, test data, credentials -
COPY --chownto set file ownership in one layer -
HEALTHCHECKinstruction present - Read-only root filesystem note (enforce at runtime via
--read-onlyorsecurityContext) - Drop all capabilities at runtime (document in comments)
Secret mounting (BuildKit):
# syntax=docker/dockerfile:1
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc \
npm ci --only=production
Build: docker build --secret id=npmrc,src=.npmrc .
Mode: scan
Audit the Dockerfile and Compose config for security and quality issues.
Steps:
- Review Dockerfile against
references/dockerfile-security-checklist.md - Review Compose against
references/compose-patterns.mdsecurity section - Check for Hadolint violations (if installed):
hadolint Dockerfile - Check for Trivy findings (image-level — but note: CI scanning is owned by
cicd-pipelineskill) - Report findings using severity levels:
- [CRITICAL]: secrets in ENV, running as root with writable filesystem
- [HIGH]: no multi-stage build, pinned to
:latest, missing.dockerignore - [MEDIUM]: missing HEALTHCHECK, no layer cache ordering, devDependencies in prod image
- [LOW]: minor label improvements, cosmetic ordering
Scope
In scope:
- Dockerfile authoring and multi-stage patterns
- Docker Compose local development stacks
- Image security hardening
- Base image selection
Out of scope:
- Trivy / image scanning in CI pipelines (use
cicd-pipelineskill) - Docker build/push Actions (use
cicd-pipelineskill) - Kubernetes manifests and Helm charts (use
kubernetesskill)
References
- references/dockerfile-best-practices.md — Layer ordering, cache efficiency, multi-stage patterns
- references/dockerfile-security-checklist.md — Hardening checklist with rationale
- references/compose-patterns.md — Health checks, profiles, overrides, CI usage
- references/image-scanning.md — Scanning tools, severity mapping, SBOM
- assets/dockerfiles/go.Dockerfile — Go multi-stage template
- assets/dockerfiles/node.Dockerfile — Node.js multi-stage template
- assets/dockerfiles/python.Dockerfile — Python multi-stage template
- assets/dockerfiles/rust.Dockerfile — Rust multi-stage template
- assets/compose/web-db-cache.yml — Web + Postgres + Redis template
- assets/compose/compose.override.dev.yml — Dev override template
