Generate optimized Dockerfile for any project
✓Works with OpenClaudeYou are a Docker expert. The user wants to generate an optimized Dockerfile tailored to their project's tech stack and requirements.
What to check first
- Run
ls -lato identify project root files (package.json, requirements.txt, go.mod, pom.xml, etc.) - Check if a
.dockerignorefile exists; if not, one should be created alongside the Dockerfile - Identify the primary language/framework by examining the project structure and dependency files
Steps
- Detect the project type by examining package.json (Node.js), requirements.txt (Python), go.mod (Go), pom.xml (Java), Gemfile (Ruby), or dockerfile-equivalent indicators
- Choose an appropriate base image version (e.g.,
node:18-alpine,python:3.11-slim,golang:1.21-alpine) - Set the working directory with
WORKDIR /appto establish consistent file paths - Copy dependency files first (package.json, requirements.txt, go.mod) before copying source code to leverage Docker layer caching
- Install dependencies using the language-specific package manager (npm ci, pip install, go mod download, etc.)
- Copy the entire application source code using
COPY . . - Expose the application port (check package.json scripts or main.py for port number, common defaults: 3000, 5000, 8080)
- Define the startup command with
CMDorENTRYPOINT, using the exact command from package.json or language runtime - Add
.dockerignoreto exclude node_modules, .git, .env, and build artifacts to reduce image size
Code
# Detect project type and generate optimized Dockerfile
# Save this as: Dockerfile
# Node.js / Express example (multi-stage for production)
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
FROM node:18-alpine
WORKDIR /app
RUN addgroup -g 1001 -S nodejs && adduser -S nodejs -u 1001
COPY --from=builder --chown=nodejs:nodejs /app/node_modules ./node_modules
COPY --chown=nodejs:nodejs . .
EXPOSE 3000
USER nodejs
CMD ["node", "server.js"]
---
# Python / Flask example (single-stage)
FROM python:3.11-slim
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
ENV FLASK_APP=app.py
CMD ["flask", "run", "--host=0.0.0.0"]
---
# Go example (multi-stage build)
FROM golang:1.21-alpine
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Docker & Kubernetes Skills
Other Claude Code skills in the same category — free to download.
Docker Compose
Create docker-compose.yml for multi-service apps
K8s Deployment
Generate Kubernetes deployment manifests
K8s Service
Create Kubernetes service and ingress configs
Helm Chart
Create Helm chart for application
Docker Multistage
Optimize Docker builds with multi-stage builds
K8s ConfigMap
Create ConfigMaps and Secrets management
Docker Security
Audit and fix Dockerfile security issues
K8s HPA
Set up Horizontal Pod Autoscaler
Want a Docker & Kubernetes skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.