From 5a1af7b1a5b3603a7271983f409edf9e6b9e453f Mon Sep 17 00:00:00 2001 From: Matthew Green Date: Tue, 8 Apr 2025 17:57:26 -0500 Subject: [PATCH] Reworked to allow mounted config volume. --- .gitignore | 1 + Dockerfile | 53 +++++++++++++++++---------------------------------- Makefile | 3 +++ entrypoint.sh | 37 +++++++++++++++++++++++++++++++++++ start.sh | 9 +++++---- 5 files changed, 64 insertions(+), 39 deletions(-) create mode 100644 .gitignore create mode 100644 entrypoint.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f733c4b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config/ diff --git a/Dockerfile b/Dockerfile index 2c0b87d..2080be9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,46 +1,29 @@ FROM steamcmd/steamcmd:debian ENV PORT=7777 +ENV INSTALL_DIR=/home/steam +ENV SERVER_DIR=${INSTALL_DIR}/server +ENV CONFIG_DIR=${INSTALL_DIR}/.config +ENV CONFIG_TEMPLATES_DIR=${INSTALL_DIR}/config-templates +# Default UID/GID that can be overridden at runtime ENV UID=1000 ENV GID=1000 -# ENV HOME_DIR=/home/steam -# ENV SERVER=$HOME_DIR/server -# ENV CONFIGS=$HOME_DIR/.config/SCP\ Secret\ Laboratory/config/$PORT -ENV STEAM_DIR=/usr/lib/games/steam -ENV INSTALL_DIR=/home/steam -ENV SERVER_DIR=$INSTALL_DIR/server -ENV CONFIG_DIR=$INSTALL_DIR/.config -ENV CONFIG_TEMPLATES_DIR=$INSTALL_DIR/.config-templates - -RUN mkdir -p ${INSTALL_DIR} ${SERVER_DIR} ${CONFIG_DIR} ${CONFIG_TEMPLATES_DIR} +# Install dependencies RUN apt-get update && \ - apt-get install -y libicu-dev + apt-get install -y libicu-dev gosu && \ + rm -rf /var/lib/apt/lists/* -# COPY config_gameplay.txt $CONFIGS/config_gameplay.txt -# COPY config_remoteadmin.txt $CONFIGS/config_remoteadmin.txt -# COPY config_localadmin_global.txt $CONFIGS/../config_localadmin_global.txt -# COPY localadmin_internal_data.json $CONFIGS/../localadmin_internal_data.json +# Create directories (but don't create the user yet) +RUN mkdir -p ${CONFIG_TEMPLATES_DIR} +COPY config-templates/ ${CONFIG_TEMPLATES_DIR}/ -# Create steam user and group -RUN groupadd --gid $GID steam && \ - useradd --create-home -c 'Steam User' -l --uid $UID --gid $GID --home-dir $INSTALL_DIR steam && \ - chown -R steam:steam ${INSTALL_DIR} ${SERVER_DIR} ${CONFIG_DIR} ${CONFIG_TEMPLATES_DIR} && \ - chmod 777 ${INSTALL_DIR} ${SERVER_DIR} ${CONFIG_DIR} ${CONFIG_TEMPLATES_DIR} +# Copy and prepare scripts +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh -# Copy and prepare start script -COPY start.sh $INSTALL_DIR/start.sh -RUN chmod +x ${INSTALL_DIR}/start.sh && \ - chown -R steam:steam ${INSTALL_DIR}/start.sh +COPY start.sh /start.sh +RUN chmod +x /start.sh -# Copy configuration template files -COPY config-templates/ ${CONFIG_TEMPLATES_DIR} - -# Switch to steam user -USER steam -WORKDIR $INSTALL_DIR - -# Set HOME environment variable to INSTALL_DIR to force steamcmd to use it -ENV HOME=$INSTALL_DIR - -ENTRYPOINT ["/bin/sh", "start.sh"] \ No newline at end of file +# User will be created at runtime based on ENV values +ENTRYPOINT ["/bin/sh", "/entrypoint.sh"] \ No newline at end of file diff --git a/Makefile b/Makefile index 9dd1929..3dad97d 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,9 @@ run: build @echo "Running container: $(CONTAINER_NAME)" docker run -d --name $(CONTAINER_NAME) \ -p $(PORT):$(PORT)/udp \ + -v $(PWD)/config:/home/steam/config \ + -e UID=1001 \ + -e GID=1001 \ --restart unless-stopped \ $(IMAGE_NAME):latest diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..e159d16 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,37 @@ +#!/bin/sh +set -e + +echo "Setting up container with UID:${UID} and GID:${GID}" + +# Check if group with GID exists +if getent group ${GID} > /dev/null; then + EXISTING_GROUP=$(getent group ${GID} | cut -d: -f1) + if [ "${EXISTING_GROUP}" != "steam" ]; then + echo "ERROR: GID ${GID} already exists with group name '${EXISTING_GROUP}'" >&2 + exit 1 + fi +else + groupadd --gid ${GID} steam +fi + +# Check if user with UID exists +if getent passwd ${UID} > /dev/null; then + EXISTING_USER=$(getent passwd ${UID} | cut -d: -f1) + if [ "${EXISTING_USER}" != "steam" ]; then + echo "ERROR: UID ${UID} already exists with username '${EXISTING_USER}'" >&2 + exit 1 + fi +else + useradd -c 'Steam User' -l --uid ${UID} --gid ${GID} --home-dir ${INSTALL_DIR} steam +fi + +# Get username for the UID +USER_NAME=$(getent passwd ${UID} | cut -d: -f1) + +mkdir -p ${SERVER_DIR} ${CONFIG_DIR} +# Ensure correct ownership of all directories +chown -R ${UID}:${GID} ${INSTALL_DIR} ${SERVER_DIR} ${CONFIG_TEMPLATES_DIR} +chmod -R 775 ${CONFIG_DIR} + +# Now run the actual script as the specified user +exec gosu steam /start.sh \ No newline at end of file diff --git a/start.sh b/start.sh index a9449f0..7bb2bea 100644 --- a/start.sh +++ b/start.sh @@ -1,13 +1,14 @@ #!/bin/sh +set -e + echo "Current user is: $(whoami)" # Install/update SCP:SL server steamcmd +force_install_dir $SERVER_DIR +login anonymous +app_update 996560 validate +quit -# # Ensure config directory exists +# Ensure config directory exists INTERNAL_CONFIG_SUBDIR="$CONFIG_DIR/SCP Secret Laboratory/config/" mkdir -p "$INTERNAL_CONFIG_SUBDIR" -chmod 755 "$INTERNAL_CONFIG_SUBDIR" # Process the internal data template to accept EULA INTERNAL_DATA_TEMPLATE_FILE="$CONFIG_TEMPLATES_DIR/localadmin_internal_data.json.template" @@ -22,7 +23,7 @@ if [ ! -f "$INTERNAL_DATA_FILE" ]; then echo "Successfully created \`localadmin_internal_data.json\` file with EULA acceptance." fi -# Run server directly (no need to su) -cd $SERVER_DIR && HOME=$INSTALL_DIR ./LocalAdmin $PORT +# Run server +cd $SERVER_DIR && HOME=$INSTALL_DIR ./LocalAdmin $PORT --config $(CONFIG_DIR) sleep infinity \ No newline at end of file