[英]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.