Dockerfile Optimizer and Hardener
ROLE: Senior DevOps Engineer and container security specialist.
- Category
- Software Development & Coding
- Output
- Code
- Fields
- 4
- software-development
- best-practices
- performance-optimization
- code-fix
- formatting
- risk-mitigation
Prompt
ROLE: Senior DevOps Engineer and container security specialist.
TASK: Analyze, optimize, and harden the provided Dockerfile for Application type running on Base image.
Follow these steps:
1. Parse the Dockerfile and identify the application type, base image, and typical change patterns. If Application type, Base image, or Change frequency are not provided, infer sensible defaults or state assumptions clearly.
2. Restructure into a multi-stage build to minimize the final image size. Strip build tools, caches, and unnecessary packages from the production stage.
3. Optimize layer ordering to maximize Docker build cache hits. Place rarely changing layers (e.g., dependency installation) before frequently changing layers (e.g., application source code), guided by Change frequency or standard best practices.
4. Apply security hardening: create and use a non-root user, pin exact package and image versions, avoid embedding secrets, drop unnecessary Linux capabilities, and use flags like `--no-install-recommends` where applicable.
5. Ensure proper startup configuration: add a `HEALTHCHECK`, set a correct `ENTRYPOINT`/`CMD` pair, and handle OS signals appropriately.
6. Add clear, concise comments explaining each optimization.
CONSTRAINTS:
- Use only the information provided. Do not invent package names, versions, infrastructure details, or external services.
- If any context is missing, ambiguous, or requires validation, list it in an "Open questions" section at the end instead of guessing.
- Keep explanations concise and focused on actionable improvements.
OUTPUT STRUCTURE:
- **Optimized Dockerfile**: A complete, ready-to-use Dockerfile wrapped in a markdown code block. Include inline comments for key changes.
- **Change Summary**: A Markdown table with columns `Optimization`, `Change Made`, and `Reason`.
- **Impact Estimate**: A brief paragraph estimating expected image size reduction and security posture improvements.
- **Open questions**: (Only include if applicable) A short list of missing or ambiguous details that need confirmation.
DOCKERFILE INPUT:
```
Original Dockerfile
```
How to use this prompt
- Fill in the fields above. Required ones are marked with *.
- Check the prompt preview: your answers appear highlighted in place.
- Select Copy prompt and paste it into ChatGPT, Claude, Gemini or any AI assistant.
More Software Development & Coding prompts
- Bug Fix Brief for an AI AgentConvert raw bug reports into precise, unambiguous execution briefs tailored for AI coding agents.
- Caching Strategy DesignerDesign a robust, production-ready caching strategy for the specified feature, endpoint, or data flow.
- Database Schema Designer NormalizedProduce a production-ready, normalized relational schema with precise constraints, justified indexes, and practical migration guidance.
- Production Error Log TriageROLE: You are a senior on-call SRE and incident response engineer specializing in production debugging.
- Bug Diagnosis and FixYou are an expert software engineer and debugging specialist.
- Conventional Commit Message WriterROLE: You are a senior software engineer and version control specialist dedicated to maintaining clean, standardized, and highly readable Git history.