skip to Main Content

I’m doing a practice application to learn docker, I’m doing the application with Python and fast Api along with its tutorial, I’m using everything with dockerfile and docker compose, which has a connection to the postgresql database, for that I’m using a orm sqlalchemy, running my application ‘normally’ from the command line, the project runs without any problem, but when running it with docker compose it generates several errors like the following:

ModuleNotFoundError: No module named 'routers'

When I ‘solve’ it, it generates another error which I have not been able to solve and I do not understand which is the following:

ModuleNotFoundError: No module named 'sqlalchemy'

Well, if I have sqlalchemy install, requirements.txt:

SQLAlchemy==1.4.39

This is my Python code, this is the main one:

from fastapi import FastAPI

from .routers import roles

app = FastAPI()


app.include_router(roles.router)

And this is the code where the error is generated:

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from db.postgres_connection import SessionLocal, engine
from models import roles
from schemas import roles as schemas

roles.Base.metadata.create_all(bind=engine)
router = APIRouter()


def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()


@router.get('/api/v1/roles/', response_model=list[schemas.RoleBase])
async def get_roles(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    roles = get_roles(db, skip=skip, limit=limit)
    return roles

This is my Dockerfile:

FROM python:3.10.5-slim-buster
WORKDIR /code

COPY ./app ./code/app
COPY ./requirements.txt /code/

RUN pip install -r requirements.txt
EXPOSE 8000

CMD [ "uvicorn", "app.main:app", "--reload" ]

This is my docker compose file:

version: '3.9'
services:
  web:
    build: .
    ports:
      - '8000:8000'
    volumes:
      - .:/app

  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: hamel
      POSTGRES_PASSWORD: contrasena
      POSTGRES_DB: bankmel
    volumes:
      - /home/isla/storage:/var/lib/postgresql/data
    ports:
      - '5432:5432'

structure folder:
enter image description here

2

Answers


  1. you got a couple of problems

    In the docker file change, your copy path code is creating one more directory under code.

    COPY ./app ./app
    

    then you will have a couple of other problems in your code.

    Login or Signup to reply.
  2. Modify your Dockerfile as follows, I’m sure this error will be fixed:

    FROM python:3.10.5-slim-buster
    WORKDIR /code
    
    COPY ./app ./app
    COPY ./requirements.txt ./requirements.txt
    
    RUN pip install -r requirements.txt
    EXPOSE 8000
    
    CMD [ "uvicorn", "app.main:app", "--reload" ]
    

    also in docker-compose.yml you should change volume in web as follows:

    volumes:
          - .:/code
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search