Docker optimize

From weisser-dev/awesome-opencode · View interactive version

Docker optimize is an AI agent skill from the GitHub repository weisser-dev/awesome-opencode. Analyze Dockerfile and produce optimized version with multi-stage builds, layer caching, minimal base, and security hardening

How to use this skill

Skill file

What I do

When to use me

Use this skill when you need to:

Process

  1. Analyze: Read the existing Dockerfile and .dockerignore
  1. Optimize base image: Select the smallest viable base
  1. Implement multi-stage build: Separate build and runtime

```dockerfile

# Build stage

FROM node:20-alpine AS build

WORKDIR /app

COPY package*.json ./

RUN npm ci --production=false

COPY . .

RUN npm run build

# Production stage

FROM node:20-alpine AS production

WORKDIR /app

COPY --from=build /app/dist ./dist

COPY --from=build /app/node_modules ./node_modules

USER node

EXPOSE 3000

CMD ["node", "dist/index.js"]

```

  1. Maximize layer caching: Order instructions by change frequency
  1. Security hardening: Apply container security best practices
  1. Validate: Verify the optimized image

Anti-Pattern Checklist

Optimization Targets

| Metric | Goal |

|--------|------|

| Image size | Reduce by 50%+ from naive build |

| Build time (cached) | Under 30 seconds for source-only changes |

| Security | No critical/high CVEs in base image |

| Layers | Minimize total layer count |

Language-Specific Patterns

Java

FROM eclipse-temurin:21-jdk-alpine AS build
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn package -DskipTests

FROM eclipse-temurin:21-jre-alpine
COPY --from=build /app/target/*.jar app.jar
USER 1001
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

Go

FROM golang:1.22-alpine AS build
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /server .

FROM gcr.io/distroless/static
COPY --from=build /server /server
USER nonroot
EXPOSE 8080
ENTRYPOINT ["/server"]

Rules

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.