Skip to content

Commit c432c34

Browse files
committed
Small refactor of user/groups and add checks during startup. Only use -x in bash scripts when DEBUG=true set in env vars
1 parent a1245bc commit c432c34

File tree

14 files changed

+70
-45
lines changed

14 files changed

+70
-45
lines changed

docker/rootfs/bin/common.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export CYAN BLUE YELLOW RED RESET
1212
PUID=${PUID:-0}
1313
PGID=${PGID:-0}
1414

15+
NPMUSER=npm
16+
NPMGROUP=npm
17+
NPMHOME=/tmp/npmuserhome
18+
export NPMUSER NPMGROUP NPMHOME
19+
1520
if [[ "$PUID" -ne '0' ]] && [ "$PGID" = '0' ]; then
1621
# set group id to same as user id,
1722
# the user probably forgot to specify the group id and
@@ -40,3 +45,10 @@ log_fatal () {
4045
/run/s6/basedir/bin/halt
4146
exit 1
4247
}
48+
49+
# param $1: group_name
50+
get_group_id () {
51+
if [ "${1:-}" != '' ]; then
52+
getent group "$1" | cut -d: -f3
53+
fi
54+
}

docker/rootfs/etc/nginx/nginx.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# run nginx in foreground
22
daemon off;
33
pid /run/nginx/nginx.pid;
4-
user npmuser;
4+
user npm;
55

66
# Set number of worker processes automatically based on number of CPU cores.
77
worker_processes auto;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ cd /app || exit 1
1212
log_info 'Starting backend ...'
1313

1414
if [ "${DEVELOPMENT:-}" = 'true' ]; then
15-
s6-setuidgid npmuser yarn install
16-
exec s6-setuidgid npmuser bash -c 'export HOME=/tmp/npmuserhome;node --max_old_space_size=250 --abort_on_uncaught_exception node_modules/nodemon/bin/nodemon.js'
15+
s6-setuidgid "$PUID:$PGID" yarn install
16+
exec s6-setuidgid "$PUID:$PGID" bash -c "export HOME=$NPMHOME;node --max_old_space_size=250 --abort_on_uncaught_exception node_modules/nodemon/bin/nodemon.js"
1717
else
1818
while :
1919
do
20-
s6-setuidgid npmuser bash -c 'export HOME=/tmp/npmuserhome;node --abort_on_uncaught_exception --max_old_space_size=250 index.js'
20+
s6-setuidgid "$PUID:$PGID" bash -c "export HOME=$NPMHOME;node --abort_on_uncaught_exception --max_old_space_size=250 index.js"
2121
sleep 1
2222
done
2323
fi

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ set -e
88
if [ "$DEVELOPMENT" = 'true' ]; then
99
. /bin/common.sh
1010
cd /app/frontend || exit 1
11-
HOME=/tmp/npmuserhome
11+
HOME=$NPMHOME
1212
export HOME
1313
mkdir -p /app/frontend/dist
1414
chown -R "$PUID:$PGID" /app/frontend/dist
1515

1616
log_info 'Starting frontend ...'
17-
s6-setuidgid npmuser yarn install
18-
exec s6-setuidgid npmuser yarn watch
17+
s6-setuidgid "$PUID:$PGID" yarn install
18+
exec s6-setuidgid "$PUID:$PGID" yarn watch
1919
else
2020
exit 0
2121
fi

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ set -e
66
. /bin/common.sh
77

88
log_info 'Starting nginx ...'
9-
exec s6-setuidgid npmuser nginx
9+
exec s6-setuidgid "$PUID:$PGID" nginx

docker/rootfs/etc/s6-overlay/s6-rc.d/prepare/00-all.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ if [ "$(id -u)" != "0" ]; then
99
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."
1010
fi
1111

12-
. /etc/s6-overlay/s6-rc.d/prepare/10-npmuser.sh
12+
if [ "$DEBUG" = "true" ]; then
13+
set -x
14+
fi
15+
16+
. /etc/s6-overlay/s6-rc.d/prepare/10-usergroup.sh
1317
. /etc/s6-overlay/s6-rc.d/prepare/20-paths.sh
1418
. /etc/s6-overlay/s6-rc.d/prepare/30-ownership.sh
1519
. /etc/s6-overlay/s6-rc.d/prepare/40-dynamic.sh

docker/rootfs/etc/s6-overlay/s6-rc.d/prepare/10-npmuser.sh

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/command/with-contenv bash
2+
# shellcheck shell=bash
3+
4+
set -e
5+
6+
log_info "Configuring $NPMUSER user ..."
7+
8+
if id -u "$NPMUSER" 2>/dev/null; then
9+
# user already exists
10+
usermod -u "$PUID" "$NPMUSER"
11+
else
12+
# Add user
13+
useradd -o -u "$PUID" -U -d "$NPMHOME" -s /bin/false "$NPMUSER"
14+
fi
15+
16+
log_info "Configuring $NPMGROUP group ..."
17+
if [ "$(get_group_id "$NPMGROUP")" = '' ]; then
18+
# Add group. This will not set the id properly if it's already taken
19+
groupadd -f -g "$PGID" "$NPMGROUP"
20+
else
21+
groupmod -o -g "$PGID" "$NPMGROUP"
22+
fi
23+
24+
# Set the group ID and check it
25+
groupmod -o -g "$PGID" "$NPMGROUP"
26+
if [ "$(get_group_id "$NPMGROUP")" != "$PGID" ]; then
27+
echo "ERROR: Unable to set group id properly"
28+
exit 1
29+
fi
30+
31+
# Set the group against the user and check it
32+
usermod -G "$PGID" "$NPMGROUP"
33+
if [ "$(id -g "$NPMUSER")" != "$PGID" ] ; then
34+
echo "ERROR: Unable to set group against the user properly"
35+
exit 1
36+
fi
37+
38+
# Home for user
39+
mkdir -p "$NPMHOME"
40+
chown -R "$PUID:$PGID" "$NPMHOME"

docker/rootfs/etc/s6-overlay/s6-rc.d/prepare/20-paths.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
# shellcheck shell=bash
33

44
set -e
5-
# verbose
6-
set -x
75

86
log_info 'Checking paths ...'
97

docker/rootfs/etc/s6-overlay/s6-rc.d/prepare/30-ownership.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
# shellcheck shell=bash
33

44
set -e
5-
# verbose
6-
set -x
75

86
log_info 'Setting ownership ...'
97

108
# root
119
chown root /tmp/nginx
1210

13-
# npmuser
11+
# npm user and group
1412
chown -R "$PUID:$PGID" /data
1513
chown -R "$PUID:$PGID" /etc/letsencrypt
1614
chown -R "$PUID:$PGID" /run/nginx

0 commit comments

Comments
 (0)