Skip to content

Commit a7d6b14

Browse files
authored
[Enhancement] Support docker user --user mode #77 (#78)
1 parent 106510c commit a7d6b14

2 files changed

Lines changed: 86 additions & 80 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ Just be careful to use the tag you have built.
342342

343343
Change Date|Major Changes
344344
---|---
345+
2023-12-20|Support docker --user mode (see [#77](https://github.com/GioF71/librespot-docker/issues/77))
345346
2023-10-06|Change ownership of volumes (see [#75](https://github.com/GioF71/librespot-docker/issues/75))
346347
2023-09-05|Clean Dockerfile (see [#73](https://github.com/GioF71/librespot-docker/issues/73))
347348
2023-06-23|Pass device name in quotes (see [#67](https://github.com/GioF71/librespot-docker/issues/67))

app/bin/run-librespot.sh

Lines changed: 85 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
22

3+
current_user_id=$(id -u)
4+
echo "Current user id is [$current_user_id]"
5+
36
DEFAULT_STARTUP_DELAY_SEC=0
47

58
declare -A file_dict
@@ -19,85 +22,86 @@ CMD_LINE="/usr/bin/librespot"
1922
DEFAULT_UID=1000
2023
DEFAULT_GID=1000
2124

22-
if [[ "${BACKEND}" == "pulseaudio" && -z "${PUID}" ]]; then
23-
PUID=$DEFAULT_UID;
24-
echo "Setting default value for PUID: ["$PUID"]"
25-
if [[ -z "${PGID}" ]]; then
26-
PGID=$DEFAULT_GID;
27-
echo "Also setting default value for PGID: ["$PGID"]"
28-
fi
29-
fi
30-
31-
if [[ "${BACKEND}" == "pulseaudio" && -z "${PGID}" ]]; then
32-
PGID=$DEFAULT_GID;
33-
echo "Setting default value for PGID: ["$PGID"]"
34-
fi
35-
36-
USER_NAME=librespot-user
37-
GROUP_NAME=librespot-group
38-
39-
HOME_DIR=/home/$USER_NAME
40-
41-
# handle user mode
42-
if [[ -n "${PUID}" && -n "${PUID}" ]]; then
43-
echo "Ensuring user with uid:[$PUID] gid:[$PGID] exists ...";
44-
### create group if it does not exist
45-
if [ ! $(getent group $PGID) ]; then
46-
echo "Group with gid [$PGID] does not exist, creating..."
47-
groupadd -g $PGID $GROUP_NAME
48-
echo "Group [$GROUP_NAME] with gid [$PGID] created."
49-
else
50-
GROUP_NAME=$(getent group $PGID | cut -d: -f1)
51-
echo "Group with gid [$PGID] name [$GROUP_NAME] already exists."
52-
fi
53-
### create user if it does not exist
54-
if [ ! $(getent passwd $PUID) ]; then
55-
echo "User with uid [$PUID] does not exist, creating..."
56-
useradd -g $PGID -u $PUID -M $USER_NAME
57-
echo "User [$USER_NAME] with uid [$PUID] created."
58-
else
59-
USER_NAME=$(getent passwd $PUID | cut -d: -f1)
60-
echo "user with uid [$PUID] name [$USER_NAME] already exists."
61-
HOME_DIR="/home/$USER_NAME"
25+
if [[ $current_user_id -eq 0 ]]; then
26+
if [[ "${BACKEND}" == "pulseaudio" && -z "${PUID}" ]]; then
27+
PUID=$DEFAULT_UID;
28+
echo "Setting default value for PUID: ["$PUID"]"
29+
if [[ -z "${PGID}" ]]; then
30+
PGID=$DEFAULT_GID;
31+
echo "Also setting default value for PGID: ["$PGID"]"
6232
fi
63-
### create home directory
64-
if [ ! -d "$HOME_DIR" ]; then
65-
echo "Home directory [$HOME_DIR] not found, creating."
66-
mkdir -p $HOME_DIR
67-
echo ". done."
6833
fi
69-
chown -R $PUID:$PGID $HOME_DIR
70-
if [ -z "${AUDIO_GID}" ]; then
71-
echo "WARNING: AUDIO_GID is mandatory for user mode and alsa backend"
72-
else
73-
if [ $(getent group $AUDIO_GID) ]; then
74-
echo "Alsa Mode - Group with gid $AUDIO_GID already exists"
75-
else
76-
echo "Alsa Mode - Creating group with gid $AUDIO_GID"
77-
groupadd -g $AUDIO_GID sq-audio
78-
fi
79-
echo "Alsa Mode - Adding $USER_NAME [$PUID] to gid [$AUDIO_GID]"
80-
AUDIO_GRP=$(getent group $AUDIO_GID | cut -d: -f1)
81-
echo "gid $AUDIO_GID -> group $AUDIO_GRP"
82-
usermod -a -G $AUDIO_GRP $USER_NAME
83-
echo "Alsa Mode - Successfully created user $USER_NAME:$GROUP_NAME [$PUID:$PGID])";
34+
35+
if [[ "${BACKEND}" == "pulseaudio" && -z "${PGID}" ]]; then
36+
PGID=$DEFAULT_GID;
37+
echo "Setting default value for PGID: ["$PGID"]"
8438
fi
85-
# set ownership on volumes
86-
chown -R $PUID:$PGID /data/cache
87-
chown -R $PUID:$PGID /data/system-cache
88-
chown -R $PUID:$PGID /user/config
89-
fi
9039

91-
PULSE_CLIENT_CONF="/etc/pulse/client.conf"
40+
USER_NAME=librespot-user
41+
GROUP_NAME=librespot-group
42+
43+
HOME_DIR=/home/$USER_NAME
44+
45+
# handle user mode
46+
if [[ -n "${PUID}" && -n "${PUID}" ]]; then
47+
echo "Ensuring user with uid:[$PUID] gid:[$PGID] exists ...";
48+
### create group if it does not exist
49+
if [ ! $(getent group $PGID) ]; then
50+
echo "Group with gid [$PGID] does not exist, creating..."
51+
groupadd -g $PGID $GROUP_NAME
52+
echo "Group [$GROUP_NAME] with gid [$PGID] created."
53+
else
54+
GROUP_NAME=$(getent group $PGID | cut -d: -f1)
55+
echo "Group with gid [$PGID] name [$GROUP_NAME] already exists."
56+
fi
57+
### create user if it does not exist
58+
if [ ! $(getent passwd $PUID) ]; then
59+
echo "User with uid [$PUID] does not exist, creating..."
60+
useradd -g $PGID -u $PUID -M $USER_NAME
61+
echo "User [$USER_NAME] with uid [$PUID] created."
62+
else
63+
USER_NAME=$(getent passwd $PUID | cut -d: -f1)
64+
echo "user with uid [$PUID] name [$USER_NAME] already exists."
65+
HOME_DIR="/home/$USER_NAME"
66+
fi
67+
### create home directory
68+
if [ ! -d "$HOME_DIR" ]; then
69+
echo "Home directory [$HOME_DIR] not found, creating."
70+
mkdir -p $HOME_DIR
71+
echo ". done."
72+
fi
73+
chown -R $PUID:$PGID $HOME_DIR
74+
if [ -z "${AUDIO_GID}" ]; then
75+
echo "WARNING: AUDIO_GID is mandatory for user mode and alsa backend"
76+
else
77+
if [ $(getent group $AUDIO_GID) ]; then
78+
echo "Alsa Mode - Group with gid $AUDIO_GID already exists"
79+
else
80+
echo "Alsa Mode - Creating group with gid $AUDIO_GID"
81+
groupadd -g $AUDIO_GID sq-audio
82+
fi
83+
echo "Alsa Mode - Adding $USER_NAME [$PUID] to gid [$AUDIO_GID]"
84+
AUDIO_GRP=$(getent group $AUDIO_GID | cut -d: -f1)
85+
echo "gid $AUDIO_GID -> group $AUDIO_GRP"
86+
usermod -a -G $AUDIO_GRP $USER_NAME
87+
echo "Alsa Mode - Successfully created user $USER_NAME:$GROUP_NAME [$PUID:$PGID])";
88+
fi
89+
# set ownership on volumes
90+
chown -R $PUID:$PGID /data/cache
91+
chown -R $PUID:$PGID /data/system-cache
92+
chown -R $PUID:$PGID /user/config
93+
fi
9294

93-
echo "cat /app/assets/pulse-client-template.conf"
94-
cat /app/assets/pulse-client-template.conf
95+
PULSE_CLIENT_CONF="/etc/pulse/client.conf"
9596

96-
echo "Creating pulseaudio configuration file $PULSE_CLIENT_CONF..."
97-
cp /app/assets/pulse-client-template.conf $PULSE_CLIENT_CONF
98-
sed -i 's/PUID/'"$PUID"'/g' $PULSE_CLIENT_CONF
99-
cat $PULSE_CLIENT_CONF
97+
echo "cat /app/assets/pulse-client-template.conf"
98+
cat /app/assets/pulse-client-template.conf
10099

100+
echo "Creating pulseaudio configuration file $PULSE_CLIENT_CONF..."
101+
cp /app/assets/pulse-client-template.conf $PULSE_CLIENT_CONF
102+
sed -i 's/PUID/'"$PUID"'/g' $PULSE_CLIENT_CONF
103+
cat $PULSE_CLIENT_CONF
104+
fi
101105

102106
if [ -n "$SPOTIFY_USERNAME" ]; then
103107
CMD_LINE="$CMD_LINE --username '$SPOTIFY_USERNAME'"
@@ -260,16 +264,17 @@ if [[ -z "${LOG_COMMAND_LINE}" || "${LOG_COMMAND_LINE^^}" = "Y" ]]; then
260264
safe=$(echo "${safe/"$SPOTIFY_USERNAME"/"$some_asterisks"}")
261265
safe=$(echo "${safe/"$SPOTIFY_PASSWORD"/"$some_asterisks"}")
262266
echo "Command Line: [$safe]"
263-
264-
#echo "Command Line: ["$CMD_LINE"]"
265-
266267
fi
267268

268-
if [[ "${BACKEND}" = "pulseaudio" || -n "${PUID}" ]]; then
269-
echo "Running in user mode ..."
270-
su - $USER_NAME -c "$CMD_LINE";
269+
if [[ $current_user_id -eq 0 ]]; then
270+
if [[ "${BACKEND}" = "pulseaudio" || -n "${PUID}" ]]; then
271+
echo "Running in user mode ..."
272+
su - $USER_NAME -c "$CMD_LINE";
273+
else
274+
echo "Running as root ..."
275+
eval $CMD_LINE;
276+
fi
271277
else
272-
echo "Running as root ..."
278+
echo "Running as uid: [$current_user_id] ..."
273279
eval $CMD_LINE;
274280
fi
275-

0 commit comments

Comments
 (0)