feat: 添加 Docker 化部署配置与 CI/CD 自动化
- 新增后端 Dockerfile(Node.js 20 Alpine 基础镜像) - 新增前端 Dockerfile(多阶段构建:Vite build + Nginx) - 新增 Nginx 配置(serve 前端静态文件 + 反代 /api) - 新增 docker-compose.dev.yml 和 docker-compose.prod.yml - 新增 GitHub Actions 工作流(自动构建推送阿里云镜像) - 新增 .dockerignore 排除不需要的文件 - 新增部署方案文档
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
# ============================================
|
||||
# 前端 Docker 构建忽略文件
|
||||
# ============================================
|
||||
|
||||
# 依赖
|
||||
node_modules
|
||||
|
||||
# 构建产物(由构建阶段生成)
|
||||
dist
|
||||
|
||||
# 本地环境
|
||||
.env
|
||||
.frontend-port
|
||||
|
||||
# 版本控制
|
||||
.git
|
||||
.gitignore
|
||||
|
||||
# IDE
|
||||
.idea
|
||||
.vscode
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# 系统
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# npm
|
||||
npm-debug.log*
|
||||
@@ -0,0 +1,41 @@
|
||||
# ============================================
|
||||
# IDC 设备管理系统 - 前端(Nginx)Dockerfile
|
||||
# 第一阶段:构建前端静态文件
|
||||
# 第二阶段:Nginx 运行
|
||||
# ============================================
|
||||
|
||||
# ---------- 第一阶段:构建 ----------
|
||||
FROM node:20-alpine AS build
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# 复制依赖配置
|
||||
COPY package.json package-lock.json* yarn.lock* ./
|
||||
|
||||
# 安装所有依赖(含 devDependencies,vite build 需要)
|
||||
RUN npm install --ignore-scripts
|
||||
|
||||
# 复制前端源码
|
||||
COPY . .
|
||||
|
||||
# 构建生产版本
|
||||
RUN npm run build
|
||||
|
||||
# ---------- 第二阶段:Nginx 运行 ----------
|
||||
FROM nginx:alpine
|
||||
|
||||
# 设置时区
|
||||
RUN apk add --no-cache tzdata
|
||||
ENV TZ=Asia/Shanghai
|
||||
|
||||
# 复制 Nginx 配置
|
||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||
|
||||
# 复制前端构建产物到 Nginx 静态目录
|
||||
COPY --from=build /app/dist /usr/share/nginx/html
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 80
|
||||
|
||||
# Nginx 前台运行
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
@@ -0,0 +1,77 @@
|
||||
# ============================================
|
||||
# IDC 设备管理系统 - Nginx 配置
|
||||
# 功能:serve 前端静态文件 + 反向代理 API
|
||||
# ============================================
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
server_name localhost;
|
||||
|
||||
# 前端静态文件
|
||||
root /usr/share/nginx/html;
|
||||
index index.html;
|
||||
|
||||
# Gzip 压缩
|
||||
gzip on;
|
||||
gzip_types text/plain text/css application/json application/javascript text/xml application/xml text/javascript image/svg+xml;
|
||||
gzip_min_length 1024;
|
||||
gzip_vary on;
|
||||
|
||||
# 前端路由(SPA 支持)
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
# API 反向代理到后端
|
||||
location /api/ {
|
||||
proxy_pass http://backend:8000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_cache_bypass $http_upgrade;
|
||||
|
||||
# 超时设置
|
||||
proxy_read_timeout 120s;
|
||||
proxy_send_timeout 120s;
|
||||
}
|
||||
|
||||
# 上传文件访问
|
||||
location /uploads/ {
|
||||
proxy_pass http://backend:8000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
}
|
||||
|
||||
# 健康检查端点
|
||||
location /health {
|
||||
proxy_pass http://backend:8000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
}
|
||||
|
||||
# API 文档
|
||||
location /api-docs {
|
||||
proxy_pass http://backend:8000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
}
|
||||
|
||||
# 禁止访问隐藏文件
|
||||
location ~ /\. {
|
||||
deny all;
|
||||
access_log off;
|
||||
log_not_found off;
|
||||
}
|
||||
|
||||
# 静态资源缓存策略
|
||||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2?|ttf|eot)$ {
|
||||
expires 30d;
|
||||
add_header Cache-Control "public, immutable";
|
||||
access_log off;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user