Dockerfile: mount DEB sources instead of copy to reduce image size#914
Open
stklcode wants to merge 1 commit into
Open
Dockerfile: mount DEB sources instead of copy to reduce image size#914stklcode wants to merge 1 commit into
stklcode wants to merge 1 commit into
Conversation
The DEB package is used temporarily and deleted after installation. The intermediate layer still exists which bloats the final image by about 300MB without any real benefit. Mount (bind) the context directory to /source and drop the COPY layers to reduce the image size. This feature requires BuildKit or an equivalent platform that supports mounts. It is available since Docker v18.09 and default since v23.0 and should not be an issue in modern build environments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
General information
This PR affects the Dockerfile and targets size reduction of the published images.
Proposed changes
The DEB package is used temporarily and deleted after installation. The intermediate layer still exists which bloats the final image by about 300MB without any real benefit. The DEB file is deleted anyway, so the COPY layer is effectively hidden.
Mount (bind) the context directory to /source and drop the COPY layers to reduce the image size.
This feature requires BuildKit or an equivalent platform that supports mounts. It is available since Docker v18.09 and default since v23.0 and should not be an issue in modern build environments.
(with older Docker 18-22
DOCKER_BUILDKIT=1is required)Reference: https://docs.docker.com/reference/dockerfile/#run---mounttypebind
Additional notes
We mount the entire context using
RUN --mount,type=bind,target=/sourcefor the CMK installation. This could be narrowed down to mount only the required files (like we do for needed-packages), but the only difference here is cachng behavior.Using other builders like rootless Podman with SELinux enabled, monting requires proper container_file_t labels or relabelling (e.g.
--mount=type=bind,target=/source,relabel=private). Using Docker/BuildKit or Buildah in most CI environments should be fine without.Test build (using Podman 5.8 on Linux). Upstream image for reference and two custom builds with/without optimization for comparison.
Size comparison
Layer history