KCNA · Question #68
What is a best practice to minimize the container image size?
The correct answer is B. Use multistage builds. Using multistage builds is a best practice for minimizing container image size by separating build-time dependencies from runtime dependencies.
Question
What is a best practice to minimize the container image size?
Options
- AUse a DockerFile.
- BUse multistage builds.
- CBuild images with different tags.
- DAdd a build.sh script.
How the community answered
(38 responses)- A5% (2)
- B89% (34)
- C3% (1)
- D3% (1)
Why each option
Using multistage builds is a best practice for minimizing container image size by separating build-time dependencies from runtime dependencies.
While a Dockerfile is essential for building images, merely using one does not inherently guarantee a small image size; the instructions within it determine the size.
Multistage builds allow you to use multiple `FROM` statements in your Dockerfile, where each `FROM` instruction can use a different base image, and critically, you can copy only the necessary artifacts from one stage to a leaner final stage, thereby discarding all build tools and intermediate layers that are not needed at runtime.
Building images with different tags helps with versioning and management, but it does not directly minimize the size of any individual image.
Adding a `build.sh` script external to the Dockerfile simply automates the build process but does not, by itself, reduce the final image size.
Concept tested: Docker image size optimization
Source: https://docs.docker.com/build/building/multi-stage/
Topics
Community Discussion
No community discussion yet for this question.