forked from NginxProxyManager/nginx-proxy-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support to run processes as a user/group, defined
with PUID and PGID environment variables - Detects if image is run with a user in docker command and fails if so - Adds s6 prepare scripts for adding a 'npmuser' - Split up and refactor the s6 prepare scripts - Runs nginx and backend node as 'npmuser' - Changes ownership of files required at startup
- Loading branch information
Showing
21 changed files
with
266 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
CYAN='\E[1;36m' | ||
BLUE='\E[1;34m' | ||
YELLOW='\E[1;33m' | ||
RED='\E[1;31m' | ||
RESET='\E[0m' | ||
export CYAN BLUE YELLOW RED RESET | ||
|
||
log_info () { | ||
echo -e "${BLUE}❯ ${CYAN}$1${RESET}" | ||
} | ||
|
||
log_error () { | ||
echo -e "${RED}❯ $1${RESET}" | ||
} | ||
|
||
# The `run` file will only execute 1 line so this helps keep things | ||
# logically separated | ||
|
||
log_fatal () { | ||
echo -e "${RED}--------------------------------------${RESET}" | ||
echo -e "${RED}ERROR: $1${RESET}" | ||
echo -e "${RED}--------------------------------------${RESET}" | ||
/run/s6/basedir/bin/halt | ||
exit 1 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/command/with-contenv bash | ||
# shellcheck shell=bash | ||
|
||
set -e | ||
|
||
. /bin/common.sh | ||
|
||
if [ "$(id -u)" != "0" ]; then | ||
log_fatal "This docker container must be run as root, do not specify a user.\nYou can specify PUID and PGID env vars to run processes as that user and group after initialization." | ||
fi | ||
|
||
. /etc/s6-overlay/s6-rc.d/prepare/10-npmuser.sh | ||
. /etc/s6-overlay/s6-rc.d/prepare/20-paths.sh | ||
. /etc/s6-overlay/s6-rc.d/prepare/30-ownership.sh | ||
. /etc/s6-overlay/s6-rc.d/prepare/40-dynamic.sh | ||
. /etc/s6-overlay/s6-rc.d/prepare/50-ipv6.sh | ||
. /etc/s6-overlay/s6-rc.d/prepare/60-secrets.sh | ||
. /etc/s6-overlay/s6-rc.d/prepare/90-banner.sh |
18 changes: 18 additions & 0 deletions
18
docker/rootfs/etc/s6-overlay/s6-rc.d/prepare/10-npmuser.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/command/with-contenv bash | ||
# shellcheck shell=bash | ||
|
||
set -e | ||
|
||
PUID=${PUID:-911} | ||
PGID=${PGID:-911} | ||
|
||
# Add npmuser user | ||
log_info 'Creating npmuser ...' | ||
|
||
groupmod -g 1000 users || exit 1 | ||
useradd -u "${PUID}" -U -d /data -s /bin/false npmuser || exit 1 | ||
usermod -G users npmuser || exit 1 | ||
groupmod -o -g "$PGID" npmuser || exit 1 | ||
# Home for npmuser | ||
mkdir -p /tmp/npmuserhome | ||
chown -R npmuser:npmuser /tmp/npmuserhome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/command/with-contenv bash | ||
# shellcheck shell=bash | ||
|
||
set -e | ||
|
||
log_info 'Checking paths ...' | ||
|
||
# Ensure /data is mounted | ||
if [ ! -d '/data' ]; then | ||
log_fatal '/data is not mounted! Check your docker configuration.' | ||
fi | ||
# Ensure /etc/letsencrypt is mounted | ||
if [ ! -d '/etc/letsencrypt' ]; then | ||
log_fatal '/etc/letsencrypt is not mounted! Check your docker configuration.' | ||
fi | ||
|
||
# Create required folders | ||
mkdir -p \ | ||
/data/nginx \ | ||
/data/custom_ssl \ | ||
/data/logs \ | ||
/data/access \ | ||
/data/nginx/default_host \ | ||
/data/nginx/default_www \ | ||
/data/nginx/proxy_host \ | ||
/data/nginx/redirection_host \ | ||
/data/nginx/stream \ | ||
/data/nginx/dead_host \ | ||
/data/nginx/temp \ | ||
/data/letsencrypt-acme-challenge \ | ||
/run/nginx \ | ||
/tmp/nginx/body \ | ||
/var/log/nginx \ | ||
/var/lib/nginx/cache/public \ | ||
/var/lib/nginx/cache/private \ | ||
/var/cache/nginx/proxy_temp | ||
|
||
touch /var/log/nginx/error.log || true | ||
chmod 777 /var/log/nginx/error.log || true | ||
chmod -R 777 /var/cache/nginx || true | ||
chmod 644 /etc/logrotate.d/nginx-proxy-manager |
21 changes: 21 additions & 0 deletions
21
docker/rootfs/etc/s6-overlay/s6-rc.d/prepare/30-ownership.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/command/with-contenv bash | ||
# shellcheck shell=bash | ||
|
||
set -e | ||
|
||
log_info 'Setting ownership ...' | ||
|
||
# root | ||
chown root /tmp/nginx | ||
|
||
# npmuser | ||
chown -R npmuser:npmuser \ | ||
/data \ | ||
/etc/letsencrypt \ | ||
/etc/nginx \ | ||
/run/nginx \ | ||
/tmp/nginx \ | ||
/var/cache/nginx \ | ||
/var/lib/logrotate \ | ||
/var/lib/nginx \ | ||
/var/log/nginx |
17 changes: 17 additions & 0 deletions
17
docker/rootfs/etc/s6-overlay/s6-rc.d/prepare/40-dynamic.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/command/with-contenv bash | ||
# shellcheck shell=bash | ||
|
||
set -e | ||
|
||
log_info 'Dynamic resolvers ...' | ||
|
||
DISABLE_IPV6=$(echo "${DISABLE_IPV6:-}" | tr '[:upper:]' '[:lower:]') | ||
|
||
# Dynamically generate resolvers file, if resolver is IPv6, enclose in `[]` | ||
# thanks @tfmm | ||
if [ "$DISABLE_IPV6" == "true" ] || [ "$DISABLE_IPV6" == "on" ] || [ "$DISABLE_IPV6" == "1" ] || [ "$DISABLE_IPV6" == "yes" ]; | ||
then | ||
echo resolver "$(awk 'BEGIN{ORS=" "} $1=="nameserver" { sub(/%.*$/,"",$2); print ($2 ~ ":")? "["$2"]": $2}' /etc/resolv.conf) ipv6=off valid=10s;" > /etc/nginx/conf.d/include/resolvers.conf | ||
else | ||
echo resolver "$(awk 'BEGIN{ORS=" "} $1=="nameserver" { sub(/%.*$/,"",$2); print ($2 ~ ":")? "["$2"]": $2}' /etc/resolv.conf) valid=10s;" > /etc/nginx/conf.d/include/resolvers.conf | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/bash | ||
|
||
# This command reads the `DISABLE_IPV6` env var and will either enable | ||
# or disable ipv6 in all nginx configs based on this setting. | ||
|
||
log_info 'IPv6 ...' | ||
|
||
# Lowercase | ||
DISABLE_IPV6=$(echo "${DISABLE_IPV6:-}" | tr '[:upper:]' '[:lower:]') | ||
|
||
process_folder () { | ||
FILES=$(find "$1" -type f -name "*.conf") | ||
SED_REGEX= | ||
|
||
if [ "$DISABLE_IPV6" == "true" ] || [ "$DISABLE_IPV6" == "on" ] || [ "$DISABLE_IPV6" == "1" ] || [ "$DISABLE_IPV6" == "yes" ]; then | ||
# IPV6 is disabled | ||
echo "Disabling IPV6 in hosts in: $1" | ||
SED_REGEX='s/^([^#]*)listen \[::\]/\1#listen [::]/g' | ||
else | ||
# IPV6 is enabled | ||
echo "Enabling IPV6 in hosts in: $1" | ||
SED_REGEX='s/^(\s*)#listen \[::\]/\1listen [::]/g' | ||
fi | ||
|
||
for FILE in $FILES | ||
do | ||
echo "- ${FILE}" | ||
sed -E -i "$SED_REGEX" "$FILE" | ||
done | ||
|
||
# ensure the files are still owned by the npmuser | ||
chown -R npmuser:npmuser "$1" | ||
} | ||
|
||
process_folder /etc/nginx/conf.d | ||
process_folder /data/nginx |
30 changes: 30 additions & 0 deletions
30
docker/rootfs/etc/s6-overlay/s6-rc.d/prepare/60-secrets.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/command/with-contenv bash | ||
# shellcheck shell=bash | ||
|
||
set -e | ||
|
||
# in s6, environmental variables are written as text files for s6 to monitor | ||
# search through full-path filenames for files ending in "__FILE" | ||
log_info 'Docker secrets ...' | ||
|
||
for FILENAME in $(find /var/run/s6/container_environment/ | grep "__FILE$"); do | ||
echo "[secret-init] Evaluating ${FILENAME##*/} ..." | ||
|
||
# set SECRETFILE to the contents of the full-path textfile | ||
SECRETFILE=$(cat "${FILENAME}") | ||
# if SECRETFILE exists / is not null | ||
if [[ -f "${SECRETFILE}" ]]; then | ||
# strip the appended "__FILE" from environmental variable name ... | ||
STRIPFILE=$(echo "${FILENAME}" | sed "s/__FILE//g") | ||
# echo "[secret-init] Set STRIPFILE to ${STRIPFILE}" # DEBUG - rm for prod! | ||
|
||
# ... and set value to contents of secretfile | ||
# since s6 uses text files, this is effectively "export ..." | ||
printf $(cat "${SECRETFILE}") > "${STRIPFILE}" | ||
# echo "[secret-init] Set ${STRIPFILE##*/} to $(cat ${STRIPFILE})" # DEBUG - rm for prod!" | ||
echo "Success: ${STRIPFILE##*/} set from ${FILENAME##*/}" | ||
|
||
else | ||
echo "Cannot find secret in ${FILENAME}" | ||
fi | ||
done |
Oops, something went wrong.