-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-as-user.sh
32 lines (28 loc) · 892 Bytes
/
run-as-user.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env bash
# Runs a command as the LOCAL_USERID if passed in, otherwise stick with root
# Check the UID
if [ "$USERID" ] \
&& [ "$USERID" != "root" ] \
&& [ "$USERID" -gt 1 ]
then
# Create the user
useradd --create-home --shell /bin/bash --uid "$USERID" dockeruser
# Check the GID
if [ "$GROUPID" ] && [ "$GROUPID" -gt 0 ]
then
# Create the group
groupName=$(getent group "$GROUPID" | grep -Po '^.+?(?=:)')
if [ ! "$groupName" ]; then
groupName='dockeruser'
groupadd --gid "$GROUPID" "$groupName"
fi
adduser --quiet dockeruser "$groupName"
fi
# Switch to the user
echo "Running as user dockeruser (uid $USERID) and group $groupName the CMD: $@"
export HOME=/home/dockeruser
exec /usr/local/bin/gosu dockeruser "$@"
else
echo "Running as root the CMD: $@"
exec "$@"
fi