Table of Contents

Docker Layers

Layers are stacked filesystem snapshots. Each Dockerfile instruction creates a layer; the image is the union of all layers. Layers are cached—rebuilding reuses unchanged layers, making subsequent builds fast. Inspect layers with docker history.

$ docker history myapp                # show layers and sizes
$ docker inspect myapp:latest         # show layer details in JSON
$ docker build --no-cache .           # rebuild without caching layers
$ docker build -t myapp .             # rebuild (reuses unchanged layers)

Smaller layers reduce image size and improve distribution speed. Combine RUN commands with && to reduce layer count: RUN apt-get update && apt-get install -y curl creates one layer instead of two. The order of Dockerfile instructions matters for caching—place frequently changing instructions (like COPY .) near the end so earlier, stable layers don't rebuild unnecessarily.