diff --git a/Dockerfile-alpine-slim.template b/Dockerfile-alpine-slim.template index 3b85b5c7..638630ce 100644 --- a/Dockerfile-alpine-slim.template +++ b/Dockerfile-alpine-slim.template @@ -101,6 +101,8 @@ RUN set -x \ && ln -sf /dev/stderr /var/log/nginx/error.log \ # Ensure we can run our entrypoint && apk add --no-cache dumb-init \ +# Add support for manually monitoring files to trigger server reloads + && apk add --no-cache inotify-tools \ # create a docker-entrypoint.d directory && mkdir /docker-entrypoint.d diff --git a/Dockerfile-alpine.template b/Dockerfile-alpine.template index 7e9ba156..5f3d7c1a 100644 --- a/Dockerfile-alpine.template +++ b/Dockerfile-alpine.template @@ -77,4 +77,6 @@ RUN set -x \ # Bring in curl and ca-certificates to make registering on DNS SD easier && apk add --no-cache curl ca-certificates \ # Ensure we can run our entrypoint - && apk add --no-cache dumb-init + && apk add --no-cache dumb-init \ +# Add support for manually monitoring files to trigger server reloads + && apk add --no-cache inotify-tools diff --git a/Dockerfile-debian.template b/Dockerfile-debian.template index bb72fabc..99941508 100644 --- a/Dockerfile-debian.template +++ b/Dockerfile-debian.template @@ -83,6 +83,7 @@ RUN set -x \ gettext-base \ curl \ dumb-init \ + inotify-tools \ && apt-get remove --purge --auto-remove -y && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list \ \ # if we have leftovers from building, let's purge them (including extra, unnecessary build deps) diff --git a/entrypoint/99-monitor-config-changes.sh b/entrypoint/99-monitor-config-changes.sh new file mode 100755 index 00000000..c2ed02b3 --- /dev/null +++ b/entrypoint/99-monitor-config-changes.sh @@ -0,0 +1,44 @@ +#!/bin/sh +# vim:sw=2:ts=2:sts=2:et + +set -eu +if [ -n "${DEBUG_TRACE_SH:-}" ] && \ + [ "${DEBUG_TRACE_SH:-}" != "${DEBUG_TRACE_SH#*"$(basename "${0}")"*}" ] || \ + [ "${DEBUG_TRACE_SH:-}" = 'all' ]; then + set -x +fi + +LC_ALL=C + +if [ -e "${NGINX_ENTRYPOINT_MONITOR_PID:=/run/nginx_monitor.pid}" ] || + [ -z "${NGINX_ENTRYPOINT_MONITOR_CONFIG+monitor}" ] || \ + ! command -v inotifywait; then + exit 0 +fi + +echo "Monitoring for changes in '${NGINX_ENTRYPOINT_MONITOR_CONFIG:=/etc/nginx}'" +while true; do + inotifywait \ + --recursive \ + --event 'create' \ + --event 'delete' \ + --event 'modify' \ + --event 'move' \ + "${NGINX_ENTRYPOINT_MONITOR_CONFIG}" + + sleep "${NGINX_ENTRYPOINT_MONITOR_DELAY:-10s}" + + if [ ! -e "${NGINX_ENTRYPOINT_MONITOR_PID}" ]; then + logger -s -t 'nginx' -p 'local0.3' 'Monitor failure or exit requested' + break + fi + + if nginx -t; then + nginx -s + else + logger -s -t 'nginx' -p 'local0.3' 'Refusing to reload config, config error' + fi +done & +echo "${!}" > "${NGINX_ENTRYPOINT_MONITOR_PID}" + +exit 0