Search code examples
pythondockerpython-poetry

uvicorn executable file not found in $PATH when running docker


I'm going through the course Test-Driven Development with FastAPI and Docker from testdriven.io. When I was about to start the docker container I was met with this error:

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

This is my 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

The only thing that I updated is instead of using pip, I am using poetry so I'm not sure if this is related to the issue. Here is my Dockerfile using 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 . .

Solution

  • You need to run RUN poetry config virtualenvs.create false before installing using poetry.
    Poetry by default creates a virtual environment before installing dependencies. This will prevent it.
    Finally Dockerfile should look something like this:

    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