I set my app.py
to debug mode:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html', title='Title Here')
# ...
# more routes here
# ...
if __name__ == '__main__':
app.run(debug=True)
The Python library called geopandas
is messing my local machine up, that's why I decided to dockerise my app, and it works perfectly! (Except the flask debug mode)
My Dockerfile
looks like this. I tried the debugpy
library with no success:
FROM python:3.9-slim as base
# Setup env
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONFAULTHANDLER 1
FROM base AS python-deps
# Install pipenv and compilation dependencies
RUN pip install pipenv
RUN apt-get update && apt-get install -y --no-install-recommends gcc
# Install python dependencies in /.venv
COPY Pipfile .
COPY Pipfile.lock .
RUN PIPENV_VENV_IN_PROJECT=1 pipenv install geopandas
RUN PIPENV_VENV_IN_PROJECT=1 pipenv install debugpy
RUN PIPENV_VENV_IN_PROJECT=1 pipenv install --deploy
CMD pipenv debugpy
FROM base AS runtime
# Copy virtual env from python-deps stage
COPY --from=python-deps /.venv /.venv
ENV PATH="/.venv/bin:$PATH"
WORKDIR /home
# Install application into container
COPY . .
Then I build the image with this:
docker build -t <IMAGE-NAME:HERE> .
Then I initialise the docker container with this script:
docker run --rm -ti --mount type=bind,source=/"$(pwd)",target=/home -p 5000:5000 <IMAGE-NAME:HERE> flask run --port 5000 --host 0.0.0.0 --debugger
And everything is working but the debug mode:
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://172.17.0.2:5000
Any ideas?
CodePudding user response:
First, to answer your question:
flask --debug run --port 5000 --host 0.0.0.0
Debug mode enables --debugger
and also --reload
.
I have had great success using Docker as the runtime for my Flask application during development. It makes development quite a bit easier, especially when paired with Docker compose once your app grows a bit and you need other services like a database or a redis instance.
I'm a little confused as to why your CMD
is before your runtime stage, and why you're using pipenv debugpy
instead of pipenv run debugpy
. (That said, I have no experience with debugpy, so maybe this is actually correct.) Anyway, I'd encourage you to investigate ENTRYPOINT
to see if it might simplify invocation.
As an aside, you should consider creating a docker-compose.yml
file and starting your service with docker compose up
. The docker CLI incantation above is already starting to become unwieldy. This is untested, but should get you started:
version: "3.9"
services:
web:
# Fix slow shutdown of api container
# See: https://stackoverflow.com/a/62854251/1749551
init: true
environment:
FLASK_ENV: development # Implies --debug
PORT: 5000
ports:
- 5000:5000
restart: always
entrypoint: "flask run --host=0.0.0.0"
# To enable pdb within Docker:
# https://hackernoon.com/debugging-using-pdb-in-dockerized-environment-i21n2863
stdin_open: true
tty: true
# Bind the project directory into the container so changes are reflected by Flask
volumes:
- .:/home
And lastly, be conscious of what COPY . .
will pick up and add to your image.