Skip to content

Commit 506fd0f

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 e7e1b3c commit 506fd0f

4 files changed

+49
-0
lines changed

Dockerfile-alpine-slim.template

+2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ RUN set -x \
102102
# Ensure we can run our entrypoint and make it debian compatible
103103
&& apk add --no-cache tini \
104104
&& ln -s /sbin/tini /usr/bin/tini \
105+
# Add support for manually monitoring files to trigger server reloads
106+
&& apk add --no-cache inotify-tools \
105107
# create a docker-entrypoint.d directory
106108
&& mkdir /docker-entrypoint.d
107109

Dockerfile-alpine.template

+2
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,5 @@ RUN set -x \
7979
# Ensure we can run our entrypoint and do it compatible with alpine
8080
&& apk add --no-cache tini \
8181
&& ln -s /sbin/tini /usr/bin/tini
82+
# Add support for manually monitoring files to trigger server reloads
83+
&& 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
tini \
86+
inotify-tools \
8687
&& ln -s /usr/bin/tini /sbin/tini \
8788
&& apt-get remove --purge --auto-remove -y && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list \
8889
\
+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)