我正在学习 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 . .
答案 0 :(得分:0)
您需要先运行 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