Flask Docker generator should not use `python3 -m flask run`

Hi! The generated Dockerfile for flask should have a different CMD.

Right now it runs python3 -m flask run. This is incorrect. Flask warns about this in their docs:

Do not use the development server when deploying to production. It is intended for use only during local development. It is not designed to be particularly secure, stable, or efficient.

When you look at the fly logs, it also emits a warning:

This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.

I’d propose the following changes:

diff --git a/Dockerfile b/Dockerfile
index 7f8924f..ce75327 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -10,9 +10,10 @@ WORKDIR /code
 
 COPY requirements.txt requirements.txt
 RUN pip3 install -r requirements.txt
+RUN pip3 install gunicorn
 
 COPY . .
 
 EXPOSE 8080
 
-CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0", "--port=8080"]
+CMD ["gunicorn", "--bind", "0.0.0.0:8080", "app:app"]

This will do the following:

  1. Installsrequirements.txt
  2. If gunicorn was installed already from the requirements file, pip3 install gunicorn is a noop. Otherwise it will install the latest version.
  3. Updates the default Dockerfile to use gunicorn, rather than the flask debug server

Alternatively, you could look for gunicorn in requirements.txt and add if it’s missing.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.