DevTools Logo

Dockerfile Layer Optimizer

Dockerfile Layer Optimizer

Check a Dockerfile for layer, cache and multi-stage suggestions. Everything runs in your browser.

Runs in your browser
Try an example

Dockerfile

8 lines · 146 chars

Findings and suggestion

9 findings · 5 filesystem layers in the final stage
Qualitative: many small RUN layers cache independently but add layers; combine only steps that change together. These are suggestions from common best practices; the tool does not build the image or measure its size.

Findings

  • Base image 'node:latest' is not pinnedwarning · line 1

    Consider a version tag or digest instead of latest or an implicit tag, so rebuilds start from the same base.

  • Consider a multi-stage buildinfo · line 1

    Compile in a builder stage and copy only runtime artifacts into the final image so build tools stay out of it.

  • Final stage runs as rootinfo · line 1

    Consider adding a non-root USER before CMD/ENTRYPOINT.

  • Source is copied before dependencies are installedinfo · line 3

    Consider copying only the dependency manifest first, installing, then copying the source, so edits to source files do not re-run the install.

  • Broad build-context copy detectedinfo · line 3

    Consider a .dockerignore, and copy dependency manifests before source files so cache keys stay stable.

  • apt-get update is separated from installwarning · line 4

    The package index becomes stale or invalidates a cache layer; consider update and install in one RUN.

  • Consecutive RUN layers can be combinedinfo · line 4

    Combining related commands with && reduces layers; the trade-off is coarser caching, since any change re-runs the whole combined RUN.

  • apt package lists are not cleanedwarning · line 5

    Removing /var/lib/apt/lists/* in the same RUN keeps the index out of this layer; a rm in a later RUN does not shrink earlier layers.

  • apt-get install without --no-install-recommendsinfo · line 5

    Consider --no-install-recommends to avoid pulling optional packages.

Suggested Dockerfile

  • Combined 4 consecutive RUN instructions starting at line 4 into one.
  • Added rm -rf /var/lib/apt/lists/* to the RUN at line 4 (same layer as apt-get install).
FROM node:latest AS builder
WORKDIR /app
COPY . .
RUN apt-get update && apt-get install -y curl && npm ci && npm run build && rm -rf /var/lib/apt/lists/*
CMD ["npm", "start"]

Examples

Separated apt update and install

Input
FROM node:22
RUN apt-get update
RUN apt-get install -y curl
Output
warnings: apt-update-layer (line 2), apt-cache (line 3), run-combine info

update without install in the same RUN triggers apt-update-layer; the install layer without list cleanup triggers apt-cache.

Optimizer merges RUN layers

Input
optimizeDockerfile("FROM node:22\nRUN apt-get update && apt-get install -y curl\nRUN npm ci")
Output
RUN apt-get update && apt-get install -y curl && npm ci && rm -rf /var/lib/apt/lists/*

Consecutive RUN commands join with && and the missing apt list cleanup is appended to the merged layer.

About this tool

Dockerfile Layer Optimizer parses a Dockerfile with parseDockerfile(source) into instructions and named stages (FROM ... AS <name>, with --platform= prefixes understood and backslash continuations joined), then analyzeDockerfile(source) reports ordered findings: empty and missing-from errors, an only-from error for a bare base image, unpinned-base warnings for latest or implicit tags, apt-update-layer warnings when apt-get update runs without install in the same RUN, apt-cache warnings when apt-get install omits rm -rf /var/lib/apt/lists/*, plus dockerignore, run-combine, and multi-stage info hints.

Every analysis also returns a layerCount over layer-producing instructions, a cacheEstimate (High for multi-stage builds, Medium otherwise with a reason), and an optimized Dockerfile string from optimizeDockerfile(source): consecutive RUN lines merge with && and any apt-get install layer without cleanup gains && rm -rf /var/lib/apt/lists/*. Findings sort deterministically by line, then severity (error, warning, info), then rule name.

Multi-stage awareness is descriptive, not prescriptive: COPY --from=<stage> lines parse as ordinary COPY instructions, and the multi-stage info hint only fires when build-tool RUN commands (npm, pnpm, yarn, go, cargo, mvn, gradle with build/install/compile) appear in a single-stage file — a file that already has two or more stages never gets that hint.

How to use this tool

  1. Paste the Dockerfile

    Call analyzeDockerfile(source): parseDockerfile splits the input into instructions and stages while skipping blanks and # comments and ignoring unknown keywords.

  2. Read findings in order

    Findings arrive sorted by line, severity, then rule — fix error rules (empty, missing-from, only-from) before warning rules (unpinned-base, apt-update-layer, apt-cache).

  3. Combine apt steps

    Move apt-get update and apt-get install into one RUN and end it with rm -rf /var/lib/apt/lists/* to clear both the apt-update-layer and apt-cache warnings.

  4. Apply the optimizer

    Copy the optimized field (optimizeDockerfile output): consecutive RUN layers are already merged with && and apt cleanup appended where it was missing.

Use cases

Slimming Node and Debian-based images

Find apt-cache findings and apply the optimizer's rm -rf cleanup to stop shipping package indexes in the final layer.

Stabilizing layer caching

Use run-combine and dockerignore hints to order dependency installs before broad COPY . /app steps that invalidate the cache.

Reviewing multi-stage conversions

Check whether the multi-stage info hint fires on a single-stage build with compile steps, then split into builder and runtime stages.

Common mistakes

Mistake:Pinning nothing and building FROM node:latest.

Fix:Use an explicit version tag or digest (e.g. node:22) — bare names and :latest trigger unpinned-base because cache keys are not reproducible.

Mistake:Running apt-get update in its own layer.

Fix:Combine update and install in a single RUN; a lone update layer goes stale and raises apt-update-layer.

Mistake:Assuming COPY --from=builder needs a hint to be recognized.

Fix:COPY --from lines already parse as COPY instructions tied to their stage; no finding is emitted for them — the multi-stage hint only appears for single-stage builds with compile commands.

Frequently asked questions

What does this tool do?

It parses your Dockerfile into instructions and named stages, then reports ordered findings — unpinned-base warnings for latest tags, apt-update-layer and apt-cache warnings for apt hygiene, plus dockerignore, run-combine, and multi-stage hints — alongside a layer count, a cache estimate, and an optimized Dockerfile with merged RUN layers.

Why am I seeing apt-update-layer and apt-cache warnings?

apt-update-layer means a RUN calls apt-get update without apt-get install in the same layer, so the package index goes stale; apt-cache means an install layer never removes /var/lib/apt/lists/*. Combine update and install in one RUN ending with rm -rf /var/lib/apt/lists/* — the optimizer's output already applies exactly that fix.

Is my Dockerfile uploaded or stored?

No. Parsing, analysis, and optimization are pure client-side string processing — your Dockerfile never leaves your browser.

When does the multi-stage hint appear?

Only when build-tool RUN commands (npm, pnpm, yarn, go, cargo, mvn, or gradle with build/install/compile) appear in a single-stage file. Files that already define two or more stages — including COPY --from=<stage> lines — never receive it.

References & standards