nylegarcia/ai-skills/docker-optimize
Claude Code and agent skill by nylegarcia/ai-skills
- community
Checking that the SKILL.md file is still in the repository.
Optimize Docker images for size, build speed, and security via multi-stage builds, layer-cache ordering, minimal base images, BuildKit cache mounts, and .dockerignore. Use when a Docker image is too large or slow to build, when reviewing or writing a Dockerfile, or when user mentions "optimize docker", "shrink image", "docker build slow", or "reduce image size".
Install
npx vskill@latest install nylegarcia/ai-skills/docker-optimizeSKILL.md
The same SKILL.md was published earlier as nylegarcia/ai-skills/docker-optimize.
Docker Optimize
Audit and rewrite Dockerfiles so images are small, builds are fast (cache-friendly), and containers run securely. Always measure before and after so the improvement is provable.
Workflow
1. Measure the baseline
If dive is available, use dive app:before to inspect wasted bytes per layer.
2. Audit the Dockerfile
Run droast Dockerfile if available (opinionated Rust linter, catches most of the below). Then check, in order of impact:
- [ ] Base image: default to
-slim, not Alpine. Alpine's musl libc forces native deps (Python wheels, C-based Node modules) to compile from source — slower builds and runtime breakage for a negligible size win. Full-fat images (node,python,ubuntu) only in build stages, if at all. - [ ] Pinned by digest: tags are mutable —
node:22-slimcan point at a different build tomorrow. PinFROM node:22-slim@sha256:...for immutable, reproducible builds. - [ ] Multi-stage build: are build tools (compilers, dev dependencies, SDKs) present in the final image? They shouldn't be.
- [ ] Layer order: are dependency manifests (
package.json,requirements.txt,go.mod) copied and installed beforeCOPY . .? Least-changing instructions first — layer order is your cache strategy. - [ ] `.dockerignore`:
COPY . .is a fine pattern if.dockerignoreis thorough. Treat it like.gitignore: exclude.git,node_modules, build output, logs, secrets/env files. Prefer this over sprawling, hard-to-maintain lists of specificCOPYcommands. - [ ] RUN hygiene: package-manager caches cleaned in the same layer (
apt-get clean,--no-cache-dir,rm -rf /var/lib/apt/lists/*)? A laterrmin its own layer removes nothing from image size. Lockfile-respecting installs (npm ci, notnpm install)? - [ ] BuildKit cache mounts:
RUN --mount=type=cache,...for package-manager caches so rebuilds don't redownload? - [ ] Signals & PID 1:
CMD/ENTRYPOINTin exec (JSON array) form so SIGTERM reaches the app, not a wrapping shell? An init (tini/docker run --init) if the app spawns child processes? - [ ] CI caching: builds in CI export/import cache (
--cache-to/--cache-fromwith a registry orghabackend)? Without it, every CI build is a cold build. - [ ] Security: runs as non-root
USER? No secrets inCOPY/ENV/build args?HEALTHCHECKwhere relevant?
3. Rewrite
Apply the canonical shape — build stage with all tooling, minimal runtime stage that copies only artifacts:
Language-specific templates (Node, Python, Go, Java) and advanced techniques: see REFERENCE.md.
4. Verify
Re-run droast Dockerfile until clean. Report the before/after size and build-time delta. If the app has tests, run them against the new image.
Rules of thumb
- Order instructions least-changing → most-changing; a changed layer invalidates every layer after it.
- One logical concern per
RUN; chain with&&and clean up in the same layer. - Build stage:
COPY . .with a strict.dockerignore. Runtime stage:COPY --from=buildonly the artifacts. - Slim over Alpine (glibc vs musl); distroless over
scratchfor the runtime stage — same tiny/no-shell security profile, but ships runtime basics like CA root certificates. - Pin base images by digest, never bare
latestor a mutable tag alone. - Never bake secrets into layers — use
RUN --mount=type=secretfor build-time credentials. - One process per container is a guideline, not a law: if the app genuinely needs e.g. an Nginx proxy in front and you're not on Kubernetes, running both under
supervisordin one container is fine.
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/.archived/docker-optimize/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/nylegarcia/ai-skills/docker-optimize)