Skip to content

Commit

Permalink
Upgrade s6-overlay to v3
Browse files Browse the repository at this point in the history
Fixes tons of issues related to startup and shutdown
  • Loading branch information
alexyao2015 committed Feb 24, 2022
1 parent e1b5974 commit abf7b52
Show file tree
Hide file tree
Showing 29 changed files with 74 additions and 81 deletions.
20 changes: 8 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# syntax=docker/dockerfile:experimental
ARG ZM_VERSION=master
ARG S6_ARCH=amd64
ARG S6_OVERLAY_VERSION="v2.2.0.3"

#####################################################################
# #
Expand Down Expand Up @@ -44,7 +43,8 @@ RUN set -x \

COPY rootfs .
RUN set -x \
&& find . -type f -print0 | xargs -0 -n 1 -P 4 dos2unix
&& find . -type f -print0 | xargs -0 -n 1 -P 4 dos2unix \
&& chmod -R +x *

#####################################################################
# #
Expand All @@ -54,21 +54,18 @@ RUN set -x \
FROM alpine:latest as s6downloader
# Required to persist build arg
ARG S6_ARCH
ARG S6_OVERLAY_VERSION
WORKDIR /s6downloader

RUN set -x \
&& wget -O /tmp/s6-overlay.tar.gz "https://github.com/just-containers/s6-overlay/releases/download/${S6_OVERLAY_VERSION}/s6-overlay-${S6_ARCH}.tar.gz" \
&& S6_OVERLAY_VERSION=$(wget --no-check-certificate -qO - https://api.github.com/repos/just-containers/s6-overlay/releases/latest | awk '/tag_name/{print $4;exit}' FS='[""]') \
&& S6_OVERLAY_VERSION=${S6_OVERLAY_VERSION:1} \
&& wget -O /tmp/s6-overlay-arch.tar.xz "https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-x86_64-${S6_OVERLAY_VERSION}.tar.xz" \
&& wget -O /tmp/s6-overlay-noarch.tar.xz "https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-noarch-${S6_OVERLAY_VERSION}.tar.xz" \
&& mkdir -p /tmp/s6 \
&& tar zxvf /tmp/s6-overlay.tar.gz -C /tmp/s6 \
&& tar -Jxvf /tmp/s6-overlay-noarch.tar.xz -C /tmp/s6 \
&& tar -Jxvf /tmp/s6-overlay-arch.tar.xz -C /tmp/s6 \
&& cp -r /tmp/s6/* .

RUN set -x \
&& wget -O /tmp/socklog-overlay.tar.gz "https://github.com/just-containers/socklog-overlay/releases/latest/download/socklog-overlay-${S6_ARCH}.tar.gz" \
&& mkdir -p /tmp/socklog \
&& tar zxvf /tmp/socklog-overlay.tar.gz -C /tmp/socklog \
&& cp -r /tmp/socklog/* .

#####################################################################
# #
# Prepare base-image with core programs + repository #
Expand Down Expand Up @@ -273,7 +270,6 @@ RUN set -x \
ENV \
S6_FIX_ATTRS_HIDDEN=1 \
S6_BEHAVIOUR_IF_STAGE2_FAILS=2 \
SOCKLOG_TIMESTAMP_FORMAT="" \
MAX_LOG_SIZE_BYTES=1000000 \
MAX_LOG_NUMBER=10

Expand Down
2 changes: 1 addition & 1 deletion rootfs/etc/cont-init.d/00-reconfigure-user.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
program_name="reconfigure-user"

Expand Down
6 changes: 2 additions & 4 deletions rootfs/etc/cont-init.d/10-log-config.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
program_name="log-config"

echo "Configuring log rotation with a maximum of ${MAX_LOG_NUMBER} logs and a max log size of ${MAX_LOG_SIZE_BYTES} bytes" | info "[${program_name}] "
echo -n "1 n${MAX_LOG_NUMBER} s${MAX_LOG_SIZE_BYTES}" > /var/run/s6/container_environment/S6_LOGGING_SCRIPT
sed -i "s/nMAX_NUMBER_OF_LOGS/n${MAX_LOG_NUMBER}/g" /etc/socklog.rules/zoneminder-log
sed -i "s/sMAX_SIZE_OF_LOGS/s${MAX_LOG_SIZE_BYTES}/g" /etc/socklog.rules/zoneminder-log
echo -n "1 n${MAX_LOG_NUMBER} s${MAX_LOG_SIZE_BYTES}" > /run/s6/container_environment/S6_LOGGING_SCRIPT
4 changes: 2 additions & 2 deletions rootfs/etc/cont-init.d/20-system-config.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
program_name="system-config"

Expand All @@ -11,7 +11,7 @@ ln -sf "/usr/share/zoneinfo/$TZ" /etc/localtime
echo "Configuring PHP Time" | info "[${program_name}] "
# PHP_INSTALL=`php -r "echo php_ini_loaded_file().PHP_EOL;"`
PHP_VERSION=$(php -r "echo PHP_MAJOR_VERSION;" && echo -n "." && php -r "echo PHP_MINOR_VERSION;")
echo -n "${PHP_VERSION}" > /var/run/s6/container_environment/PHP_VERSION
echo -n "${PHP_VERSION}" > /run/s6/container_environment/PHP_VERSION

echo "date.timezone = ${TZ}" >> /etc/php/"${PHP_VERSION}"/fpm/conf.d/30-zoneminder-time.ini

Expand Down
2 changes: 1 addition & 1 deletion rootfs/etc/cont-init.d/30-zm-config.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
program_name="zm-config"

Expand Down
2 changes: 1 addition & 1 deletion rootfs/etc/cont-init.d/40-msmtp-config.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
program_name="msmtp-config"

Expand Down
2 changes: 1 addition & 1 deletion rootfs/etc/cont-init.d/50-nginx-config.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
program_name="nginx-config"

Expand Down
9 changes: 5 additions & 4 deletions rootfs/etc/mariadbconfigure.d/10-zoneminder-config.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
# ==============================================================================
# ZoneMinder-config
Expand All @@ -13,9 +13,6 @@ if ! (fdmove -c 2 1 \
echo "Creating ZoneMinder db for first run" | init
mysql -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" -h"${MYSQL_HOST}" < /usr/share/zoneminder/db/zm_create.sql

echo "Disabling file log to prevent duplicate logs from syslog" | init
insert_command+="UPDATE Config SET Value = -5 WHERE Name = 'ZM_LOG_LEVEL_FILE';"

echo "Configuring ZoneMinder Email settings..." | init
insert_command+="UPDATE Config SET Value = 1 WHERE Name = 'ZM_NEW_MAIL_MODULES';"
insert_command+="UPDATE Config SET Value = 1 WHERE Name = 'ZM_OPT_EMAIL';"
Expand All @@ -32,6 +29,10 @@ else

fi

# Enforce disabling of file logs
echo "Disabling file log to prevent duplicate logs from syslog" | info
insert_command+="UPDATE Config SET Value = -5 WHERE Name = 'ZM_LOG_LEVEL_FILE';"

if [[ -n "${ZM_SERVER_HOST}" ]] \
&& [ "$(mysql -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" -h"${MYSQL_HOST}" zm -e \
"SELECT COUNT(*) FROM Servers WHERE Name = '${ZM_SERVER_HOST}';" \
Expand Down
2 changes: 1 addition & 1 deletion rootfs/etc/services.d/fcgiwrap/finish
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
program_name="fcgiwrap"
# ==============================================================================
Expand Down
2 changes: 1 addition & 1 deletion rootfs/etc/services.d/fcgiwrap/log/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
program_name="fcgiwrap"

s6-format-filter "%1 %s" "[${program_name}]" | logutil-service /log/"${program_name}"
4 changes: 2 additions & 2 deletions rootfs/etc/services.d/fcgiwrap/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
# ==============================================================================
# fcgiwrap
Expand All @@ -12,4 +12,4 @@ rm /zoneminder/run/fcgiwrap.socket > /dev/null 2>&1

exec s6-setuidgid www-data \
fdmove -c 2 1 \
fcgiwrap -f -c "${FCGIWRAP_PROCESSES}" -s unix:/zoneminder/run/fcgiwrap.socket
/usr/sbin/fcgiwrap -f -c "${FCGIWRAP_PROCESSES}" -s unix:/zoneminder/run/fcgiwrap.socket
2 changes: 1 addition & 1 deletion rootfs/etc/services.d/mariadb-configure/log/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
program_name="mariadb-configure"

s6-format-filter "%1 %s" "[${program_name}]" | logutil-service /log/"${program_name}"
6 changes: 3 additions & 3 deletions rootfs/etc/services.d/mariadb-configure/run
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
# ==============================================================================
# mariadb-configure
# Configures db using /etc/mariadbconfigure.d
# ==============================================================================

# Reconfigure to be oneshot
/bin/s6-svc -O /var/run/s6/services/mariadb-configure
s6-svc -O /run/service/mariadb-configure

echo "Waiting for MariaDB to start" | info
# Wait for db to be up before configuring
/bin/s6-svwait -U /var/run/s6/services/mariadb
s6-svwait -U /run/service/mariadb

for f in /etc/mariadbconfigure.d/*.sh; do
bash "$f"
Expand Down
4 changes: 2 additions & 2 deletions rootfs/etc/services.d/mariadb/data/check
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash

for _ in {1..3}; do
if ! (fdmove -c 2 1 \
mysql -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" -h"${MYSQL_HOST}" -e 'USE zm;' \
mysql --connect-timeout=1 -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" -h"${MYSQL_HOST}" -e 'USE zm;' \
> /dev/null); then
exit 1 # mysql is not running
fi
Expand Down
4 changes: 2 additions & 2 deletions rootfs/etc/services.d/mariadb/finish
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
# ==============================================================================
# Mariadb
# Take down the S6 supervision tree if db is no longer accessible
# ==============================================================================

s6-svscanctl -t /var/run/s6/services
/run/s6/basedir/bin/halt
2 changes: 1 addition & 1 deletion rootfs/etc/services.d/mariadb/log/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
program_name="mariadb"

s6-format-filter "%1 %s" "[${program_name}]" | logutil-service /log/"${program_name}"
7 changes: 3 additions & 4 deletions rootfs/etc/services.d/mariadb/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
# ==============================================================================
# Mariadb
Expand All @@ -8,13 +8,12 @@
s6-notifyoncheck -n 1000 echo "Waiting for Mariadb to start" | info

# Wait until db is initially ready before polling
/bin/s6-svwait -U /var/run/s6/services/mariadb
s6-svwait -U /run/service/mariadb
echo "Mariadb is up! Proceeding to monitoring." | info

# Need to sleep to act like service is running
# Terminate container if db dies
until ! (/var/run/s6/services/mariadb/data/check); do
until ! (/run/service/mariadb/data/check); do
sleep 1
done
echo "Mariadb could not be reached! Exiting..." | error
echo -n "1" > /var/run/s6/container_environment/SHUTDOWN_REASON_DB_IS_DOWN
2 changes: 1 addition & 1 deletion rootfs/etc/services.d/nginx/log/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
program_name="nginx"

s6-format-filter "%1 %s" "[${program_name}]" | logutil-service /log/"${program_name}"
7 changes: 4 additions & 3 deletions rootfs/etc/services.d/nginx/run
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
# ==============================================================================
# Nginx
# Runs Nginx
# ==============================================================================

echo "Waiting for ZoneMinder to start" | info
/bin/s6-svwait -U /var/run/s6/services/zoneminder
s6-svwait -U /run/service/zoneminder

echo "Starting Nginx..." | info

exec fdmove -c 2 1 nginx -g 'daemon off;'
exec fdmove -c 2 1 \
/usr/sbin/nginx -g 'daemon off;'
2 changes: 1 addition & 1 deletion rootfs/etc/services.d/php-fpm/log/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
program_name="php-fpm"

s6-format-filter "%1 %s" "[${program_name}]" | logutil-service /log/"${program_name}"
2 changes: 1 addition & 1 deletion rootfs/etc/services.d/php-fpm/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
# ==============================================================================
# PHP-fpm
Expand Down
12 changes: 12 additions & 0 deletions rootfs/etc/services.d/socklog/log/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/command/with-contenv bash
program_name="zoneminder"

s6-tai64n | s6-tai64nlocal | s6-format-filter "%1 %s" "[${program_name}]" | \
s6-envuidgid -D 65534:65534 nobody \
s6-log -b -- \
- \
+local1.* \
n"${MAX_LOG_NUMBER}" \
s"${MAX_LOG_SIZE_BYTES}" \
1 \
/log/"${program_name}"
1 change: 1 addition & 0 deletions rootfs/etc/services.d/socklog/notification-fd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
8 changes: 8 additions & 0 deletions rootfs/etc/services.d/socklog/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/command/with-contenv bash
. "/usr/local/bin/logger"
# ==============================================================================
# Socklog
# Starts socklog to capture logs
# ==============================================================================

s6-socklog -d3 -t3000
20 changes: 3 additions & 17 deletions rootfs/etc/services.d/zoneminder/finish
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
# ==============================================================================
# ZoneMinder
# Take down the S6 supervision tree if db is no longer accessible
# ==============================================================================

# Must run pkill to terminate container if db is down because zmpkg.pl stop
# hangs in the event that the db is down when run
if [ "${SHUTDOWN_REASON_DB_IS_DOWN:-0}" -eq "1" ]; then
echo "Stopping ZoneMinder using pkill because db is down" | error
pkill -15 -f /usr/bin/zm
else
echo "Stopping ZoneMinder" | info
s6-setuidgid www-data /usr/bin/zmpkg.pl stop
fi
/run/s6/basedir/bin/halt

# Socklog randomly started hanging container shutdown at "Docker Build #461"
# The container will not stop until socklog is forcibly killed like this
# Dirty hack to allow the container to shutdown
echo "Stopping socklog" | error
pkill -9 -f socklog

s6-svscanctl -t /var/run/s6/services
s6-setuidgid www-data /usr/bin/zmpkg.pl stop
2 changes: 1 addition & 1 deletion rootfs/etc/services.d/zoneminder/log/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
program_name="zoneminder-service"

s6-format-filter "%1 %s" "[${program_name}]" | logutil-service /log/"${program_name}"
10 changes: 5 additions & 5 deletions rootfs/etc/services.d/zoneminder/run
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
. "/usr/local/bin/logger"
# ==============================================================================
# ZoneMinder
# Runs ZoneMinder
# ==============================================================================

echo "Waiting for Socklog to start" | info
s6-svwait -U /run/service/socklog
echo "Waiting for MariaDB to start" | info
/bin/s6-svwait -U /var/run/s6/services/mariadb
s6-svwait -U /run/service/mariadb
echo "Waiting for mariadb-configure to complete..." | info
/bin/s6-svwait -U /var/run/s6/services/mariadb-configure
s6-svwait -U /run/service/mariadb-configure

echo "Starting ZoneMinder..." | info
s6-setuidgid www-data /usr/bin/zmpkg.pl start
Expand All @@ -24,5 +26,3 @@ until [ "$(pgrep -fc /usr/bin/zm)" -lt "1" ]; do
sleep 1
done
echo "ZoneMinder has crashed! Exiting..." | error
# This is required or the container stop hangs
s6-svscanctl -t /var/run/s6/services
3 changes: 0 additions & 3 deletions rootfs/etc/socklog.rules/forward-stdout

This file was deleted.

6 changes: 0 additions & 6 deletions rootfs/etc/socklog.rules/zoneminder-log

This file was deleted.

0 comments on commit abf7b52

Please sign in to comment.