60 lines
1.3 KiB
Docker
60 lines
1.3 KiB
Docker
# 多阶段构建 - 构建阶段
|
|
FROM node:20-alpine AS builder
|
|
|
|
WORKDIR /app
|
|
|
|
# 复制包管理文件
|
|
COPY package.json pnpm-lock.yaml ./
|
|
|
|
# 安装 pnpm 和依赖
|
|
RUN npm install -g pnpm && pnpm install
|
|
|
|
# 复制源代码
|
|
COPY . .
|
|
|
|
# 构建生产版本
|
|
RUN pnpm build
|
|
|
|
# 生产阶段 - nginx
|
|
FROM nginx:alpine AS production
|
|
|
|
# 复制自定义 nginx 配置
|
|
COPY <<EOF /etc/nginx/conf.d/default.conf
|
|
server {
|
|
listen 5173;
|
|
server_name localhost;
|
|
root /usr/share/nginx/html;
|
|
index index.html;
|
|
|
|
# 处理 SPA 路由
|
|
location / {
|
|
try_files \$uri \$uri/ /index.html;
|
|
}
|
|
|
|
# 静态资源缓存
|
|
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
|
|
expires 1y;
|
|
add_header Cache-Control "public, immutable";
|
|
}
|
|
|
|
# 安全头
|
|
add_header X-Frame-Options "SAMEORIGIN" always;
|
|
add_header X-Content-Type-Options "nosniff" always;
|
|
add_header X-XSS-Protection "1; mode=block" always;
|
|
|
|
# Gzip 压缩
|
|
gzip on;
|
|
gzip_vary on;
|
|
gzip_min_length 1024;
|
|
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
|
|
}
|
|
EOF
|
|
|
|
# 从构建阶段复制构建产物
|
|
COPY --from=builder /app/dist /usr/share/nginx/html
|
|
|
|
# 暴露端口
|
|
EXPOSE 5173
|
|
|
|
# 启动 nginx
|
|
CMD ["nginx", "-g", "daemon off;"] |