87 lines
2.3 KiB
Docker
87 lines
2.3 KiB
Docker
# 多阶段构建 Dockerfile
|
||
# 第一阶段:构建前端
|
||
FROM node:18-alpine AS frontend-builder
|
||
|
||
WORKDIR /app/frontend
|
||
|
||
COPY app/package.json app/pnpm-lock.yaml* ./
|
||
|
||
RUN npm config set registry https://registry.npmmirror.com && \
|
||
npm install -g pnpm && \
|
||
pnpm config set registry https://registry.npmmirror.com && \
|
||
if [ -f pnpm-lock.yaml ]; then \
|
||
pnpm install --frozen-lockfile; \
|
||
else \
|
||
npm install; \
|
||
fi
|
||
|
||
COPY app/ ./
|
||
RUN pnpm build || npm run build
|
||
|
||
# 第二阶段:构建后端 + 下载 Playwright Chromium
|
||
FROM mcr.azurecr.cn/dotnet/sdk:6.0 AS backend-builder
|
||
|
||
WORKDIR /src
|
||
|
||
# 安装 Node.js 18(用于 npx playwright install,最可靠的方式)
|
||
RUN apt-get update && apt-get install -y curl && \
|
||
curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
|
||
apt-get install -y nodejs && \
|
||
rm -rf /var/lib/apt/lists/*
|
||
|
||
COPY *.csproj ./
|
||
RUN dotnet restore dy.net.csproj
|
||
|
||
COPY . .
|
||
RUN find . -name "._*" -type f -delete
|
||
|
||
COPY --from=frontend-builder /app/frontend/dist ./app/dist
|
||
|
||
RUN dotnet publish dy.net.csproj -c Release -o /app/publish
|
||
|
||
# 用 npx 下载 Playwright Chromium 到固定路径
|
||
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
|
||
RUN npx -y playwright@1.44.0 install chromium
|
||
|
||
# 第三阶段:运行镜像
|
||
FROM mcr.azurecr.cn/dotnet/aspnet:6.0 AS final
|
||
|
||
WORKDIR /app
|
||
|
||
ENV TZ=Asia/Shanghai
|
||
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
||
|
||
# 切换阿里云镜像源,解决国内网络无法访问 deb.debian.org 的问题
|
||
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \
|
||
sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list
|
||
|
||
# 安装 Chromium 运行时依赖(Debian Bullseye)
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||
libnss3 \
|
||
libatk1.0-0 \
|
||
libatk-bridge2.0-0 \
|
||
libcups2 \
|
||
libdrm2 \
|
||
libxkbcommon0 \
|
||
libxcomposite1 \
|
||
libxdamage1 \
|
||
libxfixes3 \
|
||
libxrandr2 \
|
||
libgbm1 \
|
||
libasound2 \
|
||
libpango-1.0-0 \
|
||
libpangocairo-1.0-0 \
|
||
libgtk-3-0 \
|
||
fonts-liberation \
|
||
&& rm -rf /var/lib/apt/lists/*
|
||
|
||
COPY --from=backend-builder /app/publish .
|
||
COPY --from=backend-builder /ms-playwright /ms-playwright
|
||
|
||
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
|
||
EXPOSE 10101
|
||
ENV ASPNETCORE_URLS=http://*:10101
|
||
ENV ASPNETCORE_ENVIRONMENT=Production
|
||
|
||
ENTRYPOINT ["dotnet", "dy.net.dll"]
|