dockerfile-optimize
Skill9 starsChanged 5 months agoApache-2.0View on GitHub
Authors and optimizes Dockerfiles for small, secure, fast-building container images: multi-stage builds, minimal/distroless bases, layer caching, non-root users, and .dockerignore. Triggers when writing or reviewing a Dockerfile, shrinking image size, fixing slow builds, or hardening a container image.
- Reads credentials
- Deletes or force-pushes
- Installs packages
In this repository147 other files it gives its agents.
- second-brain/AGENTS.mdAGENTS.md
- second-brain/CLAUDE.mdCLAUDE.md
- agent-tool-mcp-builderSkill
- api-design-reviewSkill
- async-concurrency-correctnessSkill
- audit-accessibility-wcagSkill
- audit-license-complianceSkill
- audit-technical-seoSkill
- auth-jwt-sessionSkill
- author-codemodSkill
- brainstorm-designSkill
- build-audit-loggingSkill
- build-cdc-streaming-pipelineSkill
- build-cli-toolSkill
- build-data-tableSkill
- build-etl-pipelineSkill
- build-form-validationSkill
- build-native-mobile-uiSkill
- build-office-docsSkill
- build-offline-first-syncSkill
- build-react-componentSkill
- build-realtime-channelSkill
- build-spreadsheetSkill
- build-vector-searchSkill
- caching-strategySkill
- cicd-pipeline-authorSkill
- cloud-cost-optimizeSkill
- code-commentsSkill
- code-reviewSkill
- compose-local-dev-stackSkill
- configure-bundler-buildSkill
- configure-dns-tlsSkill
- configure-reverse-proxy-lbSkill
- configure-security-headers-cspSkill
- contract-testingSkill
- datetime-timezone-correctnessSkill
- db-migration-safetySkill
- debug-ci-pipeline-failureSkill
- debug-flaky-testsSkill
- debug-frontend-browserSkill
- debug-root-causeSkill
- defend-llm-prompt-injectionSkill
- deliver-webhooksSkill
- dependency-upgradeSkill
- deploy-releaseSkill
- design-api-paginationSkill
- design-authorization-modelSkill
- design-backup-dr-recoverySkill
- design-event-sourcing-cqrsSkill
- design-multi-tenancySkill
- design-protobuf-grpc-serviceSkill
- design-relational-schemaSkill
- design-search-index-infraSkill
- design-state-machineSkill
- design-token-systemSkill
- diff-table-paritySkill
- distributed-locks-leasesSkill
- encrypt-sensitive-dataSkill
- error-messageSkill
- estimate-workSkill
- The first 60 shown.
The file
Files
When to Use
ใช้ skill นี้เมื่อ:
- เขียน Dockerfile ใหม่ หรือ review Dockerfile ที่มีอยู่
- image ใหญ่เกินไป (เช่น >500MB สำหรับ app เล็ก) — ต้องลด size
- build ช้าทุกครั้งเพราะ cache invalidate ตลอด
- harden container ก่อน deploy (non-root, drop caps, read-only fs)
- migrate single-stage → multi-stage หรือ → distroless
อย่าใช้เมื่อ: ปัญหาอยู่ที่ runtime orchestration (k8s manifests, compose networking) ไม่ใช่ตัว image เอง
Steps
- อ่านบริบทก่อนแก้ —
cat Dockerfile .dockerignore+ ดู lockfile (package-lock.json/go.mod/requirements.txt/pom.xml) เพื่อรู้ภาษา/runtime. รันdocker images <name>เช็ค size ปัจจุบันเป็น baseline. ถ้ามี image อยู่แล้ว:docker history <image> --no-trunc | sort -k1หา layer ที่ใหญ่ที่สุดก่อน - Multi-stage split — แยก
builder(มี compiler/dev deps/SDK) ออกจากruntime(มีแค่ binary/artifact + runtime deps). final stage ต้องCOPY --from=builderเฉพาะ output ที่จำเป็น เช่น:- Go: copy แค่ binary → runtime ใช้
scratchหรือdistroless/static - Node: build ใน
node:20→ copydist/+node_modules(prod เท่านั้น) → runtimenode:20-slimหรือdistroless/nodejs20 - Python: build wheels ใน builder →
pip install --no-indexใน runtime จาก wheels - ตั้งชื่อ stage ชัด (
AS builder,AS runtime) — ห้ามมี stage ลอยที่ไม่ถูก copy
- Go: copy แค่ binary → runtime ใช้
- เลือก + pin base image — เลือกตามลำดับ:
distroless(no shell, เล็กสุด, ปลอดภัยสุด) >alpine(มี shell, glibc-incompat เสี่ยง) >-slim(debian, glibc ครบ). pin ด้วย digest ไม่ใช่ tag ลอย:
หา digest:FROM node:20-slim@sha256:<digest>docker buildx imagetools inspect node:20-slim. tag เปลี่ยนเงียบได้ digest ไม่เปลี่ยน → reproducible build - Layer ordering + cache mounts — เรียงจาก "เปลี่ยนน้อย → เปลี่ยนบ่อย": copy manifest+lockfile ก่อน install, แล้วค่อย
COPY . .:
ใช้COPY package.json package-lock.json ./ RUN --mount=type=cache,target=/root/.npm npm ci COPY . .--mount=type=cache(BuildKit) สำหรับ package cache: npm/root/.npm, pip/root/.cache/pip, go/root/.cache/go-build+/go/pkg/mod, apt/var/cache/apt. รวมRUN apt-get update && apt-get install -y --no-install-recommends ... && rm -rf /var/lib/apt/lists/*ใน RUN เดียว - Non-root + harden — สร้าง user แล้วสลับก่อน CMD:
distroless ใช้RUN groupadd -r app && useradd -r -g app -u 10001 app USER 10001USER nonrootหรือ:nonroottag. เพิ่มHEALTHCHECK, ใช้ exec-formCMD ["app"](ไม่ใช่ shell-form), ตั้งWORKDIRที่ user เขียนได้. caps/read-only fs บังคับตอน runtime:--read-only --cap-drop=ALL --security-opt=no-new-privileges(เขียนไว้ใน compose/k8s + comment ใน Dockerfile) - .dockerignore + ลบ build artifacts — สร้าง/แก้
.dockerignoreตัด.git,node_modules,*.md,.env*,dist,__pycache__,target,coverage,*.log,.DS_Store, secrets. ตรวจ final stage ห้ามมี: compiler, package-manager cache,.env, private key,.git. secret ที่ใช้ตอน build → ใช้RUN --mount=type=secret,id=...ไม่ใช่ARG/COPY(ARG ติดใน history) - Scan + วัด diff — build แล้ว scan:
รายงาน before/after: size (MB), build time (cold + warm cache), CVE count. แก้ HIGH/CRITICAL ที่มาจาก base image โดยขยับ base version ก่อนdocker build -t app:new . trivy image --severity HIGH,CRITICAL app:new # หรือ grype app:new docker images app:new --format '{{.Size}}'
Common Errors
- ARG เก็บ secret ติด history —
ARG TOKEN+docker historyเห็นค่าได้ แม้ลบใน layer หลัง. ใช้RUN --mount=type=secretเสมอ - alpine + glibc binary พัง — alpine ใช้ musl; prebuilt wheel/native module ที่ link glibc จะ segfault หรือ
Error loading shared library. Python/Node native deps → ใช้-slim(debian) ปลอดภัยกว่า COPY . .ก่อน install — ทำให้ทุก source change invalidate layer install → build ช้าทุกครั้ง. ต้อง copy lockfile ก่อนเสมอ- cache mount ไม่ทำงาน — ต้องเปิด BuildKit:
DOCKER_BUILDKIT=1หรือdocker buildx build. ไม่งั้น--mountถูก ignore เงียบ - distroless แล้ว debug ไม่ได้ — ไม่มี shell,
docker exec ... shจะ fail. ใช้:debugtag ตอน dev หรือctr/ephemeral debug container แทน - USER แล้วเขียน WORKDIR ไม่ได้ — non-root เขียน path ที่ root เป็นเจ้าของไม่ได้ →
EACCES.chownให้ user หรือCOPY --chown=app:app latest/floating tag — build ซ้ำได้ผลต่างกัน + CVE สุ่มเข้ามา. pin digestapt-get installแยก RUN จาก update — apt cache layer เก่า → ติดตั้ง package เวอร์ชันเก่า/พัง. รวมupdate && install && rm -rf listsใน RUN เดียว- ลืม
--no-install-recommends/npm ciแทนnpm install— ดึง deps เกินจำเป็น, lockfile ไม่ตรง
Verify
ก่อนถือว่าเสร็จ ต้องผ่านทุกข้อ (มีหลักฐานรันจริง ไม่ใช่ "แก้แล้ว"):
docker buildผ่าน (cold cache) — บันทึก size + time- build ซ้ำ (warm cache) เร็วขึ้นชัด — source change เล็กไม่ trigger re-install
docker run app:new idแสดง uid ≠ 0 (non-root จริง)docker history app:new— final layers ไม่มี compiler/secret/.git/package cachetrivy image --severity HIGH,CRITICAL= 0 (หรือ document ตัวที่แก้ไม่ได้ + เหตุผล)- container start + health check ผ่าน (
docker runแล้ว app ตอบได้จริง) - size diff: รายงาน before → after เป็น MB + % ลด
- base image pin ด้วย
@sha256:digest
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

