Docker容器中使用uv安装FastAPI后,fastapi命令无法在PATH中找到的问题求助
Docker容器中使用uv安装FastAPI后,fastapi命令无法在PATH中找到的问题求助
大家好,我最近在自己的业余项目里尝试把uv和Docker结合起来用,跟着官方示例改了Dockerfile,只调整了CMD部分指向我的main文件,结果运行容器的时候遇到了问题,想请大家帮忙排查下哪里出错了!
我的Dockerfile内容如下:
# Dockerfile FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy WORKDIR /app RUN --mount=type=cache,target=/root/.cache/uv \ --mount=type=bind,source=uv.lock,target=uv.lock \ --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ uv sync --frozen --no-install-project --no-dev ADD . /app RUN --mount=type=cache,target=/root/.cache/uv \ uv sync --frozen --no-dev FROM python:3.12-slim-bookworm # Copy the application from the builder COPY --from=builder --chown=app:app /app /app # Place executables in the environment at the front of the path ENV PATH="/app/.venv/bin:$PATH" # Run the FastAPI application by default CMD ["fastapi", "dev", "--host", "0.0.0.0", "main"]
这是我的pyproject.toml配置:
[project] name = "backend" version = "0.1.0" description = "Add your description here" readme = "README.md" requires-python = ">=3.10" dependencies = [ "fastapi[standard]>=0.115.6", "pydantic-settings>=2.7.1", "sqlalchemy>=2.0.37", "uvicorn>=0.34.0", "websockets>=14.1", ] [dependency-groups] dev = ["pytest>=8.3.4"] [tool.uv] dev-dependencies = ["ruff>=0.6.2", "fastapi-cli>=0.0.5"]
我的项目目录结构是这样的:
. ├── backend │ ├── main.py │ ├── Dockerfile │ ├── pyproject.toml │ ├── uv.lock │ ├── app │ │ ├──etc...
main.py里包含了FastAPI的app实例。
当我构建并运行容器时,收到了如下错误提示:
Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "fastapi": executable file not found in $PATH: unknown
我检查过PATH的设置,明明已经把虚拟环境的bin目录放到了最前面,但还是找不到fastapi命令。有没有大佬能帮我找出问题所在呀?
备注:内容来源于stack exchange,提问作者kamagitheboilerman




