'Running containerized a flask app with gunicorn and nginx
I've been working on my VPS lately in a goal which is to have nginx handle all traffic in a reverse proxy to containers which contain my projects, which most importantly are flask and ran using gunicorn wsgi I've tried multiple attempts using Dockerfile, docker-compose, etc... The closest thing I got was using this post.
And those are the steps I followed to install docker.
Where even then it did not as the example said and wasn't able to see the message displayed by flask but only saw the ngnx installation successful message. Even if it worked, I was suggested by people on the python server to use gunicorn (which makes sense) and I wasn't able to switch it out, and most importantly to have nginx OUTSIDE the container, where nginx would just handle the traffic into the other containers without being in one, which I also couldn't pull off. I come from a "heroku" background so It's still a little complicated for me to pull this off, I've watched countless videos and explanations online but I couldn't find anyone that had a good tutorial on my specific case of "gunicorn, nginx, flask, docker". This is a lot to ask so I'm not really expecting a detailed explanation on how to do this (even though I would REALLY appreciate that), nor a step by step guide of course, but pointing me in the right direction would, like is there something I'm doing that could be done in a more efficient or just better manner? if not then is there anything you suggest I look into? articles, docs links, anything, hit me with it!
I would show the files I ended up with but they were made for nginx to be inside a container which isn't what I want my end result to be and they didn't work anyways so I'm hoping to start from scratch
Those are the steps I was suggested to figure out (I'm still working on them because I'm restarting and taking this slowly, unlike last time):
- Figure out the command to run your app using gunicorn
- Add that command to your dockerfile, and make sure you can build the container
- Expose the container's ports and make sure you can make your requests correctly
- Use NGINX to forward those requests
Solution 1:[1]
I don't know if this will help you, but for flask with gunicorn inside a container, I suggest you to follow this tutorial : https://mark.douthwaite.io/getting-production-ready-a-minimal-flask-app/
with its source code : https://github.com/markdouthwaite/minimal-flask-api
Personally, it helped me a lot.
Maybe next you want to build the image :
docker build -t image_name .
where the dockerfile is located and then and run the container with something like :
docker run -p 8080:8080 image_name
And then use Nginx to set a proxy path on localhost:8080/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DharmanBot |
