From 9296f88bbb2c0629ada4f5e6823aa90b704c45a6 Mon Sep 17 00:00:00 2001 From: Kristoffer Berdal Date: Tue, 9 Sep 2025 22:45:14 +0200 Subject: [PATCH] allow for UID/GID specification via PUID/PGID env variables --- Dockerfile | 6 ++-- entrypoint.sh | 87 ++++++++++++++------------------------------------- startgame.sh | 70 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 65 deletions(-) create mode 100755 startgame.sh diff --git a/Dockerfile b/Dockerfile index 246819a..038bc6a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -61,7 +61,9 @@ RUN mkdir -p /config \ zlib1g \ liblzma5 \ liblzo2-2 \ - nano + nano \ + gosu \ + && gosu nobody true WORKDIR /config @@ -69,6 +71,7 @@ WORKDIR /config COPY --from=ottd_build /app /app # Add the entrypoint +ADD startgame.sh /usr/local/bin/startgame ADD entrypoint.sh /usr/local/bin/entrypoint # Expose the volume @@ -89,5 +92,4 @@ ENV XDG_DATA_HOME=/config ENV PATH="$PATH:/app" # Finally, let's run OpenTTD! -USER openttd CMD [ "/usr/local/bin/entrypoint" ] diff --git a/entrypoint.sh b/entrypoint.sh index 388c655..4bf8ea0 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,70 +1,31 @@ #!/bin/sh -# This script is based fairly heavily off bateau84/openttd's. Thanks, man! +# This is the script that will run at the end +set -- /usr/local/bin/startgame "$@" -SAVEPATH="/config/save" -LOADGAME_CHECK="${loadgame}x" -EXTRA_FLAGS="-c /config/openttd.cfg" +# Change openttd user and group ids to what was specified +PUID=${PUID:-911} +PGID=${PGID:-1000} +groupmod -o -g "$PGID" openttd +usermod -o -u "$PUID" openttd -# Required to force config to save to /config -if [ -f /config/.config/openttd.cfg ]; then - export XDG_DATA_HOME='' - SAVEPATH="/config/.config/save" - EXTRA_FLAGS="-c /config/.config/openttd.cfg" - echo "WARN: Using legacy configuration directory /config/.config/ - it is recommended to migrate to all data inside /config/* when possible." -elif [ ! -f /config/openttd.cfg ]; then - # we start the server then kill it quickly to write a config file - # yes this is a horrific hack but whatever - echo "INFO: No config file found: generating one" - timeout 3 /app/openttd -D ${EXTRA_FLAGS} > /dev/null 2>&1 -fi +# Take ownership of /config and /app +chown -R openttd:openttd /config +chown -R openttd:openttd /app -if [ "${LOADGAME_CHECK}" != "x" ]; then - case ${loadgame} in - 'false') - echo "INFO: Creating a new game." - exec /app/openttd -D ${EXTRA_FLAGS} -x -d ${DEBUG} - exit 0 - ;; - 'last-autosave') - SAVEGAME_TARGET=`ls -rt ${SAVEPATH}/autosave/*.sav | tail -n1` +echo " + +User UID: $(id -u openttd) +User GID: $(id -g openttd) + +─────────────────────────────────────── + +" + +# Drop from root to the openttd user +set -- gosu openttd "$@" + +# Run original entrypoint +exec "$@" - if [ -r "${SAVEGAME_TARGET}" ]; then - echo "INFO: Loading from latest autosave - ${SAVEGAME_TARGET}" - exec /app/openttd -D ${EXTRA_FLAGS} -g "${SAVEGAME_TARGET}" -x -d ${DEBUG} - exit 0 - else - echo "FATAL: ${SAVEGAME_TARGET} not found" - exit 1 - fi - ;; - 'exit') - SAVEGAME_TARGET="${SAVEPATH}/autosave/exit.sav" - if [ -r "${SAVEGAME_TARGET}" ]; then - echo "INFO: Loading from exit save" - exec /app/openttd -D ${EXTRA_FLAGS} -g "${SAVEGAME_TARGET}" -x -d ${DEBUG} - exit 0 - else - echo "${SAVEGAME_TARGET} not found - Creating a new game." - exec /app/openttd -D ${EXTRA_FLAGS} -x -d ${DEBUG} - exit 0 - fi - ;; - *) - SAVEGAME_TARGET="${SAVEPATH}/${loadgame}" - if [ -r "${SAVEGAME_TARGET}" ]; then - echo "INFO: Loading ${SAVEGAME_TARGET}" - exec /app/openttd -D ${EXTRA_FLAGS} -g "${SAVEGAME_TARGET}" -x -d ${DEBUG} - exit 0 - else - echo "FATAL: ${SAVEGAME_TARGET} not found" - exit 1 - fi - ;; - esac -else - echo "INFO: loadgame not set - Creating a new game." - exec /app/openttd -D ${EXTRA_FLAGS} -x -d ${DEBUG} - exit 0 -fi diff --git a/startgame.sh b/startgame.sh new file mode 100755 index 0000000..388c655 --- /dev/null +++ b/startgame.sh @@ -0,0 +1,70 @@ +#!/bin/sh + +# This script is based fairly heavily off bateau84/openttd's. Thanks, man! + +SAVEPATH="/config/save" +LOADGAME_CHECK="${loadgame}x" +EXTRA_FLAGS="-c /config/openttd.cfg" + +# Required to force config to save to /config +if [ -f /config/.config/openttd.cfg ]; then + export XDG_DATA_HOME='' + SAVEPATH="/config/.config/save" + EXTRA_FLAGS="-c /config/.config/openttd.cfg" + echo "WARN: Using legacy configuration directory /config/.config/ - it is recommended to migrate to all data inside /config/* when possible." +elif [ ! -f /config/openttd.cfg ]; then + # we start the server then kill it quickly to write a config file + # yes this is a horrific hack but whatever + echo "INFO: No config file found: generating one" + timeout 3 /app/openttd -D ${EXTRA_FLAGS} > /dev/null 2>&1 +fi + +if [ "${LOADGAME_CHECK}" != "x" ]; then + case ${loadgame} in + 'false') + echo "INFO: Creating a new game." + exec /app/openttd -D ${EXTRA_FLAGS} -x -d ${DEBUG} + exit 0 + ;; + 'last-autosave') + SAVEGAME_TARGET=`ls -rt ${SAVEPATH}/autosave/*.sav | tail -n1` + + if [ -r "${SAVEGAME_TARGET}" ]; then + echo "INFO: Loading from latest autosave - ${SAVEGAME_TARGET}" + exec /app/openttd -D ${EXTRA_FLAGS} -g "${SAVEGAME_TARGET}" -x -d ${DEBUG} + exit 0 + else + echo "FATAL: ${SAVEGAME_TARGET} not found" + exit 1 + fi + ;; + 'exit') + SAVEGAME_TARGET="${SAVEPATH}/autosave/exit.sav" + + if [ -r "${SAVEGAME_TARGET}" ]; then + echo "INFO: Loading from exit save" + exec /app/openttd -D ${EXTRA_FLAGS} -g "${SAVEGAME_TARGET}" -x -d ${DEBUG} + exit 0 + else + echo "${SAVEGAME_TARGET} not found - Creating a new game." + exec /app/openttd -D ${EXTRA_FLAGS} -x -d ${DEBUG} + exit 0 + fi + ;; + *) + SAVEGAME_TARGET="${SAVEPATH}/${loadgame}" + if [ -r "${SAVEGAME_TARGET}" ]; then + echo "INFO: Loading ${SAVEGAME_TARGET}" + exec /app/openttd -D ${EXTRA_FLAGS} -g "${SAVEGAME_TARGET}" -x -d ${DEBUG} + exit 0 + else + echo "FATAL: ${SAVEGAME_TARGET} not found" + exit 1 + fi + ;; + esac +else + echo "INFO: loadgame not set - Creating a new game." + exec /app/openttd -D ${EXTRA_FLAGS} -x -d ${DEBUG} + exit 0 +fi