pythondockerflaskproductionwaitress

serving flask app with waitress and docker


I am serving a flask app with docker but the docker logs command shows that the app is running on a development server. I want to serve this app with waitress.

The project is structured like this below. A docker-compose.yml file to build the image, expose the port and run the manage.py file

docker-compose.yml

  web:
    build: .
    image: web
    container_name: web
    ports:
      - 8080:5000
    command: python manage.py run -h 0.0.0.0

manage.py file imports the create_app and provides it into FLaskGroup

from flask.cli import FlaskGroup
from project.server import create_app

app = create_app()
cli = FlaskGroup(create_app=create_app)

if __name__ == "__main__":
    cli()

project/server/__init__.py file imports the main_blueprint and registers it.

from project.server.main.views import main_blueprint
from flask import Flask
import os

def create_app(script_info=None):

    app = Flask(
        __name__,
        template_folder="../client/templates",
        static_folder="../client/static",
    )

    app_settings = os.getenv("APP_SETTINGS")
    app.config.from_object(app_settings)

    app.register_blueprint(main_blueprint)

    app.shell_context_processor({"app": app})

    return app

project/server/main/views.py

from flask import render_template, Blueprint, jsonify, request

main_blueprint = Blueprint("main", __name__,)

@main_blueprint.route("/", methods=["GET"])
def home():
    return render_template("pages/home.html")


@main_blueprint.route("/test", methods=["GET"])
def parse():
    return jsonify({"result": "test"}), 202

How can I modify the existing code to serve the flask app with waitress? Thank you.


Solution

  • I got it running by changing the docker-compose.yml file:

    command

    python manage.py run -h 0.0.0.0 to waitress-serve --call "project.server:create_app"

    port

    8080:5000 to 8080:8080

    docker-compose.yml file looks like below now:

      web:
        build: .
        image: web
        container_name: web
        ports:
          - 8080:8080
        command: waitress-serve --call "project.server:create_app"