简体   繁体   English

将应用程序 docker 化后无法访问 flask 端点

[英]Unable to access flask endpoints after dockerizing the app

I'm working on a flask application which has a list of urls registered.我正在开发一个已注册 url 列表的 flask 应用程序。 I was able to run the flask app locally and access the urls which are registered for it.我能够在本地运行 flask 应用程序并访问为其注册的 url。

But when I dockerize the app and running it using docker-compose , I'm not able to access the urls for the app.但是,当我将应用程序 dockerize 并使用docker-compose运行它时,我无法访问该应用程序的网址。

When running the app locally, if I access the app.url_map of the app, I was able to see list of all registered urls for that app.在本地运行应用程序时,如果我访问应用程序的app.url_map ,我能够看到该应用程序的所有注册 url 列表。 (I created an endpoint - /urls which when hit returns url_map list) (我创建了一个端点 - /urls当点击返回url_map列表)

But when I try to hit the same endpoint - /urls after running the app in docker, I don't see any list in that except the /urls endpoint which is basically itself.但是,当我在 docker 中运行应用程序后尝试访问相同的端点 - /urls时,除了/urls端点(基本上就是它本身)之外,我看不到任何列表。

Where am I going wrong?我哪里错了?

Here's the wsgi.py file:这是wsgi.py文件:

import os
from flask import Flask, jsonify
from app.workload.views import register_workload_urls

def load_urls(app):
    app_mode = os.getenv("APP_MODE")
    if app_mode == "WORKLOAD":
        register_workload_urls(app)

application = Flask(__name__)

@application.route('/urls')
def urls():
    return jsonify({"APP": str(os.getenv("APP_MODE")), "URLs registered": f"{str(application.url_map)}"})

if __name__ == "__main__":
    load_urls(application)
    application.run()

views.py:视图.py:

def register_workload_urls(application):
    @application.route('/api/workload/health/check/')
    def w_check():
        return jsonify({'status': f"Workload service is {app.config_pub.status}."})

    @application.route('/api/workload/health/up/')
    def w_up():
        app.config_pub.status = "UP"
        app.config_pub.dispatch("UP")
        return jsonify({'status': f"Workload service is {app.config_pub.status}."})

    @application.route('/api/workload/health/down/')
    def w_down():
        app.config_pub.status = "DOWN"
        app.config_pub.dispatch("DOWN")
        return jsonify({'status': f"Workload service is {app.config_pub.status}."})

docker-compose.yml: docker-compose.yml:

  workload_service:
    container_name: workload_container
    restart: always
    image: workload
    build: 
      context: ./dsdp
      dockerfile: Dockerfile.app.local
    ports:
      - "5000:5000"
    environment:
        - PYTHONPATH=.
        - FLASK_APP=wsgi.py
        - FLASK_DEBUG=1
        - CONFIG_PATH=config/local
        - APP_MODE=WORKLOAD
    command: flask run --host=0.0.0.0 --port 5000

Dockerfile.app.local: 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
EXPOSE 5000

Output of /urls when running in docker:在 docker 中运行时/urls的 Output:

{
  "APP": "WORKLOAD",
  "URLs registered": "Map([<Rule '/urls' (GET, HEAD, OPTIONS) -> urls>,\n <Rule '/static/<filename>' (GET, HEAD, OPTIONS) -> static>])"
}

Looks like some issue with having the code in wsgi.py file.wsgi.py文件中包含代码似乎有些问题。 Removed app creation code from wsgi.py file and pasted in __init__.py file in app folder and imported the function from there.wsgi.py文件中删除应用程序创建代码并粘贴到app文件夹中的__init__.py文件中,并从那里导入 function。

Everything is working as expected now.现在一切都按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在另一个python文件中访问flask app端点? - Access flask app endpoints in another python file? 在Docker化Python API / Flask APP时给出pyodbc.Error - Giving pyodbc.Error while Dockerizing a Python API/Flask APP 添加 https 支持后,无法访问托管在 EC2 上的 Flask 应用程序 - Unable to access Flask app hosted on EC2 after adding https support 如何将对谷歌应用程序引擎 flask 端点的访问限制为仅应用程序代码(或应用程序引擎服务帐户) - How to limit access to google app engine flask endpoints to just application code (or app engine service accounts) 无法访问部署在 Azure 应用服务上的 Flask 应用 - Unable to access Flask App deployed on Azure App Service 如何将 Flask API 端点连接到 React 应用程序 - How to connect Flask API endpoints to React app 基本烧瓶应用程序中所有端点上的 404 错误 - 404 error on all endpoints in a basic flask app 无法在 http://localhost:5000 访问 Dockerized Flask 应用程序 - Unable access Dockerized Flask app at http://localhost:5000 无法从php curl访问Flask Web应用 - Unable to access Flask web app from php curl 无法访问 Flask 应用生产服务器中的查询字符串参数 - Unable to access query string parameters in Flask app production server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM