Poetry fails to run any command inside FastAPI web app’s Docker container

  docker, docker-compose, fastapi, python, python-poetry

I’m trying to set up a FastAPI web api inside a Docker container (I’m using Poetry as my dependency manager). It runs fine, however I’m unable to run tests by using Pytest (inside the container, of course), which is installed. After running docker exec -it app poetry show the output I get seems normal, and I can access the app by entering localhost:8000 in a broswer, but even when trying docker exec -it app poetry add {some_new_python_package} it throws me errors.
PS: I wouldn’t like to use pipenv since I quite like using poetry.

The command that I use to try to run the tests is

(c.1) docker exec -it perseverance-api-app poetry run pytest tests/

Below is the error python gives me, as well as any relevant information I could think so.

Output of the command (c.1)

FileNotFoundError

  [Errno 2] No such file or directory

  at /usr/lib/python3.8/os.py:591 in _execvpe
       587│         argrest = (args,)
       588│         env = environ
       589│ 
       590│     if path.dirname(file):
    →  591│         exec_func(file, *argrest)
       592│         return
       593│     saved_exc = None
       594│     path_list = get_exec_path(env)
       595│     if name != 'nt':

Dockerfile content

FROM ubuntu:20.04

ARG APP_PHASE_ENV

ENV APP_PATH=/home/app

ENV LANG=en_US.UTF-8
ENV LC_ALL=en_US.UTF-8
ENV LC_TIME=en_DK.UTF-8
ENV DEBIAN_FRONTEND=noninteractive

# system dependencies
RUN apt-get update && 
    apt-get install --no-install-recommends -y 
    bash 
    build-essential 
    git 
    libpq-dev 
    python3-pip 
    # Clean cache
    && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false 
    && apt-get clean -y && rm -rf /var/lib/apt/lists/*

ENV APP_PHASE_ENV=${APP_PHASE_ENV} 
    PYTHONUNBUFFERED=1 
    PYTHONFAULTHANDLER=1 
    PYTHONDONTWRITEBYTECODE=1 
    PYTHONHASHSEED=random 
    # pip only:
    PIP_NO_CACHE_DIR=off 
    PIP_DISABLE_PIP_VERSION_CHECK=on 
    # poetry only
    POETRY_VERSION="1.1.4" 
    POETRY_NO_INTERACTION=1 
    POETRY_VIRTUALENVS_IN_PROJECT=1

# install poetry
RUN pip3 install --no-cache-dir "poetry==$POETRY_VERSION"

WORKDIR ${APP_PATH}
COPY pyproject.toml poetry.lock ${APP_PATH}/

# install project dependencies
RUN poetry install 
    $(if [ "$APP_PHASE_ENV" = 'production' ]; then echo '--no-dev'; fi) 
    --no-interaction --no-ansi 
    # clean poetry installation's cache
    && if [ "$APP_PHASE_ENV" = 'production' ]; then rm -rf "$POETRY_CACHE_DIR"; fi

COPY . ${APP_PATH}
EXPOSE 8000

docker-compose file content

version: "3.9"
services:
  app:
    container_name: app
    build:
      context: .
      args:
        - APP_PHASE_ENV=${APP_PHASE_ENV}
    ports:
      - "8000:8000"
    volumes:
      - .:/home/app
    command: poetry run python3 ./src/main.py

App root dir

.
├── docker-compose.yml
├── Dockerfile
├── poetry.lock
├── pyproject.toml
├── README.md
├── src
│   ├── __init__.py
│   └── main.py
└── tests
    ├── __init__.py
    ├── pytest.ini
    ├── test_api_version.py
    └── test_root.py

*** the command is pytest tests/ because that is where the pytest.ini file is and it seems well structured as it is. Maybe the problem is related to the path? Thanks in advanced

Source: Docker Questions

LEAVE A COMMENT