From d263ddbbcc1fdf7ab0fdb0af40b1d529ee6313da Mon Sep 17 00:00:00 2001 From: Cassianvale Date: Fri, 7 Mar 2025 14:59:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20docker=E9=95=9C=E5=83=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 17 +++++++++++++++++ docker-compose.yml | 3 +++ frontend/Dockerfile | 12 +++++++++--- frontend/nginx.conf | 12 ++++++------ 4 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..3e93267 --- /dev/null +++ b/.env.example @@ -0,0 +1,17 @@ +# API配置 +API_KEY= +API_URL= +API_MODEL= +API_TIMEOUT=60 + +# 登录配置 +LOGIN_PASSWORD= + +# 公告文本 +ANNOUNCEMENT_TEXT=欢迎使用! + +# 后端服务地址(公网访问时需要设置为公网IP或域名) +# 本地开发时可以使用:http://backend:8888 +# 公网访问时使用:http://your_public_ip:8888 +BACKEND_URL=http://your_public_ip:8888 + diff --git a/docker-compose.yml b/docker-compose.yml index 57ddb0b..e328be4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,6 +15,7 @@ services: - API_TIMEOUT=${API_TIMEOUT} - LOGIN_PASSWORD=${LOGIN_PASSWORD} - ANNOUNCEMENT_TEXT=${ANNOUNCEMENT_TEXT} + - BACKEND_URL=${BACKEND_URL:-http://localhost:8888} volumes: - ./logs:/app/logs restart: unless-stopped @@ -34,6 +35,8 @@ services: container_name: stock-scanner-frontend ports: - "80:80" + environment: + - BACKEND_URL=${BACKEND_URL:-http://backend:8888} depends_on: - backend restart: unless-stopped diff --git a/frontend/Dockerfile b/frontend/Dockerfile index fc329c1..77e6ec2 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -20,11 +20,17 @@ RUN npm run build FROM nginx:stable-alpine as production-stage # 复制自定义nginx配置(如需要) -COPY nginx.conf /etc/nginx/conf.d/default.conf +COPY nginx.conf /etc/nginx/conf.d/default.conf.template # 从构建阶段复制构建结果到nginx服务目录 COPY --from=build-stage /app/dist /usr/share/nginx/html +# 创建启动脚本 +RUN echo '#!/bin/sh \n\ +envsubst "\$BACKEND_URL" < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf \n\ +nginx -g "daemon off;"' > /docker-entrypoint.sh && \ +chmod +x /docker-entrypoint.sh + # 暴露80端口 EXPOSE 80 @@ -32,5 +38,5 @@ EXPOSE 80 HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD wget --quiet --tries=1 --spider http://localhost:80 || exit 1 -# 设定Nginx是否以守护进程的方式启动 -CMD ["nginx", "-g", "daemon off;"] +# 设定启动命令 +CMD ["/docker-entrypoint.sh"] diff --git a/frontend/nginx.conf b/frontend/nginx.conf index 046294f..4456ae7 100644 --- a/frontend/nginx.conf +++ b/frontend/nginx.conf @@ -15,7 +15,7 @@ server { # API请求代理到后端服务 location /api/ { - proxy_pass http://backend:8888/api/; + proxy_pass ${BACKEND_URL}/api/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; @@ -25,35 +25,35 @@ server { } location /login { - proxy_pass http://backend:8888/login; + proxy_pass ${BACKEND_URL}/login; proxy_http_version 1.1; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } location /check_auth { - proxy_pass http://backend:8888/check_auth; + proxy_pass ${BACKEND_URL}/check_auth; proxy_http_version 1.1; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } location /need_login { - proxy_pass http://backend:8888/need_login; + proxy_pass ${BACKEND_URL}/need_login; proxy_http_version 1.1; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } location /config { - proxy_pass http://backend:8888/config; + proxy_pass ${BACKEND_URL}/config; proxy_http_version 1.1; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } location /analyze { - proxy_pass http://backend:8888/analyze; + proxy_pass ${BACKEND_URL}/analyze; proxy_http_version 1.1; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade;