Understanding Gunicorn and Flask on Docker/Docker-Compose Understanding Gunicorn and Flask on Docker/Docker-Compose docker docker

Understanding Gunicorn and Flask on Docker/Docker-Compose


app.run() and gunicorn are two ways to run a webserver. The first is the Flask development server, and it's useful for development but shouldn't be deployed in production. You shouldn't run both at the same time.

gunicorn should be pointed to the app object so that it can import it and use it to run the webserver itself. That's all it needs.


Instead of CMD [ "gunicorn", "-b", ":8000", "run" ]

Do CMD ["gunicorn", "app:app", "-b", "0.0.0.0:8000"]

You can see that instead of the telling the gunicorn process to run you instead tell the process where to look. The application that you want gunicorn to serve is app. You can also add more options to the gunicorn command such as reload, the number of workers, timeout, log-levels, etc...

To expand on Alex Hall's answer, you don't want to run a Flask server on production, because the ability to scale is very limited. According to the Flask docs, the mention that:

Flask’s built-in server is not suitable for production as it doesn’t scale well and by default serves only one request at a time