Free Docker Dev Tool
Paste one or two Dockerfiles to analyze layers by instruction, compare builds side-by-side, detect cache invalidation points, and find optimization opportunities.
Every instruction in a Dockerfile that modifies the filesystem creates a new read-only layer. FROM imports base layers. RUN, COPY, and ADD create new layers with filesystem changes. ENV, LABEL, EXPOSE, and CMD add metadata without creating filesystem layers.
Understanding the layer structure is critical for two reasons: build cache efficiency and image size optimization. Layers are cached by Docker — if a layer and all its ancestors are unchanged, Docker reuses the cached layer instead of rebuilding. Any change to a layer invalidates all subsequent layers.
COPY before package install invalidates cache on every source change
The most common Dockerfile mistake: COPY . . before RUN npm install or RUN pip install. Any source file change invalidates the COPY layer and forces a full reinstall. Fix: copy only the dependency manifest first (COPY package.json .), run install, then copy source. This way source changes only rebuild the final COPY layer.
Multiple RUN commands vs chained commands
Each RUN instruction creates a separate layer. RUN apt-get update followed by RUN apt-get install creates two layers — and the update layer may be stale by the time the install runs. Combine: RUN apt-get update && apt-get install -y pkg && rm -rf /var/lib/apt/lists/*. The cleanup in the same layer actually reduces the final image size.
Secrets and credentials in layers
Any secret passed via ENV MY_SECRET=value or copied via COPY .env . is permanently in the image layer history — even if a later layer deletes it. Use Docker BuildKit secrets (RUN --mount=type=secret) or multi-stage builds to prevent credentials from appearing in any layer.
Large base images vs slim/alpine variants
The FROM node:18 image is ~950MB. FROM node:18-slim is ~240MB. FROM node:18-alpine is ~100MB. Switching base image shows up as a complete layer rebuild in the diff. For production images, alpine variants reduce attack surface and pull time significantly — check compatibility with native dependencies first.