diff --git a/Dockerfile-alpine-slim.template b/Dockerfile-alpine-slim.template index f6803565..580dbd14 100644 --- a/Dockerfile-alpine-slim.template +++ b/Dockerfile-alpine-slim.template @@ -102,6 +102,8 @@ RUN set -x \ # Ensure we can run our entrypoint and make it debian compatible && apk add --no-cache tini \ && ln -s /sbin/tini /usr/bin/tini \ +# 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 dede8171..ac0e81d5 100644 --- a/Dockerfile-alpine.template +++ b/Dockerfile-alpine.template @@ -79,3 +79,5 @@ RUN set -x \ # Ensure we can run our entrypoint and do it compatible with alpine && apk add --no-cache tini \ && ln -s /sbin/tini /usr/bin/tini +# 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 e323235f..dd1cd9bd 100644 --- a/Dockerfile-debian.template +++ b/Dockerfile-debian.template @@ -83,6 +83,7 @@ RUN set -x \ gettext-base \ curl \ tini \ + inotify-tools \ && ln -s /usr/bin/tini /sbin/tini \ && apt-get remove --purge --auto-remove -y && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list \ \ 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