# ===== 第一阶段:构建阶段 ===== FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build # 配置 NuGet 使用国内镜像源 RUN dotnet nuget add source https://mirrors.cloud.tencent.com/nuget/ \ && dotnet nuget disable source nuget.org # 配置 apt-get 使用 apt-cacher-ng 作为代理 RUN echo 'Acquire::http::Proxy "http://192.168.1.140:3142";' > /etc/apt/apt.conf.d/01proxy # 创建并配置 Debian 镜像源 RUN echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \ echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \ echo "deb https://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list # 允许使用不安全的软件源 RUN echo 'Acquire::AllowInsecureRepositories "true";' > /etc/apt/apt.conf.d/99allow-insecure && \ echo 'Acquire::AllowDowngradeToInsecureRepositories "true";' >> /etc/apt/apt.conf.d/99allow-insecure WORKDIR /src # 安装 Fortran 编译器 RUN apt-get update && apt-get install -y --no-install-recommends \ gfortran \ && rm -rf /var/lib/apt/lists/* # 复制 Fortran 源文件并编译 COPY Fortran/*.f90 /src/Fortran/ WORKDIR /src/Fortran # 编译所有 .f90 文件为 .o 文件 RUN for file in *.f90; do gfortran -fPIC -c "$file" -o "${file%.f90}.o"; done # 链接所有 .o 文件为共享库 RUN gfortran -shared *.o -o libSUB_UNITHYDRO.so # 回到主工作目录 WORKDIR /src # 只复制项目文件 COPY ["FortranWebApi.csproj", "./"] RUN dotnet restore # 复制源代码 COPY . . # 合并 build 和 publish 命令 RUN dotnet publish -c Release -o /app/publish # 复制编译好的 .so 文件到发布目录 RUN mkdir -p /app/publish && \ cp /src/Fortran/libSUB_UNITHYDRO.so /app/publish/ # ===== 第二阶段:运行阶段 ===== FROM mcr.microsoft.com/dotnet/aspnet:8.0 # 配置 apt-get 使用 apt-cacher-ng 作为代理 RUN echo 'Acquire::http::Proxy "http://192.168.1.140:3142";' > /etc/apt/apt.conf.d/01proxy # 创建并配置 Debian 镜像源 RUN echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \ echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \ echo "deb https://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list # 允许使用不安全的软件源 RUN echo 'Acquire::AllowInsecureRepositories "true";' > /etc/apt/apt.conf.d/99allow-insecure && \ echo 'Acquire::AllowDowngradeToInsecureRepositories "true";' >> /etc/apt/apt.conf.d/99allow-insecure # 安装运行时依赖 RUN apt-get update && apt-get install -y --no-install-recommends \ libc6-dev \ libgfortran5 \ && rm -rf /var/lib/apt/lists/* WORKDIR /app COPY --from=build /app/publish . # 确保 .so 文件的权限正确 RUN chmod 755 /app/libSUB_UNITHYDRO.so # 设置 LD_LIBRARY_PATH ENV LD_LIBRARY_PATH=/app # 设置端口和监听地址 ENV ASPNETCORE_URLS=http://+:5000 EXPOSE 5000 ENTRYPOINT ["dotnet", "FortranWebApi.dll"]