|
| 1 | +# Icinga 2 Docker image | (c) 2025 Icinga GmbH | GPLv2+ |
| 2 | + |
| 3 | +FROM debian:bookworm-slim AS build-base |
| 4 | +SHELL ["/bin/bash", "-o", "pipefail", "-c"] |
| 5 | + |
| 6 | +# Install all the necessary build dependencies for building Icinga 2 and the plugins. |
| 7 | +# |
| 8 | +# This stage includes the build dependencies for the plugins as well, so that they can share the same base |
| 9 | +# image, since Docker builds common stages only once [^1] even if they are used in multiple build stages. |
| 10 | +# This eliminates the need to have a separate base image for the plugins, that basically has kind of the |
| 11 | +# same dependencies as the Icinga 2 build stage (ok, not exactly the same, but some of them are shared). |
| 12 | +# |
| 13 | +# [^1]: https://docs.docker.com/build/building/best-practices/#create-reusable-stages |
| 14 | +RUN apt-get update && \ |
| 15 | + apt-get install -y --no-install-{recommends,suggests} \ |
| 16 | + autoconf \ |
| 17 | + automake \ |
| 18 | + bison \ |
| 19 | + ccache \ |
| 20 | + cmake \ |
| 21 | + flex \ |
| 22 | + g++ \ |
| 23 | + git \ |
| 24 | + libboost{,-{context,coroutine,date-time,filesystem,iostreams,program-options,regex,system,test,thread}}1.74-dev \ |
| 25 | + libedit-dev \ |
| 26 | + libmariadb-dev \ |
| 27 | + libpq-dev \ |
| 28 | + libssl-dev \ |
| 29 | + libsystemd-dev \ |
| 30 | + make && \ |
| 31 | + rm -rf /var/lib/apt/lists/* |
| 32 | + |
| 33 | +# Set the default working directory for subsequent commands of the next stages. |
| 34 | +WORKDIR /icinga2-build |
| 35 | + |
| 36 | +FROM build-base AS build-plugins |
| 37 | +SHELL ["/bin/bash", "-o", "pipefail", "-c"] |
| 38 | + |
| 39 | +# Install all the plugins that are not included in the monitoring-plugins package. |
| 40 | +ADD https://github.com/lausser/check_mssql_health.git#747af4c3c261790341da164b58d84db9c7fa5480 /check_mssql_health |
| 41 | +ADD https://github.com/lausser/check_nwc_health.git#a5295475c9bbd6df9fe7432347f7c5aba16b49df /check_nwc_health |
| 42 | +ADD https://github.com/bucardo/check_postgres.git#58de936fdfe4073413340cbd9061aa69099f1680 /check_postgres |
| 43 | +ADD https://github.com/matteocorti/check_ssl_cert.git#341b5813108fb2367ada81e866da989ea4fb29e7 /check_ssl_cert |
| 44 | + |
| 45 | +WORKDIR /check_mssql_health |
| 46 | +RUN mkdir bin && \ |
| 47 | + autoconf && \ |
| 48 | + autoreconf && \ |
| 49 | + ./configure "--build=$(uname -m)-unknown-linux-gnu" --libexecdir=/usr/lib/nagios/plugins && \ |
| 50 | + make && \ |
| 51 | + make install DESTDIR="$(pwd)/bin" |
| 52 | + |
| 53 | +WORKDIR /check_nwc_health |
| 54 | +RUN mkdir bin && \ |
| 55 | + autoreconf && \ |
| 56 | + ./configure "--build=$(uname -m)-unknown-linux-gnu" --libexecdir=/usr/lib/nagios/plugins && \ |
| 57 | + make && \ |
| 58 | + make install DESTDIR="$(pwd)/bin" |
| 59 | + |
| 60 | +WORKDIR /check_postgres |
| 61 | +RUN mkdir bin && \ |
| 62 | + perl Makefile.PL INSTALLSITESCRIPT=/usr/lib/nagios/plugins && \ |
| 63 | + make && \ |
| 64 | + make install DESTDIR="$(pwd)/bin" && \ |
| 65 | + # This is necessary because of this build error: cannot copy to non-directory: /var/lib/docker/.../merged/usr/local/man |
| 66 | + rm -rf bin/usr/local/man |
| 67 | + |
| 68 | +FROM build-base AS build-icinga2 |
| 69 | +SHELL ["/bin/bash", "-o", "pipefail", "-c"] |
| 70 | + |
| 71 | +# To access the automated build arguments in the Dockerfile originated from the Docker BuildKit [^1], |
| 72 | +# we need to declare them here as build arguments. This is necessary because we want to use unique IDs |
| 73 | +# for the mount cache below for each platform to avoid conflicts between multi arch builds. Otherwise, |
| 74 | +# the build targets will invalidate the cache one another, leading to strange build errors. |
| 75 | +# |
| 76 | +# [^1]: https://docs.docker.com/reference/dockerfile/#automatic-platform-args-in-the-global-scope |
| 77 | +ARG TARGETPLATFORM |
| 78 | + |
| 79 | +# Create the directory where the final Icinga 2 files will be installed. |
| 80 | +# |
| 81 | +# This directory will be used as the destination for the `make install` command below and will be |
| 82 | +# copied to the final image. Other than that, this directory will not be used for anything else. |
| 83 | +RUN mkdir /icinga2-install |
| 84 | + |
| 85 | +# Mount the source code as a bind mount instead of copying it, so that we can use the cache effectively. |
| 86 | +# Additionally, add the ccache and CMake build directories as cache mounts to speed up rebuilds. |
| 87 | +RUN --mount=type=bind,source=.,target=/icinga2,readonly \ |
| 88 | + --mount=type=cache,id=ccache-${TARGETPLATFORM},target=/root/.ccache \ |
| 89 | + --mount=type=cache,id=icinga2-build-${TARGETPLATFORM},target=/icinga2-build \ |
| 90 | + PATH="/usr/lib/ccache:$PATH" \ |
| 91 | + cmake -S /icinga2 -B /icinga2-build \ |
| 92 | + -DCMAKE_BUILD_TYPE=ReleaseWithDebInfo \ |
| 93 | + # The command group name below is required for the prepare-dirs script to work, as it expects |
| 94 | + # the command group name, which by default is `icingacmd` to exist on the system. Since we |
| 95 | + # don't create the `icingacmd` command group in this image, we need to override it with icinga. |
| 96 | + -DICINGA2_COMMAND_GROUP=icinga \ |
| 97 | + -DCMAKE_INSTALL_PREFIX=/usr \ |
| 98 | + -DCMAKE_INSTALL_SYSCONFDIR=/data/etc \ |
| 99 | + -DCMAKE_INSTALL_LOCALSTATEDIR=/data/var \ |
| 100 | + -DICINGA2_SYSCONFIGFILE=/etc/sysconfig/icinga2 \ |
| 101 | + -DICINGA2_RUNDIR=/run \ |
| 102 | + # See https://github.com/Icinga/docker-icinga2/pull/103 for why we need to enable systemd support. |
| 103 | + -DUSE_SYSTEMD=ON \ |
| 104 | + -DICINGA2_WITH_{COMPAT,LIVESTATUS}=OFF && \ |
| 105 | + make -j$(nproc) && \ |
| 106 | + CTEST_OUTPUT_ON_FAILURE=1 make test && \ |
| 107 | + make install DESTDIR=/icinga2-install |
| 108 | + |
| 109 | +RUN rm -rf /icinga2-install/etc/icinga2/features-enabled/mainlog.conf \ |
| 110 | + /icinga2-install/usr/share/doc/icinga2/markdown && \ |
| 111 | + strip -g /icinga2-install/usr/lib/*/icinga2/sbin/icinga2 && \ |
| 112 | + strip -g /icinga2-install/usr/lib/nagios/plugins/check_nscp_api |
| 113 | + |
| 114 | +# Prepare the final image with the necessary configuration files and runtime dependencies. |
| 115 | +FROM debian:bookworm-slim AS icinga2 |
| 116 | +SHELL ["/bin/bash", "-o", "pipefail", "-c"] |
| 117 | + |
| 118 | +# Install the necessary runtime dependencies for the Icinga 2 binary and the monitoring-plugins. |
| 119 | +RUN apt-get update && \ |
| 120 | + DEBIAN_FRONTEND=noninteractive && \ |
| 121 | + apt-get install -y --no-install-{recommends,suggests} \ |
| 122 | + bc \ |
| 123 | + ca-certificates \ |
| 124 | + curl \ |
| 125 | + dumb-init \ |
| 126 | + file \ |
| 127 | + libboost-{context,coroutine,date-time,filesystem,iostreams,program-options,regex,system,thread}1.74.0 \ |
| 128 | + libcap2-bin \ |
| 129 | + libedit2 \ |
| 130 | + libldap-common \ |
| 131 | + libmariadb3 \ |
| 132 | + libmoosex-role-timer-perl \ |
| 133 | + libpq5 \ |
| 134 | + libssl3 \ |
| 135 | + libsystemd0 \ |
| 136 | + mailutils \ |
| 137 | + monitoring-plugins \ |
| 138 | + msmtp{,-mta} \ |
| 139 | + openssh-client \ |
| 140 | + openssl && \ |
| 141 | + # Official Debian images automatically run `apt-get clean` after every install, so we don't need to do it here. |
| 142 | + rm -rf /var/lib/apt/lists/* |
| 143 | + |
| 144 | +# Create the icinga user and group with a specific UID as recommended by Docker best practices. |
| 145 | +# The user has a home directory at /var/lib/icinga2, and if configured, that directory will also |
| 146 | +# be used to store the ".msmtprc" file created by the entrypoint script. |
| 147 | +RUN adduser \ |
| 148 | + --system \ |
| 149 | + --group \ |
| 150 | + --home /var/lib/icinga2 \ |
| 151 | + --disabled-login \ |
| 152 | + --allow-bad-names \ |
| 153 | + --no-create-home \ |
| 154 | + --uid 5665 icinga |
| 155 | + |
| 156 | +COPY --from=build-plugins /check_mssql_health/bin/ / |
| 157 | +COPY --from=build-plugins /check_nwc_health/bin/ / |
| 158 | +COPY --from=build-plugins /check_postgres/bin/ / |
| 159 | +COPY --from=build-plugins /check_ssl_cert/check_ssl_cert /usr/lib/nagios/plugins/check_ssl_cert |
| 160 | + |
| 161 | +COPY --from=build-icinga2 /icinga2-install/ / |
| 162 | + |
| 163 | +# Create for all Icinga 2 directories in /data a corresponding symlink in the root directory. |
| 164 | +# This is necessary because we want to maintain the compatibility with containers built with the |
| 165 | +# legacy Dockerfile, which expects the Icinga 2 directories to be in the root directory. |
| 166 | +RUN for dir in /etc/icinga2 /var/cache/icinga2 /var/lib/icinga2 /var/log/icinga2 /var/spool/icinga2; do \ |
| 167 | + ln -vs "/data$dir" "$dir"; \ |
| 168 | +done |
| 169 | + |
| 170 | +# Run the prepare-dirs script to create non-existing directories and set the correct permissions for them. |
| 171 | +# It's invoked in the same way as in the systemd unit file in a Debian package, so this will ensure that |
| 172 | +# all the necessary directories are created with the correct permissions and ownership. |
| 173 | +RUN /usr/lib/icinga2/prepare-dirs /etc/sysconfig/icinga2 |
| 174 | + |
| 175 | +# Well, since the /data directory is intended to be used as a volume, we should also declare it as such. |
| 176 | +# This will allow users to mount their own directories or even specific files to the /data directory |
| 177 | +# without any issues. We've already filled the /data directory with the necessary configuration files, |
| 178 | +# so users can simply mount their own files or directories if they want to override the default ones and |
| 179 | +# they will be able to do so without any issues. |
| 180 | +VOLUME ["/data"] |
| 181 | + |
| 182 | +COPY tools/container/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh |
| 183 | +RUN chmod +x /usr/local/bin/docker-entrypoint.sh |
| 184 | +ENTRYPOINT ["/usr/bin/dumb-init", "-c", "--", "/usr/local/bin/docker-entrypoint.sh"] |
| 185 | + |
| 186 | +EXPOSE 5665 |
| 187 | +USER icinga |
| 188 | + |
| 189 | +CMD ["icinga2", "daemon"] |
0 commit comments