[英]Docker-compose - module import issue
我正在开发 Flask 应用程序,而我的docker-compose.yml
无法找到我的配置文件夹。 我在每个级别都有__init__.py
文件,我尝试从docker-compose.yml
文件中添加/删除配置路径,但仍然是同样的问题。 这是我的项目结构:
.
├── ...
├── web_messaging
│ ├── app.py
│ ├── __init__.py
│ ├── ...
│ └── config
│ ├── settings.py
│ ├── gunicorn.py
│ └── __init__.py
│
├── __init__py
├── .env
├── requirements.txt
├── docker-compose.yml
└── Dockerfile
当我运行docker-compose up --build
时,我收到此错误:
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'
应用程序.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()"
看来我解决了。 config
文件夹必须在main_messaging
文件夹之外,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.