Skip to main content

Docker Multi-Stage Build Optimizer

Optimize Docker images with multi-stage builds — minimize image size, layer caching, security hardening, and production-ready configurations.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt optimizes a Dockerfile for a [application_type] whose image is currently [current_size]. It produces a multi-stage build (builder to production), a minimal [base_image], layer caching ordered most-to-least changing, security hardening (non-root user, no shell, read-only filesystem), a .dockerignore for [ignore_patterns], a health check, graceful-shutdown signal handling, build arguments for [build_args], a dev docker-compose with hot reload, and a production compose with [production_services], targeting under [target_size].

The structure works because it attacks size and attack surface at the same time. A multi-stage build leaves build tools behind in the builder stage, and a minimal, non-root, shell-less image shrinks both the download and what an attacker can do inside it. Ordering layers from least to most volatile maximizes cache hits, so rebuilds stay fast. The Hadolint config and [scan_tool] vulnerability scan are guardrails that keep the gains from regressing on the next change.

When to use it

  • A Docker image that has ballooned to [current_size] and is slowing your pipeline.
  • Hardening a container before it goes to production (non-root, read-only, no shell).
  • Setting up a clean dev-versus-production compose split with hot reload locally.
  • Reducing image pull time and cold-start latency for a [application_type] service.
  • Adding Hadolint and [scan_tool] scanning so image quality doesn't regress.
  • Establishing a health check and graceful shutdown for orchestrated deployments.

Example output

You get an optimized multi-stage Dockerfile built on [base_image], a matching .dockerignore covering [ignore_patterns], a dev docker-compose.yml with hot reload, a production compose wiring [production_services], a Hadolint config, a build-time benchmark note, and a vulnerability-scan step using [scan_tool]. The Dockerfile includes a non-root user, health check, signal handling, and build args for [build_args], aiming under [target_size].

Pro tips

  • Give an accurate [current_size] and a realistic [target_size]; "under 200MB" is achievable for many Node images but not all.
  • Pick a truly minimal [base_image] (alpine or distroless) only if your app's native dependencies support it.
  • Order your COPY steps so dependency manifests come before source, maximizing the layer-cache benefit on rebuilds.
  • Keep [ignore_patterns] thorough; a fat build context (tests, .git, node_modules) silently inflates build time.
  • Run the [scan_tool] step in CI, not just locally, so a vulnerable base image can't slip through unnoticed.
  • Verify the read-only filesystem doesn't break paths your app writes to; mount explicit volumes for those.

Frequently Asked Questions

How much smaller will my image actually get?
It depends on your `[application_type]` and `[base_image]`, but multi-stage builds that drop build tooling plus a minimal base often cut images dramatically, for example from over a gigabyte toward the `[target_size]` you set. Native dependencies that need compilers in the runtime can limit how small you can realistically go.
Is alpine or distroless always the right base image?
Not always. Minimal bases like alpine or distroless shrink size and attack surface, but alpine's musl libc and missing shells can break apps with certain native modules. Choose `[base_image]` based on whether your dependencies build and run cleanly on it; test before committing.
Will the read-only filesystem break my application?
It can if your app writes to disk at runtime, such as caches, temp files, or uploads. The hardening is correct in principle; the fix is to mount explicit writable volumes for those specific paths rather than abandoning the read-only root, which preserves most of the security benefit.
Does it set up scanning so the image stays secure over time?
Yes. The prompt includes a Hadolint config for the Dockerfile and a vulnerability scan with `[scan_tool]`. Run both in CI rather than only locally so a regression, like a newly vulnerable base image, fails the build instead of shipping quietly to production.
What's the point of the two separate docker-compose files?
The dev compose enables hot reload and developer conveniences, while the production compose wires real `[production_services]` like a database, cache, and reverse proxy. Separating them keeps debugging tools and bind mounts out of production while giving local development a fast feedback loop.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in DevOps & Cloud Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support