Skip to content

Commit ae86545

Browse files
committed
entrypoint: Monitor config dir for changes
We see a lot of crudges and hacks to notify nginx or the nginx container informing it it needs to restart. While there certainly cases that require manual control, for the most, this could be easily automated. With inotify, we can recursively monitor /etc/nginx (or any directory per config) for changes (currently, not monitoring for for access time changes, e.g. reads or `touch` (not creating new files) events). On an event, we sleep first for (configurable) seconds, the default is 10, so that multiple updates don't cause multiple restarts. E.g. copying 10 certificates into /etc/nginx/certs, won't trigger 10 reloads. The monitor will run indefinably, but to ensure there is 'some' way to exit it, is to remove the pid file (configurable location) and triggering a `/etc/nginx` change (`touch '/etc/nginx/exit'` for example to create a file. It's not perfect, but probably will never be used anyway. The current configuration won't change existing behavior, it needs to be explicitly enabled. Signed-off-by: Olliver Schinagl <[email protected]>
1 parent 2c51119 commit ae86545

4 files changed

+50
-1
lines changed

Dockerfile-alpine-slim.template

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ RUN set -x \
101101
&& ln -sf /dev/stderr /var/log/nginx/error.log \
102102
# Ensure we can run our entrypoint
103103
&& apk add --no-cache dumb-init \
104+
# Add support for manually monitoring files to trigger server reloads
105+
&& apk add --no-cache inotify-tools \
104106
# create a docker-entrypoint.d directory
105107
&& mkdir /docker-entrypoint.d
106108

Dockerfile-alpine.template

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,6 @@ RUN set -x \
7777
# Bring in curl and ca-certificates to make registering on DNS SD easier
7878
&& apk add --no-cache curl ca-certificates \
7979
# Ensure we can run our entrypoint
80-
&& apk add --no-cache dumb-init
80+
&& apk add --no-cache dumb-init \
81+
# Add support for manually monitoring files to trigger server reloads
82+
&& apk add --no-cache inotify-tools

Dockerfile-debian.template

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ RUN set -x \
8383
gettext-base \
8484
curl \
8585
dumb-init \
86+
inotify-tools \
8687
&& apt-get remove --purge --auto-remove -y && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list \
8788
\
8889
# if we have leftovers from building, let's purge them (including extra, unnecessary build deps)
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/sh
2+
# vim:sw=2:ts=2:sts=2:et
3+
4+
set -eu
5+
if [ -n "${DEBUG_TRACE_SH:-}" ] && \
6+
[ "${DEBUG_TRACE_SH:-}" != "${DEBUG_TRACE_SH#*"$(basename "${0}")"*}" ] || \
7+
[ "${DEBUG_TRACE_SH:-}" = 'all' ]; then
8+
set -x
9+
fi
10+
11+
LC_ALL=C
12+
13+
if [ -e "${NGINX_ENTRYPOINT_MONITOR_PID:=/run/nginx_monitor.pid}" ] ||
14+
[ -z "${NGINX_ENTRYPOINT_MONITOR_CONFIG+monitor}" ] || \
15+
! command -v inotifywait; then
16+
exit 0
17+
fi
18+
19+
echo "Monitoring for changes in '${NGINX_ENTRYPOINT_MONITOR_CONFIG:=/etc/nginx}'"
20+
while true; do
21+
inotifywait \
22+
--recursive \
23+
--event 'create' \
24+
--event 'delete' \
25+
--event 'modify' \
26+
--event 'move' \
27+
"${NGINX_ENTRYPOINT_MONITOR_CONFIG}"
28+
29+
sleep "${NGINX_ENTRYPOINT_MONITOR_DELAY:-10s}"
30+
31+
if [ ! -e "${NGINX_ENTRYPOINT_MONITOR_PID}" ]; then
32+
logger -s -t 'nginx' -p 'local0.3' 'Monitor failure or exit requested'
33+
break
34+
fi
35+
36+
if nginx -t; then
37+
nginx -s
38+
else
39+
logger -s -t 'nginx' -p 'local0.3' 'Refusing to reload config, config error'
40+
fi
41+
done &
42+
echo "${!}" > "${NGINX_ENTRYPOINT_MONITOR_PID}"
43+
44+
exit 0

0 commit comments

Comments
 (0)