skip to Main Content
FROM python:3.8
WORKDIR /app 

COPY requirements.txt /
RUN pip install --requirement /requirements.txt

COPY ./app /app

EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host=0.0.0.0" , "--reload" , "--port", "8000"]

when i used

docker-compose up -d
ModuleNotFoundError: No module named ‘app’

  • the folders in Fastapi framework:

  • fastapi

    • app

      -main.py

  •    language_detector.py
    
  • Dockerfile

  • docker-compose

3

Answers


  1. Try creating the /app folder before

    FROM python:3.8
    RUN mkdir -p /app
    WORKDIR /app 
    
    COPY requirements.txt /
    RUN pip install --requirement /requirements.txt
    
    COPY ./app /app
    
    EXPOSE 8000
    CMD ["uvicorn", "app.main:app", "--host=0.0.0.0" , "--reload" , "--port", "8000"]
    

    And launching it:

    docker-compose up –build

    Login or Signup to reply.
  2. CMD ["uvicorn", "main:app", "--host=0.0.0.0" , "--reload" , "--port", "8000"]
    

    Your work directory is /app and the main.py file is already there. So you don’t need to call app.main module. Just call main.py script directly in CMD.

    Login or Signup to reply.
  3. The issue could likely be caused by how you define the volume mounts on docker compose:
    More on mounts here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search