Skip to content

Commit 618dbb0

Browse files
committed
Dockerfile: mount DEB sources instead of copy to reduce image size
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.
1 parent 09318f7 commit 618dbb0

1 file changed

Lines changed: 9 additions & 15 deletions

File tree

docker_image/Dockerfile

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ COPY docker-entrypoint.sh /
3535
# Starts the entrypoint script and hands over CMD by default
3636
ENTRYPOINT ["/docker-entrypoint.sh"]
3737

38-
# Make the list of required packages available to the following command
39-
COPY needed-packages /needed-packages
40-
4138
# Install the tools we need for fetching the package and installation
4239
# Then fetch the package and install it. This will make sure all Checkmk
4340
# containers will share all dependencies, including this step.
@@ -46,7 +43,8 @@ COPY needed-packages /needed-packages
4643
# be up-to-date, especially when they are pinned in our build environment.
4744
#
4845
# hadolint ignore=SC2046,DL3008
49-
RUN set -e \
46+
RUN --mount=type=bind,source=needed-packages,target=/needed-packages \
47+
set -e \
5048
&& echo "exit 101" > /usr/sbin/policy-rc.d \
5149
&& chmod +x /usr/sbin/policy-rc.d \
5250
&& export DEBIAN_FRONTEND=noninteractive \
@@ -70,7 +68,6 @@ RUN set -e \
7068
&& apt-get clean \
7169
&& rm /usr/sbin/policy-rc.d \
7270
&& rm -rf /var/lib/apt/lists/* \
73-
&& rm needed-packages \
7471
&& mv /etc/apt/sources.list.bak /etc/apt/sources.list
7572

7673
# Pure build time variable declarations (docker build --build-arg KEY=val)
@@ -79,28 +76,24 @@ ARG CMK_EDITION
7976
# Distro codename is used to find the corresponding .deb package
8077
ARG DISTRO_CODENAME="jammy"
8178

82-
# Optionally copy an existing Checkmk debian package to the container. In case the file is
83-
# available that is later used by the build procedure the file will not be downloaded.
84-
COPY check-mk-${CMK_EDITION}-${CMK_VERSION}_0.${DISTRO_CODENAME}*.deb Check_MK-pubkey.gpg /
85-
8679
# Now install the Checkmk version specific things
8780
# hadolint ignore=DL3003,DL3008,DL4006
88-
RUN set -e \
81+
RUN --mount=type=bind,target=/source \
82+
set -e \
8983
&& mkdir -p /usr/share/man/man8 \
9084
&& echo "exit 101" > /usr/sbin/policy-rc.d \
9185
&& chmod +x /usr/sbin/policy-rc.d \
9286
&& export DEBIAN_FRONTEND=noninteractive \
9387
&& PKG_NAME="check-mk-${CMK_EDITION}-${CMK_VERSION}" \
9488
&& PKG_FILE="${PKG_NAME}_0.${DISTRO_CODENAME}_$(dpkg --print-architecture).deb" \
95-
&& if [ ! -e "/${PKG_FILE}" ]; then \
89+
&& if [ ! -e "/source/${PKG_FILE}" ]; then \
9690
echo "ERROR: Please provide ${PKG_FILE} by downloading it from https://download.checkmk.com/checkmk" \
9791
&& return 1 ; \
9892
fi \
99-
&& gpg -q --import "/Check_MK-pubkey.gpg" \
100-
&& gpg --verify "${PKG_FILE}" \
101-
&& dpkg -i "${PKG_FILE}" \
93+
&& gpg -q --import "/source/Check_MK-pubkey.gpg" \
94+
&& gpg --verify "/source/${PKG_FILE}" \
95+
&& dpkg -i "/source/${PKG_FILE}" \
10296
&& dpkg -i "$(ls /omd/versions/default/share/check_mk/agents/check-mk-agent_*-1_all.deb)" \
103-
&& rm -f -- *.deb *.gpg \
10497
&& apt-get clean \
10598
&& rm /usr/sbin/policy-rc.d \
10699
&& rm -rf /var/lib/apt/lists/*
@@ -112,3 +105,4 @@ LABEL \
112105
org.opencontainers.image.vendor="Checkmk GmbH" \
113106
org.opencontainers.image.source="https://github.com/checkmk/checkmk" \
114107
org.opencontainers.image.url="https://checkmk.com/"
108+

0 commit comments

Comments
 (0)