feat: added gosu & updated run.sh to switch to the dedicated user (#1395)

Co-authored-by: Maka0 <360614-Maka0@users.noreply.gitlab.com>
This commit is contained in:
Maka0 2022-08-09 04:39:59 +02:00 committed by GitHub
parent 7ce02c31d5
commit a7c6e89dfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 7 deletions

View File

@ -97,7 +97,7 @@ ENV GIT_COMMIT_HASH=$COMMIT
# curl for used by healthcheck # curl for used by healthcheck
RUN apt-get update \ RUN apt-get update \
&& apt-get install --no-install-recommends -y \ && apt-get install --no-install-recommends -y \
curl \ curl gosu \
&& apt-get autoremove \ && apt-get autoremove \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*

View File

@ -12,17 +12,28 @@ PGID=${PGID:-911}
add_user() { add_user() {
groupmod -o -g "$PGID" abc groupmod -o -g "$PGID" abc
usermod -o -u "$PUID" abc usermod -o -u "$PUID" abc
}
change_user() {
# If container is started as root then create a new user and switch to it
if [ "$(id -u)" = "0" ]; then
add_user
chown -R $PUID:$PGID /app
echo "Switching to dedicated user"
exec gosu $PUID "$BASH_SOURCE" "$@"
elif [ "$(id -u)" = $PUID ]; then
echo " echo "
User uid: $(id -u abc) User uid: $PUID
User gid: $(id -g abc) User gid: $PGID
" "
chown -R abc:abc /app fi
} }
init() { init() {
# $MEALIE_HOME directory # $MEALIE_HOME directory
cd /app cd /app
# Activate our virtual environment here # Activate our virtual environment here
. /opt/pysetup/.venv/bin/activate . /opt/pysetup/.venv/bin/activate
@ -45,7 +56,8 @@ if [ "$ARG1" == "reload" ]; then
else else
echo "Production" echo "Production"
add_user change_user
init init
# Start API # Start API