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.
But when I dockerize the app and running it using docker-compose
, I'm not able to access the urls for the app.
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)
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.
Where am I going wrong?
Here's the wsgi.py file:
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
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>])"
}
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.