You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker构建FastAPI项目报错:uv命令未找到

解决Docker构建时"uv: not found"错误的方案

问题根源

你把uv复制到了/Scripts/目录,但这个目录不在Linux容器的PATH环境变量中,系统无法找到uv命令;同时你误用了Windows的路径习惯(Scripts),Linux容器里虚拟环境的可执行文件目录是bin而非Scripts。

修复方案

方案1:将uv复制到系统默认PATH目录(推荐)

修改Dockerfile中安装uv的行,把uv和uvx复制到Linux系统默认包含在PATH里的/usr/local/bin目录:

FROM python:3.11

# Install uv.
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /usr/local/bin/

# Copy the application into the container.
COPY . /app

# Install the application dependencies.
WORKDIR /app
RUN uv sync --frozen --no-cache

# Run the application.
CMD ["/app/.venv/bin/uvicorn", "src.main:app", "--port", "80", "--host", "0.0.0.0"]

方案2:将/Scripts/添加到PATH环境变量

如果坚持使用/Scripts/目录,需要手动将其加入PATH:

FROM python:3.11

# Install uv and add to PATH.
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /Scripts/
ENV PATH="/Scripts:${PATH}"

# Copy the application into the container.
COPY . /app

# Install the application dependencies.
WORKDIR /app
RUN uv sync --frozen --no-cache

# Run the application.
CMD ["/app/.venv/bin/uvicorn", "src.main:app", "--port", "80", "--host", "0.0.0.0"]

关键注意点

  • Linux容器中,虚拟环境的可执行文件存放在.venv/bin/而非Windows的.venv/Scripts/,所以启动命令里的路径必须修改,否则后续运行也会报错。
  • 可在构建时添加RUN ls /app/.venv命令,验证虚拟环境的目录结构是否正确。

内容的提问来源于stack exchange,提问作者GEBRU

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 23:03:16