EDIT: I'm now thinking this is a red herring. It turns out there were some unrelated build problems in the module (string in one spot instead of str in a file that imported the string package). I suppose having unit tests that I could run separately to this docker container would be useful, who could have guessed. Still though, I wish the uvicorn error was more clear. Mods, I'd say leave this post up as module build issues will cause the same error string as the directory pointing issue referenced in the other questions.
I have a docker compose file that looks like this:
version: "3.9"
services:
backend:
build: nfp-backend
restart: always
ports:
- 8000:8000
volumes:
- ./nfp-backend:/usr/src/
....
My file structure looks like this:
├─db
├-docker-compose.yml
├─nfp-backend/
├── Dockerfile
├── README.md
├── alembic
│ ├── README
│ ├── __pycache__
│ ├── env.py
│ ├── script.py.mako
│ └── versions
├── alembic.ini
├── app
│ ├── __init__.py
│ ├── __pycache__
│ ├── main.py
│ └── routers
├── requirements.txt
└── venv
├─nfp-devops
├─nfp-frontend
├─nfp-test
The backend dockerfile is as follows
FROM python:3.9
WORKDIR /usr/src
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD alembic upgrade head && uvicorn app.main:app --reload --host 0.0.0.0 --reload-dir app --log-level debug
The relevant piece of main.py:
app = FastAPI()
The answers on other questions 1 2 surrounding this issue center on the directory structure not matching the uvicorn command.
For me, this shouldn't be the issue as we should be trying to run the command against app/main.py, which is what I'm doing.
From my backend docker container:
# pwd
/usr/src
# ls
Dockerfile README.md alembic alembic.ini app requirements.txt venv
# ls app
__init__.py __pycache__ converters.py routers main.py
The self-answer on this question implies that there could be an issue with imports? I'm not an expert in python by any means, so maybe that's possible. The error message doesn't seem to imply that, though.