uvicorn-gunicorn-fastapi-docker
uvicorn-gunicorn-fastapi-docker copied to clipboard
no module found or cannot import package on startup
I can't quite figure out how to set up my app in the Dockerfile. My repo contains the following structure. valis
is the python package that contains my FastAPI app, set up for larger scale, with the app
variable inside the module wsgi.py
or main.py
. In the code I also import valis at times to load utility functions, get versions and config, etc.
- Dockerfile
- pyproject.toml
- poetry.lock
- python
- valis
-
__init__.py
- main.py
- wsgi.py (app variable)
- routes
- paths.py
- ...
-
- valis
My Dockerfile is the following
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
# Install Poetry
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | POETRY_HOME=/opt/poetry python && \
cd /usr/local/bin && \
ln -s /opt/poetry/bin/poetry && \
poetry config virtualenvs.create false
# Copy using poetry.lock* in case it doesn't exist yet
COPY ./pyproject.toml ./poetry.lock* ./
RUN poetry install --no-root --no-dev
# copy over application
COPY ./python/valis /app
# set environment variables
ENV MODULE_NAME = "valis.wsgi"
This should create a folder at /app/valis
and APP_MODULE
should resolve to valis.wsgi:app
. However, when I start the docker, I keep getting errors that either gunicorn cannot find the module ModuleNotFoundError: No module named '= valis'
, or it cannot import the module valis
, e.g.
File "/app/main.py", line 17, in <module>
import valis
depending on how I change my Dockerfile settings. I've tried setting WORKDIR /app
and ENV PYTHONPATH = "${PYTONPATH}:/app"
at the final point to try to ensure the /app/valis
is in the python path. I've also tried COPY ./python/valis /app/app/
. None of these work. What's the suggested way of setting this up so that the app can be accessed and valis
is a valid python package?
Arrange your dockerfile like this
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
# Install Python requirements
COPY requirements.txt /app/
WORKDIR /app/
RUN python -m pip install -r requirements.txt
# Install Python app
COPY <app-module> /app/<app-module>
ENV APP_MODULE=<app-module>.main:app
Thanks for the help here @osimuka ! 👏 🙇
If the original problem is solved, then you can close this issue @havok2063 ✔️
Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.