运行 docker 时在 $PATH 中找不到 uvicorn 可执行文件

问题描述 投票:0回答:1

我正在学习 testdriven.io 的“使用 FastAPI 和 Docker 进行测试驱动开发”课程。当我准备启动 docker 容器时,遇到了这个错误:

ERROR: for web  Cannot start service web: OCI runtime create failed: container_linux.go:370: starting container process caused: exec: "uvicorn": executable file not found in $PATH: unknown

这是我的 docker-compose.yml:

version: '3.8'

services:
  web:
    build: ./project
    command: uvicorn app.main:app --reload --workers 1 --host 0.0.0.0 --port 8000
    volumes:
      - ./project:/usr/src/app
    ports:
      - 8004:8000
    environment:
      - ENVIRONMENT=dev
      - TESTING=0

我更新的唯一事情是我没有使用 pip,而是使用

poetry
,所以我不确定这是否与该问题有关。这是我使用
Dockerfile
poetry
:

FROM python:3.9.2-slim-buster

WORKDIR /usr/src/app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apt-get update \
    && apt-get -y install netcat gcc \
    && apt-get clean

RUN pip install --upgrade pip
RUN pip install poetry

COPY pyproject.toml .
COPY poetry.lock .

RUN poetry install --no-dev

COPY . .
python docker python-poetry
1个回答
3
投票

使用诗歌安装之前需要运行

RUN poetry config virtualenvs.create false

默认情况下,Poetry 在安装依赖项之前会创建一个虚拟环境。这可以防止它。
最后
Dockerfile
应该看起来像这样:

FROM python:3.9.5-slim-buster

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apt-get update \
    && apt-get -y install netcat gcc \
    && apt-get clean

WORKDIR /usr/src/app

COPY ./pyproject.toml ./poetry.lock* /usr/src/app

RUN pip install --upgrade pip
RUN pip install poetry
RUN poetry config virtualenvs.create false

RUN poetry install --no-dev

COPY . /usr/src/app
© www.soinside.com 2019 - 2024. All rights reserved.