Skip to main content

Claude Prompt to Optimize Docker Image Layers

Analyze and optimize Docker image layers to cut build time, shrink image size, and raise cache hit rates in your CI/CD pipeline.

Vul de plaatshouders in

Edit the values, then copy your finished prompt.

Jouw Prompt
prompt.txt

                                

What this prompt does

This prompt has the AI analyze and optimize your Dockerfile for [language]/[framework] to shrink image size and improve build cache efficiency. You provide the [current_size] and [build_time], and it reorders layers around your [change_frequency], separates dependency installation from source copying for [package_manager], strips unnecessary files, compares [current_base] against alpine/distroless/scratch, applies build-arg and cache-mount tricks for [cacheable_operations], advises on squashing, and produces a before/after comparison aiming under [target_size].

The structure works because cache efficiency depends almost entirely on ordering: instructions that change rarely must come before instructions that change often. By telling the model what [change_frequency] looks like — source code changing constantly while dependencies change weekly — it can place the dependency layer above the source copy so most builds reuse the cache. Comparing base images explicitly forces the size-versus-compatibility trade-off into the open instead of defaulting to a bloated base. Anchoring the request to your real [current_size] and [build_time] also gives the model a concrete starting point, so its before/after numbers are grounded rather than hypothetical.

When to use it

  • Your builds have gotten slow and you suspect poor layer ordering is wrecking the cache.
  • An image has ballooned in size and you want a layer-by-layer breakdown of where the weight is.
  • You want to evaluate moving from a full base image to alpine, distroless, or scratch.
  • You need the optimal COPY strategy to separate [package_manager] dependencies from application source.
  • You are paying for slow CI and want cache-friendly ordering plus cache mounts for [cacheable_operations].
  • You have a concrete [target_size] to hit and want a measurable before/after.

Example output

Expect a rewritten Dockerfile with reordered instructions, a separated dependency layer, a multi-stage build that drops dev dependencies and build tools from the final image, and a base-image recommendation with size and security trade-offs spelled out. Alongside it you get a before/after table showing layer-by-layer size reduction and an estimate of how close the result lands to [target_size].

Pro tips

  • Describe [change_frequency] accurately; the entire layer-ordering strategy hinges on which instructions change often versus rarely.
  • Separate the dependency install from the source COPY for [package_manager] so editing a source file does not invalidate the dependency cache.
  • Treat the base-image choice as case-by-case: distroless and scratch shrink size and attack surface but remove the shell and tools you may need for debugging.
  • Use --mount=type=cache for [cacheable_operations] like package installs so you stop re-downloading the same artifacts every build.
  • Be skeptical of squashing; combining RUN steps cleans up intermediate files but can hurt cache reuse, so squash only where the layers truly belong together.
  • Verify the real [current_size] and [build_time] before and after so the before/after comparison reflects your environment, not an assumption.
  • Drop dev dependencies, tests, and docs from the final stage of your [framework] build; they are common stowaways that inflate the image without ever running in production.

Frequently Asked Questions

Will this make my Docker builds faster or just smaller?
Both — the prompt targets build cache efficiency through layer reordering and cache mounts, and image size through base-image choice and multi-stage cleanup. Faster builds come mostly from cache hits, while smaller images come from dropping dev dependencies and picking a leaner base.
How does layer ordering improve cache hits?
Instructions that change rarely are placed before instructions that change often, so editing source code does not invalidate the dependency layer. You drive this by describing `[change_frequency]` accurately, which tells the model where to put the dependency install relative to the source COPY.
Should I switch to alpine, distroless, or scratch?
The prompt compares all three against your `[current_base]` with size and security trade-offs, but the right choice is case-by-case. Distroless and scratch shrink size and attack surface but remove the shell and debugging tools, so weigh that against your operational needs.
Does it guarantee my image will hit the target size?
No — it optimizes toward `[target_size]` and shows a before/after breakdown, but the achievable size depends on your `[language]`, `[framework]`, and required dependencies. Some stacks simply have a larger irreducible footprint than others.
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.

Meer in Docker & Kubernetes 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