Reputation: 123
Can anyone see what im doing wrong here? works fine locally, but when running it in docker it cant seem to find my modules ...
The init.py files are emtpy if that info could help. Im no expert in docker and non of the tips I've googled/stackoverflowed so far has panned out, such as adding pythonpath env in the dockerfile.
Error log from docker:
Traceback (most recent call last):
File "/usr/local/bin/uvicorn", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.7/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.7/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/uvicorn/main.py", line 331, in main
run(**kwargs)
File "/usr/local/lib/python3.7/site-packages/uvicorn/main.py", line 354, in run
server.run()
File "/usr/local/lib/python3.7/site-packages/uvicorn/main.py", line 382, in run
loop.run_until_complete(self.serve(sockets=sockets))
File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
File "/usr/local/lib/python3.7/site-packages/uvicorn/main.py", line 389, in serve
config.load()
File "/usr/local/lib/python3.7/site-packages/uvicorn/config.py", line 288, in load
self.loaded_app = import_from_string(self.app)
File "/usr/local/lib/python3.7/site-packages/uvicorn/importer.py", line 23, in import_from_string
raise exc from None
File "/usr/local/lib/python3.7/site-packages/uvicorn/importer.py", line 20, in import_from_string
module = importlib.import_module(module_str)
File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "./main.py", line 3, in <module>
from routers.user import router as user_router
ModuleNotFoundError: No module named 'routers'
Dockerfile:
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
RUN apt-get update
RUN apt-get install -y --no-install-recommends
# install requirements
RUN pip3 install fastapi[all] uvicorn[standard]
# Move files
COPY ./* /app
# attemt to fix a python ModuleNotFoundError
WORKDIR /app
#ENV PATH=$PATH:/app
ENV PYTHONPATH "${PYTHONPATH}:/app"
CMD [ "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "15400"]
Docker-compose.yml:
version: '3'
services:
core_api:
build: .
container_name: "core-api-container"
ports:
- "8000:15400"
volumes:
- ./app/:/app
file structure:
API/
--__init__.py
--Dockerfile
--docker-compose.yml
--main.py
--routers/
--__init__.py
--user.py
--x.py
main.py:
from fastapi import FastAPI
from routers.user import router as user_router
from routers.x import router as x_router
app = FastAPI()
app.include_router(user_router)
app.include_router(x_router)
Upvotes: 1
Views: 3713
Reputation: 6376
I had almost the same issue with this and I try to fix it not by changing the directory structure of my application or how I copy the files but by running the application directly using gunicorn.
I run it with the following command :
gunicorn -k uvicorn.workers.UvicornWorker run:app
that can either be updated in the entrypoint of your docker file or in the command section of compose if you are using docker-compose.
Upvotes: 0
Reputation: 123
After running docker container run -it your-docker-container bash
and checked the files, it seems docker does not copy the file hierarchy as i expected. all files was under the same folder /app. none of my subfolder from the project in my local files were added, just the files those contained. No wonder i got ModuleNotFoundError.
To fix this i simply made a new project and put all my python files in the same directoy, and edited them all to import correctly. There is probably a simpler way to fix this, but cba. to figure that out at this time.
Upvotes: 1