How to Run a Flask API Application Container With Gunicorn and Nginx on Podman

A brief guide to creating in Flask

Photo by Bluewater Sweden on Unsplash

We have already established how we would want our application to be structured as per this story. I have improved the code on how the application consumes database credentials. Flask uses a built-in dev server when running locally, which is not recommended for production.

You have to configure a WSGI server for the application to serve production traffic. There are several WSGI servers available, but I have chosen Gunicorn due to its maturity. However, Gunicorn is not primarily made to serve static content and graphics. Instead of labouring Gunicorn with those tasks, we should let the HTTP boss in the industry, Nginx, handle all the traffic requests and load balancing as a reverse proxy.

We will therefore have two containers running side by side in a podman pod and traffic will get to the application container via Nginx.

Below, the Dockerfile will create the application image based on ubi/python38 image and when run, the gunicorn server will be launched as defined in the file.

The Nginx server will have to be configured to send traffic to the application container. In the Nginx config file, include configs to tell Nginx to send all traffic received via/route to gunicorn server at port 8000. These configs will then be added to the Nginx image as per the Dockerfile.

We have the code base and Dockerfiles for both govt_structure application and Nginx. We will create a pod exposed on port 8181 and deploy the application containers to the pod. Run the following script to perform these tasks:

Finally, our application is up and running and can be tested at http://localhost:8181/sayhello (/sayhello just returns a message).

Thank you for reading. You can find the full code in my GitHub repo in govt branch.

Keep an eye at on the following:

  • Pipfile content
  • Redhat registry to pull ubi8/python-38 base image
  • Database credentials are not committed in git
  • use podman logs <containername> to check logs in case the container crashes

How to Run a Flask API Application Container With Gunicorn and Nginx on Podman was originally published in Better Programming on Medium, where people are continuing the conversation by highlighting and responding to this story.

(Visited 1 times, 1 visits today)