Skip to content

Dockerfile: mount DEB sources instead of copy to reduce image size#914

Open
stklcode wants to merge 1 commit into
Checkmk:masterfrom
stklcode:refactor/dockerfile
Open

Dockerfile: mount DEB sources instead of copy to reduce image size#914
stklcode wants to merge 1 commit into
Checkmk:masterfrom
stklcode:refactor/dockerfile

Conversation

@stklcode
Copy link
Copy Markdown
Contributor

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=1 is required)

Reference: https://docs.docker.com/reference/dockerfile/#run---mounttypebind

Additional notes

We mount the entire context using RUN --mount,type=bind,target=/source for 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

REPOSITORY TAG IMAGE ID SIZE
docker.io/checkmk/check-mk-community 2.5.0p1 eddd8397d6ad 2.24 GB
localhost/check-mk-community 2.5.0p1-original 9b2f4ff16b4b 2.25 GB
localhost/check-mk-community 2.5.0p1-optimized f381d5ee0f39 1.86 GB

Layer history

CREATED BY ID (orig) SIZE (orig) ID SIZE
/bin/sh -c #(nop) LABEL org.opencontai... c87f42abd820 0 B f381d5ee0f39 0 B
|3 CMK_EDITION=community CMK_VERSION=2.5.0... 1.53 GB 1.53 GB
/bin/sh -c #(nop) COPY multi:45ace9d0f9ad6... 73e53d05d8c3 331 MB -- --
/bin/sh -c #(nop) ARG CMK_EDITION CMK_VERS... c75640b6230b 0 B 4a3a763caed1 0 B
/bin/sh -c #(nop) ARG CMK_EDITION CMK_VERSION 0 B 0 B
/bin/sh -c #(nop) ARG CMK_VERSION 0 B 0 B
/bin/sh -c set -e && echo "exit 101" >... 309 MB 309 MB
COPY file:12dbbdebed8418... 1c00a30d8ffc 2.05 kB -- --
ENTRYPOINT ["/docker-ent... e9244ff50479 0 B e9244ff50479 0 B
/bin/sh -c #(nop) COPY file:7ea8f45fd1a8a6... 7.17 kB 7.17 kB
/bin/sh -c #(nop) ENV CMK_CONTAINERIZED="T... 714d1a2f39e9 0 B 714d1a2f39e9 0 B
/bin/sh -c #(nop) ENV TZ="" 0 B 0 B
/bin/sh -c #(nop) ENV MAIL_RELAY_HOST="" 0 B 0 B
/bin/sh -c #(nop) ENV CMK_PASSWORD="" 0 B 0 B
/bin/sh -c #(nop) ENV CMK_LIVESTATUS_TCP="" 0 B 0 B
/bin/sh -c #(nop) ENV CMK_SITE_ID="cmk" 0 B 0 B
/bin/sh -c #(nop) HEALTHCHECK --interval=1... 0 B 0 B
/bin/sh -c #(nop) EXPOSE 5000 6557 0 B 0 B

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant