Skip to content

Commit dad3e1d

Browse files
committed
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
1 parent 82d9452 commit dad3e1d

File tree

21 files changed

+266
-152
lines changed

21 files changed

+266
-152
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ services:
7070
- ./letsencrypt:/etc/letsencrypt
7171
```
7272
73+
This is the bare minimum configuration required. See the [documentation](https://nginxproxymanager.com/setup/) for more.
74+
7375
3. Bring up your stack by running
7476
7577
```bash

backend/internal/certificate.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const internalCertificate = {
4646

4747
const cmd = certbotCommand + ' renew --non-interactive --quiet ' +
4848
'--config "' + letsencryptConfig + '" ' +
49+
'--work-dir "/tmp/letsencrypt-lib" ' +
50+
'--logs-dir "/tmp/letsencrypt-log" ' +
4951
'--preferred-challenges "dns,http" ' +
5052
'--disable-hook-validation ' +
5153
(letsencryptStaging ? '--staging' : '');
@@ -833,6 +835,8 @@ const internalCertificate = {
833835

834836
const cmd = certbotCommand + ' certonly ' +
835837
'--config "' + letsencryptConfig + '" ' +
838+
'--work-dir "/tmp/letsencrypt-lib" ' +
839+
'--logs-dir "/tmp/letsencrypt-log" ' +
836840
'--cert-name "npm-' + certificate.id + '" ' +
837841
'--agree-tos ' +
838842
'--authenticator webroot ' +
@@ -878,6 +882,8 @@ const internalCertificate = {
878882

879883
let mainCmd = certbotCommand + ' certonly ' +
880884
'--config "' + letsencryptConfig + '" ' +
885+
'--work-dir "/tmp/letsencrypt-lib" ' +
886+
'--logs-dir "/tmp/letsencrypt-log" ' +
881887
'--cert-name "npm-' + certificate.id + '" ' +
882888
'--agree-tos ' +
883889
'--email "' + certificate.meta.letsencrypt_email + '" ' +

docker/docker-compose.dev.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ services:
1414
networks:
1515
- nginx_proxy_manager
1616
environment:
17+
PUID: 1000
18+
PGID: 1000
1719
NODE_ENV: "development"
1820
FORCE_COLOR: 1
1921
DEVELOPMENT: "true"

docker/rootfs/bin/common.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
CYAN='\E[1;36m'
6+
BLUE='\E[1;34m'
7+
YELLOW='\E[1;33m'
8+
RED='\E[1;31m'
9+
RESET='\E[0m'
10+
export CYAN BLUE YELLOW RED RESET
11+
12+
log_info () {
13+
echo -e "${BLUE}${CYAN}$1${RESET}"
14+
}
15+
16+
log_error () {
17+
echo -e "${RED}$1${RESET}"
18+
}
19+
20+
# The `run` file will only execute 1 line so this helps keep things
21+
# logically separated
22+
23+
log_fatal () {
24+
echo -e "${RED}--------------------------------------${RESET}"
25+
echo -e "${RED}ERROR: $1${RESET}"
26+
echo -e "${RED}--------------------------------------${RESET}"
27+
/run/s6/basedir/bin/halt
28+
exit 1
29+
}

docker/rootfs/bin/handle-ipv6-setting

Lines changed: 0 additions & 46 deletions
This file was deleted.

docker/rootfs/etc/nginx/nginx.conf

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# run nginx in foreground
22
daemon off;
3-
4-
user root;
3+
pid /run/nginx/nginx.pid;
54

65
# Set number of worker processes automatically based on number of CPU cores.
76
worker_processes auto;
@@ -57,7 +56,7 @@ http {
5756
}
5857

5958
# Real IP Determination
60-
59+
6160
# Local subnets:
6261
set_real_ip_from 10.0.0.0/8;
6362
set_real_ip_from 172.16.0.0/12; # Includes Docker subnet

docker/rootfs/etc/s6-overlay/s6-rc.d/backend/run

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33

44
set -e
55

6-
echo "❯ Starting backend ..."
6+
. /bin/common.sh
7+
8+
log_info 'Starting backend ...'
9+
710
if [ "$DEVELOPMENT" == "true" ]; then
811
cd /app || exit 1
912
# If yarn install fails: add --verbose --network-concurrency 1
10-
yarn install
11-
node --max_old_space_size=250 --abort_on_uncaught_exception node_modules/nodemon/bin/nodemon.js
13+
s6-setuidgid npmuser yarn install
14+
exec s6-setuidgid npmuser node --max_old_space_size=250 --abort_on_uncaught_exception node_modules/nodemon/bin/nodemon.js
1215
else
1316
cd /app || exit 1
1417
while :
1518
do
16-
node --abort_on_uncaught_exception --max_old_space_size=250 index.js
19+
s6-setuidgid npmuser node --abort_on_uncaught_exception --max_old_space_size=250 index.js
1720
sleep 1
1821
done
1922
fi

docker/rootfs/etc/s6-overlay/s6-rc.d/frontend/run

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ set -e
66
# This service is DEVELOPMENT only.
77

88
if [ "$DEVELOPMENT" == "true" ]; then
9+
. /bin/common.sh
910
cd /app/frontend || exit 1
11+
log_info 'Starting frontend ...'
12+
HOME=/tmp/npmuserhome
13+
export HOME
14+
mkdir -p /app/frontend/dist
15+
chown -R npmuser:npmuser /app/frontend/dist
1016
# If yarn install fails: add --verbose --network-concurrency 1
11-
yarn install
12-
yarn watch
17+
s6-setuidgid npmuser yarn install
18+
exec s6-setuidgid npmuser yarn watch
1319
else
1420
exit 0
1521
fi

docker/rootfs/etc/s6-overlay/s6-rc.d/nginx/run

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33

44
set -e
55

6-
echo "❯ Starting nginx ..."
7-
exec nginx
6+
. /bin/common.sh
7+
8+
log_info 'Starting nginx ...'
9+
10+
exec s6-setuidgid npmuser nginx
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/command/with-contenv bash
2+
# shellcheck shell=bash
3+
4+
set -e
5+
6+
. /bin/common.sh
7+
8+
if [ "$(id -u)" != "0" ]; then
9+
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."
10+
fi
11+
12+
. /etc/s6-overlay/s6-rc.d/prepare/10-npmuser.sh
13+
. /etc/s6-overlay/s6-rc.d/prepare/20-paths.sh
14+
. /etc/s6-overlay/s6-rc.d/prepare/30-ownership.sh
15+
. /etc/s6-overlay/s6-rc.d/prepare/40-dynamic.sh
16+
. /etc/s6-overlay/s6-rc.d/prepare/50-ipv6.sh
17+
. /etc/s6-overlay/s6-rc.d/prepare/60-secrets.sh
18+
. /etc/s6-overlay/s6-rc.d/prepare/90-banner.sh

0 commit comments

Comments
 (0)