current i have a fastapi app that i am developing and try to do testing under 2 machine. 1 with docker and another one without.
in this case without docker one, i wanted to launch uvicorn from the terminal as usual
uvicorn main:app --reload
is possible to run without issue, with my main.py having imports from another file like this
from util import search_card_price
with the same thing, i went for docker and try to build it, it will give me this error
web_1 | File "./app/main.py", line 6, in <module>
web_1 | from util import search_card_price
web_1 | ModuleNotFoundError: No module named 'util'
what is the best way for me to do my import that works on both places?
my current application is something like for the structure,
fastapi app main folder
|
->app
|
-> main.py
-> util.py
-> config.py
-> test.py
i know if using something like this will work for docker
from .util import search_card_price
but with this solution, is not working on my local terminal run without docker. i need to know how to solve both at the same time
here is my dockerfile
# Dockerfile
# pull the official docker image
FROM python:3.9.4-slim
# set work directory
WORKDIR /app
# set env variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
and docker compose
version: '3.8'
services:
web:
build: .
command: bash -c 'uvicorn app.main:app --host 0.0.0.0'
volumes:
- .:/app
expose:
- 8000
labels:
- "traefik.enable=true"
- "traefik.http.routers.fastapi.rule=Host(`testing.localhost`)"
traefik:
image: traefik:v2.2
ports:
- 80:80
- 8081:8080
volumes:
- "./traefik.dev.toml:/etc/traefik/traefik.toml"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
2
Answers
actually it turns out i can use relative import
for local run as well, the relative import works on docker but not on my localhost here without docker is because i am using this in my terminal to start fastapi
but now, if i retain with relative import and use this command instead
adding app. infront of the main and execute this command in my root project folder, everything works fine for my local with relative import it seems.
If the Dockerfile is outside the
app
directory, you are copying the directory too the/app
inside the docker. That moves theutil.py
to/app/app/util.py
inside docker. You can useto skip creating another
app
in/app
.You may need remove
app.
from the command as well.