Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

entrypoint: Monitor config dir for changes #791

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Dockerfile-alpine-slim.template
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ RUN set -x \
# forward request and error logs to docker log collector
&& ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log \
# 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

Expand Down
7 changes: 6 additions & 1 deletion Dockerfile-alpine.template
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ RUN set -x \
&& if [ -n "/etc/apk/keys/abuild-key.rsa.pub" ]; then rm -f /etc/apk/keys/abuild-key.rsa.pub; fi \
&& if [ -n "/etc/apk/keys/nginx_signing.rsa.pub" ]; then rm -f /etc/apk/keys/nginx_signing.rsa.pub; fi \
# Bring in curl and ca-certificates to make registering on DNS SD easier
&& apk add --no-cache curl ca-certificates
&& apk add --no-cache curl ca-certificates \
# 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
3 changes: 3 additions & 0 deletions Dockerfile-debian.template
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ RUN set -x \
$nginxPackages \
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 \
\
# if we have leftovers from building, let's purge them (including extra, unnecessary build deps)
Expand Down
44 changes: 44 additions & 0 deletions entrypoint/99-monitor-config-changes.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion entrypoint/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/sbin/init /bin/sh
# vim:sw=4:ts=4:et

set -e
Expand Down