Files
web/Dockerfile
2025-11-01 17:55:27 +08:00

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:16-alpine AS builder
# 设置工作目录
WORKDIR /app
# 复制package文件
COPY package*.json ./
# 设置npmmirror镜像
RUN npm config set registry https://registry.npmmirror.com
# 安装所有依赖(包括开发依赖,构建需要)
RUN npm ci
# 复制源代码
COPY . .
# 构建应用
RUN npm run build:prod
# 多段构建 - 第二阶段Nginx部署
FROM nginx:alpine AS final
# 安装必要的工具
RUN apk add --no-cache curl
# 复制自定义nginx配置
COPY nginx.conf /etc/nginx/nginx.conf
# 从构建阶段复制构建产物
COPY --from=builder /app/dist /usr/share/nginx/html
# 复制SSL证书如果有的话
# COPY ssl/ /etc/nginx/ssl/
# 设置权限
RUN chown -R nginx:nginx /usr/share/nginx/html && \
chown -R nginx:nginx /var/cache/nginx && \
chown -R nginx:nginx /var/log/nginx && \
chown -R nginx:nginx /etc/nginx/conf.d
# 创建nginx运行时需要的目录
RUN touch /var/run/nginx.pid && \
chown -R nginx:nginx /var/run/nginx.pid
# 切换到非root用户
USER nginx
# 暴露端口
EXPOSE 80
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost/ || exit 1
# 启动nginx
CMD ["nginx", "-g", "daemon off;"]