Skip to content

Commit 83c21ad

Browse files
committed
Changes related to runtime directory ownership while running under Docker
1 parent cf19147 commit 83c21ad

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

.github/release-notes/v2.4.5.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
### Release Notes
2+
3+
* While running under Docker, the script will now change the ownership of the runtime directory mounted into the container to be in line with the UID and GID values specified through environment variables `DRPP_UID` and `DRPP_GID`, if both are set (#77). Refer to the [UID and GID section](https://github.com/phin05/discord-rich-presence-plex/blob/v2.4.5/README.md#uid-and-gid) of the README for more information.
4+
5+
### Installation Instructions
6+
7+
* [Regular](https://github.com/phin05/discord-rich-presence-plex/blob/v2.4.5/README.md#installation)
8+
* [Docker](https://github.com/phin05/discord-rich-presence-plex/blob/v2.4.5/README.md#run-with-docker)

README.md

+27-7
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Images are available for the following platforms:
184184

185185
Mount a directory for persistent data (config file, cache file and log file) at `/app/data`.
186186

187-
The directory where Discord stores its inter-process communication Unix socket file needs to be mounted into the container at `/run/app`. The path for this would be the first non-null value from the values of the following environment variables: ([source](https://github.com/discord/discord-rpc/blob/963aa9f3e5ce81a4682c6ca3d136cddda614db33/src/connection_unix.cpp#L29C33-L29C33))
187+
The runtime directory where Discord stores its inter-process communication Unix socket file needs to be mounted into the container at `/run/app`. The path for this would be the first non-null value from the values of the following environment variables in the environment Discord is running in: ([source](https://github.com/discord/discord-rpc/blob/963aa9f3e5ce81a4682c6ca3d136cddda614db33/src/connection_unix.cpp#L29C33-L29C33))
188188

189189
* XDG_RUNTIME_DIR
190190
* TMPDIR
@@ -193,15 +193,35 @@ The directory where Discord stores its inter-process communication Unix socket f
193193

194194
If all four environment variables aren't set, `/tmp` is used.
195195

196-
For example, if the environment variable `XDG_RUNTIME_DIR` is set to `/run/user/1000`, that would be the directory that needs to be mounted into the container at `/run/app`. If none of the environment variables are set, you need to mount `/tmp` into the container at `/run/app`.
196+
For example, if the environment variable `XDG_RUNTIME_DIR` is set to `/run/user/1000`, that would be the runtime directory that needs to be mounted into the container at `/run/app`. If none of the environment variables are set, you need to mount `/tmp` into the container at `/run/app`.
197197

198-
### Example
198+
### UID and GID
199199

200-
```
201-
docker run -v ./drpp:/app/data -v /run/user/1000:/run/app:ro -d --restart unless-stopped --name drpp ghcr.io/phin05/discord-rich-presence-plex:latest
202-
```
200+
The environment variables `DRPP_UID` and `DRPP_GID` can be used to specify the UID and GID of the user Discord is running as. You can determine these by running `id` in your terminal as such user.
201+
202+
If both of the above environment variables are set, the script will change the ownership of `/run/app` and its contents to be in line with the specified UID and GID to prevent issues caused due to insufficient permissions. To skip this ownership change, set the environment variable `DRPP_NO_RUNTIME_DIR_CHOWN` to `true`. Skipping this is necessary only in cases where the runtime directory isn't dedicated exclusively to a single user.
203+
204+
The ownership of `/app` and its contents will be changed as well. If both of the above environment variables are set, they will determine the ownership. Otherwise, the existing ownership information of `/run/app` will be used.
203205

204-
If you're running the container for the first time (when there are no users in the config), make sure that the `DRPP_PLEX_SERVER_NAME_INPUT` environment variable is set (see the [environment variables](#configuration---environment-variables) section above), and check the container logs for the authentication link.
206+
### Other Info
207+
208+
If you're running the container for the first time (when there are no users in the config), set the `DRPP_PLEX_SERVER_NAME_INPUT` environment variable to the name of the Plex server to be added to the config file after user authentication, and check the container logs for the authentication link.
209+
210+
### Docker Compose example
211+
212+
```yaml
213+
services:
214+
drpp:
215+
container_name: drpp
216+
image: ghcr.io/phin05/discord-rich-presence-plex:latest
217+
restart: unless-stopped
218+
environment:
219+
DRPP_UID: 1000
220+
DRPP_GID: 1000
221+
volumes:
222+
- /run/user/1000:/run/app
223+
- ./drpp:/app/data
224+
```
205225
206226
### Containerised Discord
207227

config/constants.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33

44
name = "Discord Rich Presence for Plex"
5-
version = "2.4.4"
5+
version = "2.4.5"
66

77
plexClientID = "discord-rich-presence-plex"
88
discordClientID = "413407336082833418"
@@ -18,3 +18,7 @@
1818
isInContainer = os.environ.get("DRPP_IS_IN_CONTAINER", "") == "true"
1919
runtimeDirectory = "/run/app" if isInContainer else os.environ.get("XDG_RUNTIME_DIR", os.environ.get("TMPDIR", os.environ.get("TMP", os.environ.get("TEMP", "/tmp"))))
2020
ipcPipeBase = runtimeDirectory if isUnix else r"\\?\pipe"
21+
uid = int(os.environ.get("DRPP_UID", "-1"))
22+
gid = int(os.environ.get("DRPP_GID", "-1"))
23+
containerCwd = "/app"
24+
noRuntimeDirChown = os.environ.get("DRPP_NO_RUNTIME_DIR_CHOWN", "") == "true"

main.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from config.constants import isInContainer, runtimeDirectory
1+
from config.constants import isInContainer, runtimeDirectory, uid, gid, containerCwd, noRuntimeDirChown
22
from utils.logging import logger
33
import os
44
import sys
@@ -7,10 +7,22 @@
77
if not os.path.isdir(runtimeDirectory):
88
logger.error(f"Runtime directory does not exist. Ensure that it is mounted into the container at {runtimeDirectory}")
99
exit(1)
10-
statResult = os.stat(runtimeDirectory)
11-
os.system(f"chown -R {statResult.st_uid}:{statResult.st_gid} {os.path.dirname(os.path.realpath(__file__))}")
12-
os.setgid(statResult.st_gid) # pyright: ignore[reportGeneralTypeIssues,reportUnknownMemberType]
13-
os.setuid(statResult.st_uid) # pyright: ignore[reportGeneralTypeIssues,reportUnknownMemberType]
10+
if os.geteuid() == 0: # pyright: ignore[reportGeneralTypeIssues,reportUnknownMemberType]
11+
if uid == -1 or gid == -1:
12+
logger.warning(f"Environment variable(s) DRPP_UID and/or DRPP_GID are/is not set. Manually ensure appropriate ownership of {runtimeDirectory}")
13+
statResult = os.stat(runtimeDirectory)
14+
uid, gid = statResult.st_uid, statResult.st_gid
15+
else:
16+
if noRuntimeDirChown:
17+
logger.warning(f"DRPP_NO_RUNTIME_DIR_CHOWN is set to true. Manually ensure appropriate ownership of {runtimeDirectory}")
18+
else:
19+
os.system(f"chmod 700 {runtimeDirectory}")
20+
os.system(f"chown -R {uid}:{gid} {runtimeDirectory}")
21+
os.system(f"chown -R {uid}:{gid} {containerCwd}")
22+
os.setgid(gid) # pyright: ignore[reportGeneralTypeIssues,reportUnknownMemberType]
23+
os.setuid(uid) # pyright: ignore[reportGeneralTypeIssues,reportUnknownMemberType]
24+
else:
25+
logger.warning(f"Not running as the superuser. Manually ensure appropriate ownership of mounted contents")
1426
else:
1527
try:
1628
import subprocess

0 commit comments

Comments
 (0)