todo
This commit is contained in:
62
Dockerfile
62
Dockerfile
@@ -1,58 +1,54 @@
|
||||
# 多段构建 - 第一阶段:构建应用
|
||||
FROM node:16-alpine AS builder
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 复制package文件
|
||||
COPY package*.json ./
|
||||
# 可选:使用哪种包管理器构建(默认 pnpm;也可传 npm)
|
||||
ARG PKG_MANAGER=pnpm
|
||||
# 可选:镜像加速(例如 https://registry.npmmirror.com)
|
||||
ARG NPM_REGISTRY=
|
||||
|
||||
# 设置npmmirror镜像
|
||||
RUN npm config set registry https://registry.npmmirror.com
|
||||
ENV CI=true
|
||||
|
||||
# 安装所有依赖(包括开发依赖,构建需要)
|
||||
RUN npm ci
|
||||
# 先复制依赖清单,最大化缓存命中
|
||||
COPY package.json ./
|
||||
COPY package-lock.json* pnpm-lock.yaml* yarn.lock* ./
|
||||
|
||||
RUN if [ -n "$NPM_REGISTRY" ]; then npm config set registry "$NPM_REGISTRY"; fi
|
||||
|
||||
RUN set -eux; \
|
||||
if [ "$PKG_MANAGER" = "pnpm" ]; then \
|
||||
npm i -g pnpm@8 && pnpm i --frozen-lockfile; \
|
||||
elif [ "$PKG_MANAGER" = "npm" ]; then \
|
||||
npm ci --no-audit --fund=false; \
|
||||
else \
|
||||
echo "Unsupported PKG_MANAGER=$PKG_MANAGER (use pnpm or npm)"; \
|
||||
exit 1; \
|
||||
fi
|
||||
|
||||
# 复制源代码
|
||||
COPY . .
|
||||
|
||||
# 构建应用
|
||||
RUN npm run build:prod
|
||||
RUN set -eux; \
|
||||
if [ "$PKG_MANAGER" = "pnpm" ]; then \
|
||||
pnpm run build:prod; \
|
||||
else \
|
||||
npm run build:prod; \
|
||||
fi
|
||||
|
||||
# 多段构建 - 第二阶段: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
|
||||
|
||||
# 健康检查
|
||||
# 健康检查:使用 nginx.conf 的 /health,避免额外安装 curl
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD curl -f http://localhost/ || exit 1
|
||||
CMD wget -qO- http://127.0.0.1/health >/dev/null || exit 1
|
||||
|
||||
# 启动nginx
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
|
||||
@@ -10,14 +10,8 @@ services:
|
||||
networks:
|
||||
- ${NETWORK_NAME:-proxy}
|
||||
restart: ${RESTART_POLICY:-unless-stopped}
|
||||
environment:
|
||||
- NGINX_HOST=${NGINX_HOST:-localhost}
|
||||
- NGINX_PORT=80
|
||||
volumes:
|
||||
- ./nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
# - ./ssl:/etc/nginx/ssl:ro # 如果需要SSL证书,取消注释并配置证书文件
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost/"]
|
||||
test: ["CMD", "wget", "-qO-", "http://127.0.0.1/health"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
@@ -35,4 +29,4 @@ services:
|
||||
networks:
|
||||
proxy:
|
||||
external: ${EXTERNAL_NETWORK:-true}
|
||||
name: ${NETWORK_NAME:-proxy}
|
||||
name: ${NETWORK_NAME:-proxy}
|
||||
|
||||
Reference in New Issue
Block a user