---
name: docker-container-optimizer
description: "Brings a container image under a size budget: a trimmed Dockerfile with a comment on each decision, a .dockerignore, the run flags that harden it, and a before and after size estimate per layer. Use when a container image is over its size budget and needs a trimmed Dockerfile, a .dockerignore, hardened run flags, and a layer-by-layer size estimate. Do not use for scaffolding a new multi-stage Dockerfile for a language, security scanning an image, or debugging a running container. Related topics: coding, docker."
---

# Docker Container Optimizer

Brings a container image under a size budget: a trimmed Dockerfile with a comment on each decision, a .dockerignore, the run flags that harden it, and a before and after size estimate per layer.

## When to use

Use this skill when a container image is over its size budget and needs a trimmed Dockerfile, a .dockerignore, hardened run flags, and a layer-by-layer size estimate. Do not use it for scaffolding a new multi-stage Dockerfile for a language, security scanning an image, 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. Attribute the current size to base image, caches, build tooling, copied files, and layer ordering, with a rough MB per source.
3. Rewrite the Dockerfile: pinned minimal base, essential packages with cache cleanup in one RUN, build tooling in an earlier stage, and a comment per decision.
4. Write the .dockerignore from the exclusions and the usual offenders, and substitute any package the chosen base lacks.
5. List the docker run flags that harden the container and pass the environment variables.
6. Estimate size per layer before and after, state whether the budget is met, and name the next trade-off if not.

## Inputs

- **Base image** (choose one: alpine:3.20, debian:bookworm-slim, ubuntu:24.04, distroless, Keep current)
- **Current Dockerfile** (longer free text)
- **Essential packages** (longer free text)
- **Files or directories to exclude** (longer free text)
- **Environment variables** (longer free text)
- **Desired maximum image size (MB)** (number from 10 to 500)
- **Build stage count** (choose one: 1, 2, 3)
- **Current image size (MB)** (free text, e.g. "850")

## Output

A fenced Dockerfile block with comments, a fenced .dockerignore block, a bullet list of docker run flags, and a Size Estimate table with Layer, Before (MB), and After (MB) columns plus a budget verdict line.

## Prompt template

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

### Docker Container Optimizer

Trim this container to the size budget without losing what the application needs.

- Base image: {{Base image}}
- Current Dockerfile: {{Current Dockerfile}}
- Essential packages: {{Essential packages}}
- Files or directories to exclude: {{Files or directories to exclude}}
- Environment variables: {{Environment variables}}
- Desired maximum image size (MB): {{Desired maximum image size (MB)}}
- Build stage count: {{Build stage count}}
- Current image size (MB): {{Current image size (MB)}}

Attribute the current size to its sources first: base image, package manager caches, build tooling left in the final stage, copied source and test files, and duplicated layers from poor ordering. Then trim in that order, because the base and the toolchain usually account for most of the excess. Install only the essential packages with the package manager's no-cache or clean flags in the same RUN, pin the base image by digest or minor version, and move build tooling into an earlier stage when the stage count allows. Write a .dockerignore from the exclusions plus the usual offenders (.git, node_modules, tests, docs). When a package is unavailable on the chosen base, name the equivalent or the base that has it. Give docker run flags that harden the container at no size cost: --read-only, --user, --cap-drop ALL, memory and CPU limits, and the environment variables passed as --env-file. Estimate the size per layer before and after and say whether the budget is met; when it is not, name the next trade-off.

## Pitfalls

- Cleaning the package cache in a separate RUN, which leaves the cache in the earlier layer and saves nothing.
- Switching to Alpine for an app that needs glibc, which trades image size for a runtime crash or a musl rebuild.
- Hardening with --read-only without a tmpfs for the paths the app writes, so it fails on first start.
