Open
Conversation
This fixes the issue where the frontend in docker tries to connect to localhost instead of the Vite proxy.
Replace ghcr.io/astral-sh/uv with pip install using aliyun mirror to prevent hanging. Also configure npm and uv to use domestic registry/mirror.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题描述
在使用 Docker 部署 (docker-compose up -d) 项目并在公网环境访问 Web 页面时,遇到了以下严重问题:
API 请求
ERR_CONNECTION_REFUSED(Network Error)报错:Network Error,无法访问后端接口。src/api/index.js中的baseURL写死了回退地址为绝对路径http://localhost:5001。这导致用户的浏览器加载页面后,会尝试去请求**用户自己电脑(localhost)**上的 5001 端口,而不是服务器 Docker 容器里的 5001 端口。因为跨域和本机无服务的双重原因,所有的 API 请求全部被拒绝。构建镜像极度缓慢甚至长时卡死:
docker-compose build时,经常卡在=> FROM ghcr.io/astral-sh/uv:0.9.26...长达数分钟甚至失败。ghcr.io(GitHub Container Registry) 镜像非常不稳定。此外,后续的 npm ci 和 uv sync 过程也没有配置国内镜像加速源,导致构建过程十分痛苦。spawn xdg-open ENOENT报错干扰:docker-compose logs)中经常输出这个错误。frontend/vite.config.js启用了open: true,Vite 会尝试打开浏览器,但 Docker 容器内并无桌面和 xdg-open 环境产生的报错。修复说明
移除
frontend/src/api/index.js中的 fallbackhttp://localhost:5001,将其改为空字符串''。/api/simulation/history),这会让请求发给当前的来源 URL(比如http://公网IP:3000/api/...),然后 Vite dev server 的 proxy 机制就能在容器内部正常把/api转发给 Flask 后端(localhost:5001)。彻底解决了 Docker 部署下的联通性问题。优化
Dockerfile中针对国内网络的构建流程(提速显著):COPY --from=ghcr.io/astral-sh/uv直接拉取镜像。RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ uv,使用阿里云 Pypi 源秒速安装 uv。npm config set registry https://registry.npmmirror.com,使用淘宝镜像秒速拉取前端构建包。env UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/ uv sync --frozen,使用阿里源加速 Python 包下载。移除
frontend/vite.config.js的open: true,消除不必要的控制台报错。