Suggest an editImprove this articleRefine the answer for “Why does the order of instructions in a Dockerfile matter?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The order of instructions in a **Dockerfile** matters for three main reasons: layer caching, build speed, and image size. **Key point:** a Dockerfile is read and built top to bottom, and each step creates a layer; if the order of instructions is changed thoughtlessly, the image will take longer to build, become larger, and the cache will stop working effectively.Shown above the full answer for quick recall.Answer (EN)ImageThe order of instructions in a Dockerfile matters for three main reasons: ## Layer caching Docker builds an image layer by layer and caches each step. If you change a line higher up in the Dockerfile, the cache for all the following layers is reset, and they are rebuilt from scratch. That is why commands that rarely change (for example, `apt-get install` or installing dependencies) are placed higher up, while the ones that change are placed lower (for example, copying source files). ## Build speed The right order lets you use the cache and build the image much faster. If you break the cache every time because of the wrong instruction order, the build will take longer. ## Image size and cleanliness A well-thought-out order lets you minimize the number of extra layers, and therefore reduce the final image size. For example, cleanup commands (`rm`, `apt clean`) are combined into a single `RUN`, so that no garbage is left in the previous layers. **Summary for junior:** A Dockerfile is read and built top to bottom, and each step creates a layer. If you change the order of instructions thoughtlessly, the image will take longer to build, become larger, and the cache will stop working effectively.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.