Analyzes and optimizes Dockerfiles to reduce image size, improve build time, and enhance security. Use when optimizing Docker images, reducing build times, or improving Dockerfile structure.
View on GitHubarmanzeroeight/fastagent-plugins
docker-toolkit
January 21, 2026
Select agents to install to:
npx add-skill https://github.com/armanzeroeight/fastagent-plugins/blob/main/plugins/docker-toolkit/skills/dockerfile-optimizer/SKILL.md -a claude-code --skill dockerfile-optimizerInstallation paths:
.claude/skills/dockerfile-optimizer/# Dockerfile Optimizer
Optimize Docker images for size, speed, and security.
## Quick Start
Analyze current Dockerfile:
```bash
docker build -t myapp .
docker images myapp
# Note the size, then optimize
```
## Instructions
### Step 1: Analyze Current Dockerfile
Review for optimization opportunities:
- Base image choice
- Layer structure
- Build dependencies
- Caching strategy
- Security practices
### Step 2: Optimize Base Image
**Choose minimal base**:
```dockerfile
# Before: Large base
FROM ubuntu:22.04 # ~77MB
# After: Minimal base
FROM alpine:3.18 # ~7MB
# or
FROM node:18-alpine # ~170MB vs node:18 ~990MB
```
**Use specific tags**:
```dockerfile
# Bad: Unpredictable
FROM node:latest
# Good: Specific version
FROM node:18.17-alpine3.18
```
### Step 3: Implement Multi-Stage Builds
**For compiled languages**:
```dockerfile
# Before: Single stage
FROM golang:1.21
WORKDIR /app
COPY . .
RUN go build -o app
CMD ["./app"]
# Result: ~800MB
# After: Multi-stage
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o app
FROM alpine:3.18
COPY --from=builder /app/app /app
CMD ["/app"]
# Result: ~15MB
```
### Step 4: Optimize Layer Caching
**Order by change frequency**:
```dockerfile
# Bad: Code changes invalidate all layers
FROM node:18-alpine
COPY . .
RUN npm install
CMD ["node", "server.js"]
# Good: Dependencies cached separately
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
CMD ["node", "server.js"]
```
### Step 5: Combine and Minimize Layers
**Reduce layer count**:
```dockerfile
# Before: Multiple layers
RUN apt-get update
RUN apt-get install -y curl
RUN apt-get install -y git
RUN apt-get clean
# After: Single layer
RUN apt-get update && \
apt-get install -y curl git && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
```
### Step 6: Use .dockerignore
Create `.dockerignore`:
```
node_modules
npm-debug.log
.git
.gitignore
README.md
.env
.DS_Store
*.md
.vscode
.idea
dist
b