skip to Main Content

enter image description here

Here is the Dockerfile

FROM python:3.11

WORKDIR /usr/src/app

COPY requirements.txt ./

RUN pip install --no-cache-dir -r requirements.txt

COPY src/ .

EXPOSE ${SERVER_PORT}

ENV FLASK_APP=application/app.py
ENV FLASK_ENV=development

CMD ["flask", "run", "--host=0.0.0.0"]

docker-compose.yml

version: '3.8'

services:
  mysql:
    container_name: mysql
    image: mysql:latest
    restart: always
    environment:
      MYSQL_DATABASE: 'mysql'
      MYSQL_USER: 'admin'
      MYSQL_PASSWORD: 'admin'
      MYSQL_ROOT_PASSWORD: 'admin'
    ports:
      - 3307:3306

  web:
    container_name: webapp
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "5001:5000"
    depends_on:
      - mysql

in the app.py I am accessing the database package

from database.dbscript import create_table_from_schema
print("hello")

This works fine in my local however when I deploy it to docker (docker compose up -d) getting error in webapp container as below.

2024-01-23 09:19:35 Usage: flask run [OPTIONS]
2024-01-23 09:19:35 Try 'flask run --help' for help.
2024-01-23 09:19:35 
2024-01-23 09:19:35 Error: While importing 'app.application.app', an ImportError was raised:
2024-01-23 09:19:35 
2024-01-23 09:19:35 Traceback (most recent call last):
2024-01-23 09:19:35   File "/usr/local/lib/python3.9/site-packages/flask/cli.py", line 247, in locate_app
2024-01-23 09:19:35     __import__(module_name)
2024-01-23 09:19:35   File "/app/application/app.py", line 1, in <module>
2024-01-23 09:19:35     from database.dbscript import create_table_from_schema
2024-01-23 09:19:35 ModuleNotFoundError: No module named 'database'

What could be the reason? Why docker not able to access database package.

Thanks in advance.

Tried tweaking docker compose and Dockerfile but did not help. However the app runs just fine locally and I am using Python 3.11 locally.

2

Answers


  1. Chosen as BEST ANSWER

    src worked fine but I come up this line to introduce before the import which has resolved the relative path issue.

    project_root = os.path.abspath(os.path.join(os.path.dirname(file), "..")) // Depending upon the path

    sys.path.append(project_root)

    As a result it worked fine with database.dbscript import create_table_from_schema


  2. There seems to be a problem with init.py file in your database package, so it can’t find the package file. could you provide additional information about the project directory structure?

    After seeing the additional information, You could use a relative import.

    from ..database.dbscript import create_table_from_schema
    

    Also, you can use an absolute import approach by means of the addition of init.py file in the parent folder src. Then you could import function using some import statement like:

    from src.database.dbscript import create_table_from_schema
    

    However, relative imports are preferred due to changes in the project structure could lead to problems. Code maintainability could be difficult.

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