简体   繁体   中英

Docker-compose - module import issue

I am working on a Flask app and my docker-compose.yml is not able to find my config folder. I have __init__.py files at every levels and I tried to add/remove the config path from my docker-compose.yml file but still the same issue. This is my project structure:

.
├── ...
├── web_messaging                    
│   ├── app.py 
│   ├── __init__.py        
│   ├── ...       
│   └── config 
│       ├── settings.py 
│       ├── gunicorn.py
│       └── __init__.py
│            
├── __init__py             
├── .env             
├── requirements.txt              
├── docker-compose.yml
└── Dockerfile

When I am running docker-compose up --build , I am getting this error:

website_1  | - 'config' not found.
website_1  | 
website_1  | Original exception:
website_1  | 
website_1  | ModuleNotFoundError: No module named 'config'
website_1  | [2021-04-04 15:23:26 +0000] [8] [INFO] Worker exiting (pid: 8)
website_1  | [2021-04-04 15:23:26 +0000] [1] [INFO] Shutting down: Master
website_1  | [2021-04-04 15:23:26 +0000] [1] [INFO] Reason: Worker failed to boot.

docker-compose.yml

version: '2'

services:
  website:
    build: .
    command: >
      gunicorn -c "python:web_messaging.config.gunicorn" --reload "web_messaging.app:create_app()"
    env_file:
      - '.env'
    volumes:
      - '.:/web_messaging'
    ports:
      - '8000:8000'

app.py

def create_app(settings_override=None):
    """
    Create a Flask application using the app factory pattern.

    :param settings_override: Override settings
    :return: Flask app
    """
    app = Flask(__name__, instance_relative_config=True)

    app.config.from_object('config.settings')
    app.config.from_pyfile('settings.py', silent=True)

    if settings_override:
        app.config.update(settings_override)

    error_templates(app)
    app.register_blueprint(user)
    app.register_blueprint(texting)
    extensions(app)
    configure_context_processors(app)
    return app
...

Dockerfile

FROM python:3.7.5-slim-buster

RUN apt-get update && apt-get install -qq -y \
  build-essential libpq-dev --no-install-recommends

ENV INSTALL_PATH /web_messaging
RUN mkdir -p $INSTALL_PATH

WORKDIR $INSTALL_PATH

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

CMD gunicorn -c "python:web_messaging.config.gunicorn" "web_messaging.app:create_app()"

Seems I solved it. The config folder must be outside of the main_messaging folder,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM