---
name: dockerfile-review-and-optimisation
description: "Takes a pasted Dockerfile and returns a smaller, faster-building, non-root version with a comment on each change, a size and time estimate, and the trade-offs you are accepting. Use when a pasted Dockerfile is slow to build, produces an oversized image, runs as root, or needs a quick review before it ships. Do not use for writing a Dockerfile from scratch or debugging a running container. Related topics: Engineering, Technical, Coding."
---

# Dockerfile Review and Optimisation

Takes a pasted Dockerfile and returns a smaller, faster-building, non-root version with a comment on each change, a size and time estimate, and the trade-offs you are accepting.

## When to use

Use this skill when a pasted Dockerfile is slow to build, produces an oversized image, runs as root, or needs a quick review before it ships. Do not use it for writing a Dockerfile from scratch or debugging a running container.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. List every layer that invalidates the cache earlier than necessary, starting with where source is copied relative to the dependency install.
3. Decide whether a multi-stage build removes a toolchain from the final image, and keep single-stage when nothing would be removed.
4. Flag each security issue with its line: root user, secrets in layers, unpinned base images.
5. Rewrite the Dockerfile with a comment on every changed line while keeping runtime behaviour identical.
6. Estimate the size and build-time gain and state every speed-versus-size trade-off.

## Inputs

- **The Dockerfile** (longer free text)
- **App** (longer free text)
- **Current image size and build time** (free text, e.g. "Both")
- **Runs on** (choose one: Kubernetes, ECS, Cloud Run, Docker Compose, Local only)
- **Built** (choose one: Every commit, Daily, On release, Rarely)

## Output

The rewritten Dockerfile with a comment per change, then a short estimate of size and build-time savings and one line per trade-off.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Dockerfile Review and Optimisation

Review my Dockerfile and give me back an improved one.

- The Dockerfile: {{The Dockerfile}}
- App: {{App}}
- Current image size and build time: {{Current image size and build time}}
- Runs on: {{Runs on}}
- Built: {{Built}}

Review layer ordering and caching first, since most slow builds are one line invalidating the cache too early: dependency manifests should be copied and installed before the source. Say whether a multi-stage build would help, which it does whenever a compiler or build toolchain ends up in the final image. Flag the security problems with the line each lives on: running as root, secrets baked into layers, unpinned base images. Check that a .dockerignore keeps the build context small. Give the rewritten file with a comment on each change, an estimate of the size and build time saved, and anything where I would be trading build speed against image size.

## Pitfalls

- Copying the whole source before installing dependencies, which busts the cache on every code change.
- Switching to a minimal base image that lacks a native library the app needs at runtime.
- Pinning versions so tightly that security patches never arrive.
