我正在学习 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 . .
使用诗歌安装之前需要运行
RUN poetry config virtualenvs.create false
。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