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.
|
The app exposes When starting the container you can utilize Not sure if I got your pull request tho. |
Okay, the test just now confirmed that it solves the problem. I suggest adding the |
问题描述
在使用 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,消除不必要的控制台报错。