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

Fill in the details

Paste the complete Dockerfile you want to optimize and harden.

Briefly describe the workload (e.g., web server, API, background worker, CLI tool).

Specify the target base image if known (e.g., node:18-alpine, python:3.11-slim).

Indicate what changes most often to optimize layer caching (e.g., app code changes daily, dependencies rarely).

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

  1. Fill in the fields above. Required ones are marked with *.
  2. Check the prompt preview: your answers appear highlighted in place.
  3. Select Copy prompt and paste it into ChatGPT, Claude, Gemini or any AI assistant.
See all Software Development & Coding prompts