DCA · Question #160
Some Docker images take time to build through a Continuous Integration environment. You want to speed up builds and take advantage of build caching. Where should the most frequently changed part of…
The correct answer is A. at the bottom of the Dockerfile. To leverage Docker build caching effectively and speed up builds, the most frequently changed parts of a Docker image should be placed at the bottom of the Dockerfile.
Question
Some Docker images take time to build through a Continuous Integration environment. You want to speed up builds and take advantage of build caching. Where should the most frequently changed part of a Docker image be placed in a Dockerfile?
Options
- Aat the bottom of the Dockerfile
- Bafter the FROM directive
- Cat the top of the Dockerfile
- Din the ENTRYPOINT directive
How the community answered
(61 responses)- A85% (52)
- B10% (6)
- C2% (1)
- D3% (2)
Why each option
To leverage Docker build caching effectively and speed up builds, the most frequently changed parts of a Docker image should be placed at the bottom of the Dockerfile.
Docker builds use a layer caching mechanism where each instruction creates a new layer, and if a layer (or any layer above it) changes, all subsequent layers must be rebuilt. By placing frequently changing instructions towards the bottom, changes to these layers will only invalidate a small portion of the cache, allowing earlier, less frequently changing layers to be reused.
Placing frequently changed parts directly after `FROM` would cause almost the entire build cache to be invalidated and rebuilt on most changes, significantly hindering caching benefits.
Placing frequently changed parts at the top would similarly invalidate the cache for all subsequent layers in the Dockerfile, leading to minimal caching efficiency.
The `ENTRYPOINT` directive defines the command executed when the container starts and, while typically towards the bottom, it's not the primary strategy for placing frequently changed content for build caching.
Concept tested: Dockerfile build caching optimization
Source: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#leverage-build-cache
Topics
Community Discussion
No community discussion yet for this question.