Files
DocprintService/docker/Dockerfile
T
2026-09-02 12:03:02 +08:00

48 lines
2.9 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 由 projman 注入的 .NET 运行镜像(多阶段:sdk 容器内 restore/publish -> aspnet 运行)
# 构建上下文 = 仓库根:docker build -f docker/Dockerfile .
# 通用约定:构建目标自动定位——根目录 .sln/.csproj 优先;否则全仓找第一个 .csproj
# (排除 bin/obj 与 *Test* 项目)。多可执行项目/特殊布局请自备本文件——已写则 projman 尊重不覆盖。
# 注意:.slnx(VS 新版 XML 解决方案格式)需 .NET SDK 9+ 才能解析,sdk:5.0 下不参与定位、
# 由子目录 .csproj 兜底(publish 单项目会自动连带其依赖项目)。
# 入口程序集按产物 *.deps.json 推断(X.deps.json <-> X.dll),无须硬编码项目名。
# DOTNET_VERSION 默认 5.0(平台现有 .NET 应用均为 5.0);版本不合时可在仓库自带文件里
# 用 --build-arg 覆盖(注入模板不支持外部传参,版本不合请自备)。
ARG DOTNET_VERSION=5.0
# ---- 构建阶段 ----
FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} AS build
WORKDIR /src
# 通用模板无法预知项目结构,整仓拷入一次构建(自备 Dockerfile 可按 csproj 分层命中缓存)
COPY . .
# 定位构建目标:根 sln > 根 csproj > 子目录第一个非 Test csproj;找不到则显式报错退出
RUN set -e; \
target=$(ls *.sln 2>/dev/null | head -1); \
[ -z "$target" ] && target=$(ls *.csproj 2>/dev/null | head -1); \
[ -z "$target" ] && target=$(find . -mindepth 2 -name '*.csproj' \
-not -path '*/bin/*' -not -path '*/obj/*' | grep -vi test | head -1); \
if [ -z "$target" ]; then echo 'ERROR: 未找到 .sln/.csproj(多可执行项目请自备 Dockerfile)'; exit 1; fi; \
echo "publish target: $target"; \
dotnet publish "$target" -c Release -o /app /p:UseAppHost=false
# 推断入口:产物中 X.deps.json 对应可执行 X.dll(取第一个;多可执行项目需自备 Dockerfile)
RUN set -e; entry=$(ls /app/*.deps.json | head -1 | sed 's/\.deps\.json$//'); \
echo "exec dotnet ${entry}.dll" > /app/entrypoint.sh
# ---- 运行阶段 ----
FROM mcr.microsoft.com/dotnet/aspnet:${DOTNET_VERSION} AS runtime
# .NET 5 基础镜像基于 Debian 10(已 EOL),apt 源须切 archive.debian.org 才能装包
# curl 供 projman compose healthcheck(test: CMD curl -f ...)使用;tzdata 设上海时区
RUN sed -i 's|deb.debian.org|archive.debian.org|g; s|security.debian.org|archive.debian.org|g' /etc/apt/sources.list \
&& echo 'Acquire::Check-Valid-Until "false";' > /etc/apt/apt.conf.d/99no-valid-until \
&& apt-get update \
&& apt-get install -y --no-install-recommends curl tzdata \
&& ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=build /app/ .
ENV TZ=Asia/Shanghai
ENV ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_URLS=http://+:10003
EXPOSE 10003
ENTRYPOINT ["sh", "/app/entrypoint.sh"]