quanyoumi/Dockerfile

58 lines
1.3 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:18-alpine AS frontend-builder
WORKDIR /app/frontend
# 复制前端依赖文件
COPY frontend/package*.json ./
# 安装前端依赖(包括 devDependencies因为需要 vite 构建)
RUN npm ci
# 复制前端源码
COPY frontend/ ./
# 构建前端(使用默认的 .env 文件中的配置)
RUN npm run build
# 后端运行阶段
FROM node:18-alpine
WORKDIR /app
# 安装 dumb-init 和 envsubst
RUN apk add --no-cache dumb-init gettext
# 复制后端依赖文件
COPY backend/package*.json ./
# 安装后端依赖(仅生产依赖)
RUN npm ci --only=production
# 复制后端源码
COPY backend/ ./
# 从前端构建阶段复制构建产物
COPY --from=frontend-builder /app/frontend/dist ./public
# 创建上传目录
RUN mkdir -p uploads
# 复制启动脚本
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
# 暴露端口
EXPOSE 3000
# 设置环境变量
ENV NODE_ENV=production
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/api/activities', (r) => process.exit(r.statusCode === 200 ? 0 : 1))"
# 使用启动脚本
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["node", "server.js"]