superfly / flyctl

Command line tools for fly.io services
https://fly.io
Apache License 2.0
1.44k stars 243 forks source link

Flask Docker generator should not use `flask run` #3863

Open poundifdef opened 3 months ago

poundifdef commented 3 months ago

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:

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

Jaspie88 commented 3 months ago

Is there any issue using Poetry with Gunicorn? Negates the need for a requirements.txt and manages python packages for us.

Also the default docker part 2 build uses python-slim which is missing several core python features that Flask-SQAlchemy needs.