dockerize
Write a multi-stage Dockerfile — distroless or minimal base, build/runtime split, deterministic layer order, non-root user, no secrets baked in.
- License 许可
- MIT
- 最近更新
- 2026-10-29
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- dockerize
- description
- Write a multi-stage Dockerfile — distroless or minimal base, build/runtime split, deterministic layer order, non-root user, no secrets baked in.
02 资源文件 1
- 参考文档routing.yaml343 B
03 SKILL.md 正文
dockerize
Write a Dockerfile that's small, fast to rebuild, and safe to run.
Mode: analytical — judgment inside a fixed frame. Keep the output shape; every verdict cites its evidence; unknowns are marked [TBD] or [VERIFY], never filled in.
When to use
- A new service needs a container image.
- An existing Dockerfile is bloated, slow, or runs as root.
Inputs
- Language + framework.
- Build artifact shape (compiled binary, JS bundle, Python module).
- Runtime needs (does it need
bash,curl,ca-certificates? Often: no.).
Project localization
Before the procedure, resolve the project snapshot —
${POLYMATH_DATA_DIR:-$HOME/.polymath/data}/polymath-core/project-context.json
(ignore it if its _meta.source belongs to another repo; absent → read
.polymath/project.yaml directly; neither → built-in defaults). Then apply
(contract: polymath-core:project-context):
conventions_docs: read roledeployment— approved base images, registries, and hosting targets there override the generic choices below.
Procedure
- Multi-stage always. Stage 1 builds; stage 2 runs.
- Base images:
- Builder stage: the official language image with the toolchain (e.g.
golang:1.22-alpine,node:20-alpine,python:3.12-slim). - Runtime stage: pick from smallest to most flexible:
scratch— Go static binaries, Rust musl. Cheapest.gcr.io/distroless/static/base/cc— no shell, no package manager. Recommended for compiled languages.-slim(debian-slim, python-slim) — for languages needing libc + a tiny userspace.-alpine— only if you've validated musl compatibility (Python wheels often don't).
- Builder stage: the official language image with the toolchain (e.g.
- Layer order — most-cacheable first:
- Copy dependency manifests (
go.mod,package.json,requirements.txt). - Resolve dependencies.
- Copy source.
- Build.
- Copy dependency manifests (
- User — runtime stage runs as a non-root user with a fixed UID (e.g.
USER 65532:65532). Distroless hasnonrootbaked in. - No secrets in
ENVorARG. Use BuildKit secret mounts at build time; runtime secrets via your secret manager. - HEALTHCHECK if the orchestrator doesn't supply one. Keep it cheap (< 1s, no DB calls).
- Labels —
org.opencontainers.image.source,.version,.revision. Lets the registry tie images back to a commit.
Output
# syntax=docker/dockerfile:1.7
FROM golang:1.22-alpine AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go mod download
COPY . .
RUN --mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/refund ./cmd/refund
FROM gcr.io/distroless/static-debian12:nonroot
WORKDIR /
COPY --from=build /out/refund /refund
USER 65532:65532
ENTRYPOINT ["/refund"]
LABEL org.opencontainers.image.source="https://github.com/example/refund-service"
LABEL org.opencontainers.image.version="0.1.0"
Quality bar
- Runtime image < 100 MB for a Go service; < 200 MB for Node; < 250 MB for Python-slim.
USERis set; not running as root.- No
apt-get install curl bash"just in case". .dockerignoreexcludes.git,node_modules, build outputs.
Anti-patterns to avoid
- Single-stage build copying source + toolchain into prod image.
FROM ubuntu:latestfor a Go binary.RUN apt-get update && apt-get installwithout--no-install-recommendsand cleanup.- Baking secrets via
ARG(they remain in the image layer history).
04 同仓库更多 Skills
runbook
Write or refresh an operational runbook for an alert/symptom; structured as TL;DR + Pre-checks + Steps + Verification + Rollback + Escalation.
rfc
Draft a lightweight design RFC to docs/rfcs/<slug>.md; Summary, Motivation, Detailed design, Drawbacks, Alternatives, Unresolved questions. A proposal to debate, not a recorded decision (adr).
editorial-pass
Review and tighten prose in a Markdown artifact for a target tone (terse, formal, friendly) without dropping information; preserve quotations and load-bearing constraints.
architecture-review-facilitation
Run an architecture review as a process — entry criteria, reviewer checklist, decision log, and follow-up actions — pairing the design artifact with a red-team pass.
architecture-doc
Write a C4-style architecture document; System Context + Containers (+ Components when interesting) with mermaid diagrams. Describes structure, not a decision (adr) or proposal (rfc).
adr
Write an Architecture Decision Record (Michael Nygard format) to docs/adrs/NNNN-<slug>.md; frontmatter validated by the ADR artifact schema. Records a made decision, not a proposal to debate (rfc).