问题描述
我正在开发一个烧瓶应用程序,该应用程序已注册了URL列表。我能够在本地运行flask应用程序,并访问为其注册的URL。
I'm working on a flask application which has a list of urls registered. I was able to run the flask app locally and access the urls which are registered for it.
但是当我对应用程序进行Docker化并使用运行它时docker-compose
,我无法访问该应用程序的网址。
But when I dockerize the app and running it using docker-compose
, I'm not able to access the urls for the app.
在本地运行该应用程序时,如果我访问该应用程序的 app.url_map
,我就能看到该应用程序的所有已注册网址的列表。 (我创建了一个端点- / urls
,当命中时返回 url_map
列表)
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. (I created an endpoint - /urls
which when hit returns url_map
list)
但是当我尝试在docker中运行该应用程序后击中相同的端点- / urls
时,除了 / urls
端点,它本身就是自己。
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.
我要去哪里错了?
这是 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:
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:
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:
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
在docker中运行时输出 / url
:
Output of /urls
when running in docker:
{
"APP": "WORKLOAD",
"URLs registered": "Map([<Rule '/urls' (GET, HEAD, OPTIONS) -> urls>,\n <Rule '/static/<filename>' (GET, HEAD, OPTIONS) -> static>])"
}
推荐答案
在 wsgi中包含代码似乎有些问题.py
文件。从 wsgi.py
文件中删除了应用程序创建代码,并粘贴到 app中的
文件夹并从那里导入函数。 __ init __。py
文件中
Looks like some issue with having the code in wsgi.py
file. Removed app creation code from wsgi.py
file and pasted in __init__.py
file in app
folder and imported the function from there.
现在一切都按预期运行。
Everything is working as expected now.
这篇关于在docker化应用程序后无法访问Flask端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!