From 61fea7837f322b5d23ef5b1583973288e3ffa37c Mon Sep 17 00:00:00 2001
From: "Austin S. Hemmelgarn" <austin@netdata.cloud>
Date: Tue, 21 Feb 2023 07:12:32 -0500
Subject: [PATCH] Support installing extra packages in Docker images at
 runtime. (#14456)

* Support installing extra packages in Docker images at runtime.

This enables users to pull in additional packages more easily if they
need to. It also allows us to drop optional runtime dependencies from
our base images without making life significantly more difficult for
users who actually need them.

* Reorganize code and fix issues brought up in review.

* Make new variable empty by default instead.
---
 packaging/docker/Dockerfile |  2 ++
 packaging/docker/README.md  | 14 ++++++++++++++
 packaging/docker/run.sh     | 13 +++++++++++++
 3 files changed, 29 insertions(+)

diff --git a/packaging/docker/Dockerfile b/packaging/docker/Dockerfile
index 3d27c2ba0b..54387d67c4 100644
--- a/packaging/docker/Dockerfile
+++ b/packaging/docker/Dockerfile
@@ -117,6 +117,8 @@ RUN chown -R root:root \
 ENV NETDATA_LISTENER_PORT 19999
 EXPOSE $NETDATA_LISTENER_PORT
 
+ENV NETDATA_EXTRA_APK_PACKAGES=""
+
 ENTRYPOINT ["/usr/sbin/run.sh"]
 
 HEALTHCHECK --interval=60s --timeout=10s --retries=3 CMD /usr/sbin/health.sh
diff --git a/packaging/docker/README.md b/packaging/docker/README.md
index 49083fb661..09acc843b4 100644
--- a/packaging/docker/README.md
+++ b/packaging/docker/README.md
@@ -157,6 +157,20 @@ Additionally, for each stable release, three tags are pushed, one with the full
 that would match that tag (for example, if `v1.30.1` were to be published, the `v1.30` tag would be updated to
 point to that instead of `v1.30.0`).
 
+## Adding extra packages at runtime
+
+By default, the official Netdata container images do not include a number of optional runtime dependencies. You
+can add these dependencies, or any other APK packages, at runtime by listing them in the environment variable
+`NETDATA_EXTRA_APK_PACKAGES`.
+
+Commonly useful packages include:
+
+- `apcupsd`: For monitoring APC UPS devices.
+- `libvirt-daemon`: For resolving cgroup names for libvirt domains.
+- `lm-sensors`: For monitoring hardware sensors.
+- `msmtp`: For email alert support.
+- `netcat-openbsd`: For IRC alert support.
+
 ## Health Checks
 
 Our Docker image provides integrated support for health checks through the standard Docker interfaces.
diff --git a/packaging/docker/run.sh b/packaging/docker/run.sh
index 9029e22b61..9c2fcba664 100755
--- a/packaging/docker/run.sh
+++ b/packaging/docker/run.sh
@@ -67,4 +67,17 @@ if [ -n "${NETDATA_CLAIM_URL}" ] && [ -n "${NETDATA_CLAIM_TOKEN}" ] && [ ! -f /v
                              -daemon-not-running
 fi
 
+if [ -n "${NETDATA_EXTRA_APK_PACKAGES}" ]; then
+  echo "Fetching APK repository metadata."
+  if ! apk update; then
+    echo "Failed to fetch APK repository metadata."
+  else
+    echo "Installing supplementary packages."
+    # shellcheck disable=SC2086
+    if ! apk add --no-cache ${NETDATA_EXTRA_APK_PACKAGES}; then
+      echo "Failed to install supplementary packages."
+    fi
+  fi
+fi
+
 exec /usr/sbin/netdata -u "${DOCKER_USR}" -D -s /host -p "${NETDATA_LISTENER_PORT}" "$@"