Skip to content

Commit

Permalink
Allow run as user #10 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
GioF71 authored Dec 20, 2023
1 parent 23472aa commit eb0519c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 39 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,38 @@ services:
restart: unless-stopped
```

With images built after (and including) 2023-12-20, you can specify the username with a `user: "1000:1000"` in your compose file instead of setting PUID and PGID.
The previous compose file would become:

```text
---
version: "3"
services:
minidlna-desktop:
image: giof71/minidlna
container_name: minidlna-desktop
network_mode: host
user: "1000:1000"
environment:
- MINIDLNA_ROOT_CONTAINER=M
- MINIDLNA_DIR_A_1=/music/library1
- MINIDLNA_DIR_A_2=/music/library2
- MINIDLNA_DIR_A_3=/music/library3
- MINIDLNA_ENABLE_INOTIFY=YES
- MINIDLNA_FRIENDLY_NAME=minidlna-desktop
- MINIDLNA_FORCE_SORT_CRITERIA=+upnp:class,-dc:date,+upnp:album,+upnp:originalTrackNumber,+dc:title
volumes:
- /mnt/disk1/library:/music/library1
- /mnt/disk2/library:/music/library2
- /mnt/disk3/library:/music/library3
- ./config/log:/log
- ./config/db:/db
restart: unless-stopped
```

In this case, make sure that the directories you bind exist and are writable from the specified uid/gid.

## Build

You can build (or rebuild) the image by opening a terminal from the root of the repository and issuing the following command:
Expand All @@ -133,6 +165,7 @@ Just be careful to use the tag you have built.

Date|Major Changes
:---|:---
2023-12-20|Allow docker user mode, see [#10](https://github.com/GioF71/minidlna-docker/issues/10)
2023-09-13|Switch to debian stable, see [#8](https://github.com/GioF71/minidlna-docker/issues/8)
2023-09-13|Add support to notify interval, see [#6](https://github.com/GioF71/minidlna-docker/issues/6)
2023-07-24|Switch to bookworm, see [#2](https://github.com/GioF71/minidlna-docker/issues/2)
Expand Down
82 changes: 43 additions & 39 deletions app/bin/run-minidlna.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ DEFAULT_MINIDLNA_PORT=8200
DEFAULT_UID=1000
DEFAULT_GID=1000

CONFIG_FILE=/app/conf/minidlna.conf
current_user_id=$(id -u)
echo "Current user id is [$current_user_id]"

CONFIG_FILE=/tmp/minidlna.conf

echo "# MINIDLNA CONFIG" > $CONFIG_FILE

Expand Down Expand Up @@ -90,43 +93,44 @@ if [ -n "${MINIDLNA_STRICT_DLNA}" ]; then
fi

USE_USER_MODE=N

if [ -n "${PUID}" ] || [ [ "${USER_MODE^^}" = "Y" ] || [ "${USER_MODE^^}" = "YES" ] ]; then
USE_USER_MODE=Y
if [ -z "${PUID}" ]; then
PUID=$DEFAULT_UID;
echo "Setting default value for PUID: ["$PUID"]"
fi
if [ -z "${PGID}" ]; then
PGID=$DEFAULT_GID;
echo "Setting default value for PGID: ["$PGID"]"
fi
USER_NAME=minidlna-user
GROUP_NAME=minidlna-user
HOME_DIR=/home/$USER_NAME
### create home directory and ancillary directories
if [ ! -d "$HOME_DIR" ]; then
echo "Home directory [$HOME_DIR] not found, creating."
mkdir -p $HOME_DIR
chown -R $PUID:$PGID $HOME_DIR
ls -la $HOME_DIR -d
ls -la $HOME_DIR
fi
### create group
if [ ! $(getent group $GROUP_NAME) ]; then
echo "group $GROUP_NAME does not exist, creating..."
groupadd -g $PGID $GROUP_NAME
else
echo "group $GROUP_NAME already exists."
fi
### create user
if [ ! $(getent passwd $USER_NAME) ]; then
echo "user $USER_NAME does not exist, creating..."
useradd -g $PGID -u $PUID -s /bin/bash -M -d $HOME_DIR $USER_NAME
id $USER_NAME
echo "user $USER_NAME created."
else
echo "user $USER_NAME already exists."
if [[ $current_user_id -eq 0 ]]; then
if [ -n "${PUID}" ] || [ [ "${USER_MODE^^}" = "Y" ] || [ "${USER_MODE^^}" = "YES" ] ]; then
USE_USER_MODE=Y
if [ -z "${PUID}" ]; then
PUID=$DEFAULT_UID;
echo "Setting default value for PUID: ["$PUID"]"
fi
if [ -z "${PGID}" ]; then
PGID=$DEFAULT_GID;
echo "Setting default value for PGID: ["$PGID"]"
fi
USER_NAME=minidlna-user
GROUP_NAME=minidlna-user
HOME_DIR=/home/$USER_NAME
### create home directory and ancillary directories
if [ ! -d "$HOME_DIR" ]; then
echo "Home directory [$HOME_DIR] not found, creating."
mkdir -p $HOME_DIR
chown -R $PUID:$PGID $HOME_DIR
ls -la $HOME_DIR -d
ls -la $HOME_DIR
fi
### create group
if [ ! $(getent group $GROUP_NAME) ]; then
echo "group $GROUP_NAME does not exist, creating..."
groupadd -g $PGID $GROUP_NAME
else
echo "group $GROUP_NAME already exists."
fi
### create user
if [ ! $(getent passwd $USER_NAME) ]; then
echo "user $USER_NAME does not exist, creating..."
useradd -g $PGID -u $PUID -s /bin/bash -M -d $HOME_DIR $USER_NAME
id $USER_NAME
echo "user $USER_NAME created."
else
echo "user $USER_NAME already exists."
fi
fi
fi

Expand All @@ -137,7 +141,7 @@ fi

cat $CONFIG_FILE

CMD_LINE="/usr/sbin/minidlnad -S -f $CONFIG_FILE -P /app/minidlnad.pid"
CMD_LINE="/usr/sbin/minidlnad -S -f $CONFIG_FILE -P /tmp/minidlnad.pid"
echo "CMD_LINE=$CMD_LINE"

echo "USER_MODE=[${USE_USER_MODE}]"
Expand Down

0 comments on commit eb0519c

Please sign in to comment.