Docker イメージはレイヤーとして構築されます。Dockerfile の各命令がレイヤーを作成し、Docker はキャッシングを行ってリビルドを高速化します。レイヤーとキャッシングを理解することは、高速にビルドでき、小さいイメージを生成する効率的な Dockerfile を書くための鍵となります。
レイヤー — 各命令がレイヤーを追加
Each Dockerfile instruction (FROM, RUN, COPY, etc.) creates a read-only LAYER:
→ layers stack to form the image; layers are CACHED and SHARED between images
→ if a layer is unchanged, Docker REUSES the cached layer (skips rebuilding it)
→ Layer caching makes rebuilds fast — only changed layers (and those AFTER) rebuild.
キャッシングルール(順序が重要)
Docker caches layers IN ORDER. When an instruction's input changes, that layer AND
ALL layers AFTER IT are rebuilt (cache invalidated from that point down).
→ Put STABLE things EARLY and FREQUENTLY-CHANGING things LATE in the Dockerfile.
