【问题标题】:Not able to access endpoints when running flask application inside docker container在 docker 容器中运行烧瓶应用程序时无法访问端点
【发布时间】:2019-10-08 15:49:55
【问题描述】:

我正在尝试对烧瓶应用程序进行 dockerize。但是在 http://127.0.0.1:5000/ 上点击任何端点时会出现以下错误。我已将端口从 localhost 正确映射到 container

flask.cli.NoAppException flask.cli.NoAppException:导入时 “dsdp.wsgi”,引发了 ImportError:

Traceback(最近一次调用最后一次):文件 “/usr/local/lib/python3.7/site-packages/flask/cli.py”,第 240 行,在 定位应用 import(module_name) 文件“/var/www/dsdp/wsgi.py”,第 4 行,在 from app.common.observer import ConfigPublisher, QueueSubscriber ModuleNotFoundError: No module named 'app'

这是我容器内的结构。

root@6c37b85d98cb:/var/www/dsdp# ls -ltr
total 32
-rw-rw-r-- 1 root root  135 Oct  4 07:55 uwsgi.local.ini
-rw-rw-r-- 1 root root  598 Oct  4 07:55 requirements.txt
drwxrwxr-x 5 root root 4096 Oct  4 07:55 config
-rw-rw-r-- 1 root root    0 Oct  4 07:55 __init__.py
-rwxrwxr-x 1 root root  175 Oct  7 17:14 entrypoint.sh
-rw-rw-r-- 1 root root  742 Oct  7 17:36 wsgi.py
drwxrwxr-x 7 root root 4096 Oct  7 17:43 app
-rw-rw-r-- 1 root root  614 Oct  7 17:45 Dockerfile.app.local

FLASK_APP 环境变量:

root@6c37b85d98cb:/var/www/dsdp# echo $FLASK_APP 
wsgi.py

wsgi.py 中,我尝试如下导入

from app.common.observer import ConfigPublisher, QueueSubscriber
from app.common.utils.endpoints import register_blueprints

当我在本地运行烧瓶应用程序时,这些东西运行良好。但是当我在docker中运行时它会抛出错误。我在 app 文件夹中有一个 __init__.py

这是我用来构建镜像的 Dockerfile。

Dockerfile.app.local:

FROM python:3.7
RUN mkdir -p /var/www/dsdp/
WORKDIR /var/www/dsdp/
COPY . /var/www/dsdp/
RUN pip3 install --no-cache-dir -r requirements.txt
COPY uwsgi.local.ini /etc/uwsgi.ini
EXPOSE 5000

这就是我使用 docker-compose.yml

构建图像的方式
  workload_service:
    container_name: workload_container
    restart: always
    image: workload
    build: 
      context: ./dsdp
      dockerfile: Dockerfile.app.local
    depends_on:
        - postgres_service
    ports:
      - "5000:5000"
    environment:
        - FLASK_APP=wsgi.py
        - FLASK_DEBUG=1
        - CONFIG_PATH=config/local
        - FLASK_CONFIG=local
        - APP_NAME=WORKLOAD
        - PYTHONPATH=.
    command: flask run --host=0.0.0.0 --port 5000

我在哪里犯错了?

【问题讨论】:

  • 您的 pip 安装运行正常吗?您可以切换到另一个基本图像来进行测试吗? Here is one that you can try
  • pip 安装运行良好。容器也已启动并运行。唯一的问题是,当我到达终点时,我没有得到预期的输出。
  • 也许,可能是一些带有 Alpine 的 python 图像没有获取的库。尝试将基础镜像更改为更完整的镜像,例如 ubuntu。
  • 您是否尝试将PYTHONPATH=. 添加到您的dockerfile 中?还是在容器启动时设置该变量?
  • 你的 Dockerfile 没有CMD;它是如何产生你显示的错误的?

标签: python docker flask docker-compose dockerfile


【解决方案1】:

99%(或至少绝大多数)发生这种情况,这是因为容器内部使用了 127.0.0.1:5000。那就是localhost inside 容器。这与localhost 在容器外(在主机中)不同。

解决方法是在容器内绑定到(设置host= 到)0.0.0.0:5000

【讨论】:

    【解决方案2】:

    把你的from改成:

    from .app.common.observer
    

    尝试在 Dockerfile 中设置您的 FLASK_APP

     ENV FLASK_APP dsdp.wsgi:app
     ARG FLASK_APP=dsdp.wsgi:app
    

    来自文档:

    FLASK_APP=你好

    The name is imported, automatically detecting an app (app) or factory (create_app).
    

    【讨论】:

    • 我也需要使用ARG吗?
    • 构建步骤中需要Arg 我不知道你的dockerfile是怎么写的
    • PYTHONPATH=. 移到环境变量顶部后未收到任何错误。
    猜你喜欢
    • 2018-03-21
    • 2017-10-18
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多