diff --git a/.env.example b/.env.example index 4a22083..4d040a2 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,7 @@ # 通用端口配置 BACKEND_PORT=8001 FRONTEND_PORT=3015 -BACKEND_HOST=0.0.0.0 +BACKEND_HOST=0.0.0.0 # 默认为 0.0.0.0,表示监听所有 IP 地址 不建议动 # 前端访问后端用(生产环境建议写公网或宿主机 IP) VITE_API_BASE_URL=http://127.0.0.1:8001 diff --git a/BillNote_frontend/Dockerfile b/BillNote_frontend/Dockerfile index 44deee5..27856a6 100644 --- a/BillNote_frontend/Dockerfile +++ b/BillNote_frontend/Dockerfile @@ -1,13 +1,32 @@ +# === 前端构建阶段 === FROM node:18-alpine AS build +# 安装 pnpm +RUN npm install -g pnpm + +# 设置工作目录 WORKDIR /app +# 拷贝前端源码 COPY ./BillNote_frontend /app -RUN npm install && npm run build +# 安装依赖并构建 +RUN pnpm install && pnpm run build +# === nginx 运行阶段 === FROM nginx:alpine +# 拷贝模板配置 +COPY ./BillNote_frontend/deploy/default.conf.template /etc/nginx/templates/default.conf.template + +# 拷贝构建产物 COPY --from=build /app/dist /usr/share/nginx/html +# 拷贝启动脚本 +COPY ./BillNote_frontend/deploy/start.sh /start.sh +RUN chmod +x /start.sh + EXPOSE 80 + +# 使用启动脚本启动容器 +CMD ["/start.sh"] \ No newline at end of file diff --git a/BillNote_frontend/deploy/default.conf.template b/BillNote_frontend/deploy/default.conf.template new file mode 100644 index 0000000..a6bcf4d --- /dev/null +++ b/BillNote_frontend/deploy/default.conf.template @@ -0,0 +1,18 @@ +server { + listen 80; + resolver 127.0.0.11 valid=10s; + + location / { + root /usr/share/nginx/html; + index index.html; + try_files $uri $uri/ /index.html; + } + + location /api/ { + proxy_pass http://backend:${BACKEND_PORT}; + 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; +} +} \ No newline at end of file diff --git a/BillNote_frontend/deploy/start.sh b/BillNote_frontend/deploy/start.sh new file mode 100644 index 0000000..af0819a --- /dev/null +++ b/BillNote_frontend/deploy/start.sh @@ -0,0 +1,20 @@ +#!/bin/sh +### + # @Author: Jefferyhcool 1063474837@qq.com + # @Date: 2025-04-16 01:57:05 + # @LastEditors: Jefferyhcool 1063474837@qq.com + # @LastEditTime: 2025-04-16 01:59:37 + # @FilePath: /hotfix-dev/BillNote_frontend/deploy/start.sh + # @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE +### +# 等待后端健康检查通过 +until curl -s "http://backend:${BACKEND_PORT}/health" > /dev/null; do + echo "等待后端服务就绪..." + sleep 2 +done + +# 生成 nginx 配置文件(动态变量替换) +envsubst '${BACKEND_HOST} ${BACKEND_PORT}' < /etc/nginx/templates/default.conf.template > /etc/nginx/conf.d/default.conf + +# 启动 Nginx(在前台运行) +exec nginx -g 'daemon off;' \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 8807b56..e037947 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,3 +1,4 @@ +version: "3.9" services: backend: @@ -14,6 +15,7 @@ services: - ./backend:/app ports: - "${BACKEND_PORT}:${BACKEND_PORT}" + depends_on: - frontend @@ -26,3 +28,5 @@ services: - .env ports: - "${FRONTEND_PORT}:80" + +