From 3d2d4a4533e3d9cf5598ec3a39ccf4c1d57f7285 Mon Sep 17 00:00:00 2001 From: yhl452493373 Date: Sat, 22 Nov 2025 02:36:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AF=E7=94=B1debian?= =?UTF-8?q?=E6=94=B9=E4=B8=BAalpine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 66 ++++++++++++++++++++++++++++++++------------------- entrypoint.sh | 48 ++++++++++++++----------------------- 2 files changed, 59 insertions(+), 55 deletions(-) diff --git a/Dockerfile b/Dockerfile index f42d334..245cd4c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,34 +1,50 @@ -FROM mcr.microsoft.com/dotnet/sdk:9.0 AS server-builder +# ================================ +# BUILD STAGE +# ================================ +#FROM mcr.microsoft.com/dotnet/sdk:9.0-alpine AS build +FROM mcr.azure.cn/dotnet/sdk:9.0-alpine AS build + ARG SPT_VERSION ARG SPT_BUILD_TYPE ARG SPT_BUILD_CONFIG -RUN apt update && apt install -y git git-lfs wget && \ - git clone -b develop --depth=1 https://github.com/sp-tarkov/server-csharp.git /snapshot && \ - cd /snapshot && git lfs pull && \ - cd /snapshot && \ - dotnet publish ./SPTarkov.Server/SPTarkov.Server.csproj \ - -c $SPT_BUILD_CONFIG \ - -f net9.0 \ - -r linux-x64 \ - -p:IncludeNativeLibrariesForSelfExtract=true \ - -p:PublishSingleFile=true \ - --self-contained false \ - -p:SptBuildType=$SPT_BUILD_TYPE \ - -p:SptVersion=$SPT_VERSION \ - -p:SptBuildTime=$( date +%Y%m%d ) \ - -p:SptCommit=$(git rev-parse --short HEAD) \ - -p:IsPublish=true && \ - rm SPTarkov.Server/bin/$SPT_BUILD_CONFIG/net9.0/linux-x64/publish/*.pdb && \ - rm -rf /var/lib/apt/lists/* -FROM mcr.microsoft.com/dotnet/aspnet:9.0-bookworm-slim +RUN apk add --no-cache git git-lfs wget && \ + git clone -b develop --depth=1 https://github.com/sp-tarkov/server-csharp.git /src && \ + cd /src && git lfs pull + +WORKDIR /src + +RUN dotnet publish ./SPTarkov.Server/SPTarkov.Server.csproj \ + -c $SPT_BUILD_CONFIG \ + -f net9.0 \ + -r linux-musl-x64 \ + -p:PublishSingleFile=true \ + -p:IncludeNativeLibrariesForSelfExtract=true \ + --self-contained false \ + -p:SptBuildType=$SPT_BUILD_TYPE \ + -p:SptVersion=$SPT_VERSION \ + -p:SptBuildTime=$(date +%Y%m%d) \ + -p:SptCommit=$(git rev-parse --short HEAD) \ + -p:IsPublish=true && \ + ls -l /src/SPTarkov.Server/bin/$SPT_BUILD_CONFIG/net9.0/linux-musl-x64/publish + +# ================================ +# RUNTIME STAGE +# ================================ +#FROM mcr.microsoft.com/dotnet/aspnet:9.0-alpine AS final +FROM mcr.azure.cn/dotnet/aspnet:9.0-alpine AS final + ARG SPT_BUILD_CONFIG -LABEL author="yhl452493373 " -ENV TZ=Asia/Shanghai -COPY --from=server-builder /snapshot/SPTarkov.Server/bin/$SPT_BUILD_CONFIG/net9.0/linux-x64/publish/ /app/spt-server/ -VOLUME /opt/spt-server + +RUN apk add --no-cache bash coreutils sed gawk iproute2 ca-certificates + WORKDIR /opt/spt-server -EXPOSE 6969 + +COPY --from=build /src/SPTarkov.Server/bin/$SPT_BUILD_CONFIG/net9.0/linux-musl-x64/publish/ /app/spt-server/ + COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh + +EXPOSE 6969 + ENTRYPOINT ["/entrypoint.sh"] diff --git a/entrypoint.sh b/entrypoint.sh index 96e4dd3..e771f40 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,5 +1,6 @@ -#!/bin/bash +#!/bin/sh +# 文件对比 if [ -f /opt/spt-server/SPTarkov.Server ]; then appHash=$(md5sum /app/spt-server/SPTarkov.Server | awk '{ print $1 }') exeHash=$(md5sum /opt/spt-server/SPTarkov.Server | awk '{ print $1 }') @@ -16,42 +17,29 @@ else echo "Finished!" fi -cd /opt/spt-server +cd /opt/spt-server || exit -if [ -z "$ip" ]; then - IP="0.0.0.0" -else - IP=$ip -fi +# IP 配置 +IP="${ip:-0.0.0.0}" -if [ -z "$backendIp" ]; then - BACKEND_IP=$(hostname -I | awk '{print $1}') -else - BACKEND_IP=$backendIp -fi +# 自动获取IP,端口等配置 +BACKEND_IP="${backendIp:-$(ip route get 1 | awk '{print $7}')}" +PORT="${backendPort:-6969}" +PINGDELAYMS="${webSocketPingDelayMs:-90000}" -if [ -z "$backendPort" ]; then - PORT=6969 -else - PORT=$backendPort -fi - -if [ -z "$webSocketPingDelayMs" ]; then - PINGDELAYMS=90000 -else - PINGDELAYMS=$webSocketPingDelayMs -fi - -sed -Ei "s/\"ip\": \"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\",/\"ip\": \"${IP}\",/g" SPT_Data/configs/http.json -sed -Ei "s/\"port\": ([0-9]|[1-9][1-9]{1,3}|[1-5][0-9]{4}|6[1-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]),/\"port\": ${PORT},/g" SPT_Data/configs/http.json -sed -Ei "s/\"backendIp\": \"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\",/\"backendIp\": \"${BACKEND_IP}\",/g" SPT_Data/configs/http.json -sed -Ei "s/\"backendPort\": ([0-9]|[1-9][1-9]{1,3}|[1-5][0-9]{4}|6[1-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]),/\"backendPort\": ${PORT},/g" SPT_Data/configs/http.json -sed -Ei "s/\"webSocketPingDelayMs\": ([0-9]{1,}),/\"webSocketPingDelayMs\": ${PINGDELAYMS},/g" SPT_Data/configs/http.json +# 配置文件替换 +sed -Ei "s/\"ip\": \".*?\",/\"ip\": \"${IP}\",/g" SPT_Data/configs/http.json +sed -Ei "s/\"port\": [0-9]+,/\"port\": ${PORT},/g" SPT_Data/configs/http.json +sed -Ei "s/\"backendIp\": \".*?\",/\"backendIp\": \"${BACKEND_IP}\",/g" SPT_Data/configs/http.json +sed -Ei "s/\"backendPort\": [0-9]+,/\"backendPort\": ${PORT},/g" SPT_Data/configs/http.json +sed -Ei "s/\"webSocketPingDelayMs\": [0-9]+,/\"webSocketPingDelayMs\": ${PINGDELAYMS},/g" SPT_Data/configs/http.json +# 日志文件创建 if [ ! -f "sptLogger.json" ]; then if [ -f "sptLogger.Development.json" ]; then cp sptLogger.Development.json sptLogger.json fi fi -chmod +x SPT.Server.Linux && ./SPT.Server.Linux +chmod +x SPT.Server +exec ./SPT.Server