Skip to content

Commit

Permalink
allow setting user flag at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
brchri committed Oct 8, 2023
1 parent fa98eb7 commit 57427b0
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
#!/bin/bash

# if non-root user specified at runtime with user flag, just execute CMD and exit
if [ $(id -u) -ne 0 ]; then
exec "$@"
return
fi

# OUID and OGID are the original user and group ids set during the image
# build and are replaced here at runtime if PGID and PUID are set
if [ -n "$PGID" ] && [ "$PGID" -ne 0 ]; then
sed -i "s/nonroot:x:$OUID:$OGID:/nonroot:x:$OUID:$PGID:/" /etc/passwd
sed -i "s/nonroot:x:$OGID:/nonroot:x:$PGID:/" /etc/group
if [ -n "$PGID" ] && [ "$PGID" -ne 0 ] && [ "$PGID" -ne "$OGID" ] ; then
# if group id doesn't already exist, set nonroot gid = $PGID, otherwise just use existing group name for $PGID
if ! grep ":x:$PGID:" /etc/group 2>&1 >/dev/null; then
sed -i "s/nonroot:x:$OUID:$OGID:/nonroot:x:$OUID:$PGID:/" /etc/passwd
sed -i "s/nonroot:x:$OGID:/nonroot:x:$PGID:/" /etc/group
fi
fi

if [ -n "$PUID" ] && [ "$PUID" -ne 0 ]; then
sed -i "s/nonroot:x:$OUID:/nonroot:x:$PUID:/" /etc/passwd
if [ -n "$PUID" ] && [ "$PUID" -ne 0 ] && [ "$PUID" -ne "$OUID" ]; then
# if user id doesn't already exist, set nonroot uid = $PUID, otherwise just use existing username for $PUID
if ! grep ":x:$PUID:" /etc/passwd 2>&1 >/dev/null; then
sed -i "s/nonroot:x:$OUID:/nonroot:x:$PUID:/" /etc/passwd
fi
fi

chown nonroot: /app /app/*
chown $PUID:$PGID /app /app/*

# Use su-exec to execute the command as nonroot user
exec su-exec nonroot "$@"
exec su-exec $PUID:$PGID "$@"

0 comments on commit 57427b0

Please sign in to comment.