dockerfile-optimizer▌
patricio0312rev/skills · updated Apr 8, 2026
Build optimized, secure, and cache-efficient Docker images following production best practices.
Dockerfile Optimizer
Build optimized, secure, and cache-efficient Docker images following production best practices.
Core Workflow
- Analyze current Dockerfile: Identify optimization opportunities
- Implement multi-stage builds: Separate build and runtime
- Optimize layer caching: Order instructions efficiently
- Minimize image size: Use slim base images and cleanup
- Add security hardening: Non-root user, minimal permissions
- Configure health checks: Ensure container health monitoring
Base Image Selection
Image Size Comparison
| Base Image | Size | Use Case |
|---|---|---|
node:20 |
~1GB | Development only |
node:20-slim |
~200MB | General production |
node:20-alpine |
~130MB | Size-critical production |
gcr.io/distroless/nodejs20 |
~120MB | Maximum security |
Recommendations by Language
# Node.js
FROM node:20-alpine
# Python
FROM python:3.12-slim
# Go
FROM golang:1.22-alpine AS builder
FROM scratch AS runtime # Or gcr.io/distroless/static
# Rust
FROM rust:1.75-alpine AS builder
FROM alpine:3.19 AS runtime
# Java
FROM eclipse-temurin:21-jdk-alpine AS builder
FROM eclipse-temurin:21-jre-alpine AS runtime
Multi-Stage Builds
Node.js Application
# ==================== Build Stage ====================
FROM node:20-alpine AS builder
WORKDIR /app
# Install dependencies first (cache layer)
COPY package.json package-lock.json ./
RUN npm ci --ignore-scripts
# Copy source and build
COPY . .
RUN npm run build
# Prune dev dependencies
RUN npm prune --production
# ==================== Production Stage ====================
FROM node:20-alpine AS production
# Security: Create non-root user
RUN addgroup -g 1001 -S nodejs && \
adduser -S nextjs -u 1001
WORKDIR /app
# Copy only necessary files
COPY /app/node_modules ./node_modules
COPY /app/dist ./dist
COPY /app/package.json ./
# Security: Switch to non-root user
USER nextjs
# Health check
HEALTHCHECK \
CMD node -e "require('http').get('http://localhost:3000/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1))"
EXPOSE 3000
CMD ["node", "dist/index.js"]
Next.js Application
# ==================== Dependencies ====================
FROM node:20-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
# ==================== Builder ====================
FROM node:20-alpine AS builder
WORKDIR /app
COPY /app/node_modules ./node_modules
COPY . .
# Disable telemetry during build
ENV NEXT_TELEMETRY_DISABLED=1
RUN npm run build
# ==================== Runner ====================
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
# Copy static assets
COPY /app/public ./public
# Set correct permissions for prerender cache
RUN mkdir .next && chown nextjs:nodejs .next
# Copy build output
COPY /app/.next/standalone ./
COPY /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
HEALTHCHECK \
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1
CMD ["node", "server.js"]
Python Application
# ==================== Builder ====================
FROM python:3.12-slim AS builder
WORKDIR /app
# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# Create virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# ==================== Production ====================
FROM python:3.12-slim AS production
WORKDIR /app
# Create non-root user
RUN groupadd -r appuser && useradd -r -g appuser appuser
# Copy virtual environment from builder
COPY /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Copy application code
COPY . .
USER appuser
EXPOSE 8000
HEALTHCHECK \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
Go Application
# ==================== Builder ====================
FROM golang:1.22-alpine AS builder
RUN apk add --no-cache git ca-certificates tzdata
WORKDIR /app
# Download dependencies
COPY go.mod go.sum ./
RUN go mod download && go mod verify
# Build
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags="-w -s -X main.version=$(git describe --tags --always)" \
-o /app/server ./cmd/server
# ==================== Production ====================
FROM scratch AS production
# Copy CA certificates for HTTPS
COPY /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY /usr/share/zoneinfo /usr/share/zoneinfo
# Copy binary
COPY /app/server /server
Discussion
Product Hunt–style comments (not star reviews)- No comments yet — start the thread.
general reviewsRatings
4.4★★★★★41 reviews- ★★★★★Ganesh Mohane· Dec 20, 2024
dockerfile-optimizer reduced setup friction for our internal harness; good balance of opinion and flexibility.
- ★★★★★Daniel Verma· Dec 12, 2024
dockerfile-optimizer has been reliable in day-to-day use. Documentation quality is above average for community skills.
- ★★★★★Aditi Yang· Dec 4, 2024
dockerfile-optimizer is among the better-maintained entries we tried; worth keeping pinned for repeat workflows.
- ★★★★★Diya Abbas· Dec 4, 2024
dockerfile-optimizer reduced setup friction for our internal harness; good balance of opinion and flexibility.
- ★★★★★Noor Huang· Nov 23, 2024
Solid pick for teams standardizing on skills: dockerfile-optimizer is focused, and the summary matches what you get after install.
- ★★★★★Camila Haddad· Nov 23, 2024
I recommend dockerfile-optimizer for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.
- ★★★★★Sakshi Patil· Nov 11, 2024
I recommend dockerfile-optimizer for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.
- ★★★★★Sophia Mensah· Nov 3, 2024
We added dockerfile-optimizer from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.
- ★★★★★William Iyer· Nov 3, 2024
Keeps context tight: dockerfile-optimizer is the kind of skill you can hand to a new teammate without a long onboarding doc.
- ★★★★★Sakura Martin· Oct 22, 2024
dockerfile-optimizer fits our agent workflows well — practical, well scoped, and easy to wire into existing repos.
showing 1-10 of 41
1 / 5