hts/apps/blogai/Dockerfile

72 lines
2.0 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.

# --- 第一阶段:构建阶段 ---
FROM node:20-bullseye-slim AS builder
# 构建时用 development 环境,确保 devDependencies 安装
ENV NODE_ENV=development
WORKDIR /app
# 预装 pnpm
RUN npm install -g pnpm
# 拷贝基础依赖文件
COPY pnpm-lock.yaml ./
COPY pnpm-workspace.yaml ./
COPY package.json ./
# 安装全量依赖(包含 tsup, typescript 等 devDependencies
RUN pnpm install
# 拷贝全部源码
COPY . .
# 设置 BLOGAI_HOST 环境变量,处理 env 模板
ARG BLOGAI_HOST=ai.szaiai.com
ENV BLOGAI_HOST=${BLOGAI_HOST}
COPY apps/blogai/.env.example apps/blogai/.env
RUN sed -i "s|{{BLOGAI_HOST}}|${BLOGAI_HOST}|g" apps/blogai/.env
# 进入子目录并执行构建
WORKDIR /app/apps/blogai
RUN pnpm run build
# --- 第二阶段:运行阶段 ---
FROM node:20-slim AS runner
# 最终环境:纯净 production
ENV NODE_ENV=production
ENV PORT=3008
ARG BLOGAI_HOST=ai.szaiai.com
ENV BLOGAI_HOST=${BLOGAI_HOST}
# 安装运行所需工具
RUN apt-get update && \
apt-get install -y curl supervisor && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# 工作目录
WORKDIR /plugai
# Supervisor 配置(可选)
COPY ./supervisor.conf /etc/supervisor/conf.d/supervisor.conf
# 拷贝运行时代码 + 构建产物
COPY --from=builder /app/apps/blogai/.next ./apps/blogai/.next
COPY --from=builder /app/apps/blogai/public ./apps/blogai/public
COPY --from=builder /app/apps/blogai/package.json ./apps/blogai/package.json
COPY --from=builder /app/apps/blogai/next.config.js ./apps/blogai/next.config.js
COPY --from=builder /app/apps/blogai/next-i18next.config.js ./apps/blogai/next-i18next.config.js
COPY --from=builder /app/node_modules ./node_modules
# 设置默认工作目录为子项目 blogai
WORKDIR /plugai/apps/blogai
# 添加 healthcheck
HEALTHCHECK --interval=30s --timeout=3s --start-period=25s --retries=3 \
CMD curl -fs http://localhost:3008/api/health/ || exit 1
# 可选:使用 supervisor 启动(也可以注释)
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisor.conf"]