0

I am working on a Flask project which I used to run locally before in debug mode with the simple flask run command.

Now, I have to switch to a Docker environment. I am new to Docker. I have installed Docker Desktop for my Windows machine.

The commands I use to run my project locally are: docker-compose build and docker-compose up. As you may expect, the build takes a long time.

Now, when I make changes, the project doesn't reflect my recent changes without a new build. What should I do to have a live demo for debugging like local Flask deployment gave me?

This is my DOCKERFILE:

FROM python:3.12

WORKDIR /app

RUN apt-get -y update && apt-get install -y curl gnupg
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get -y update && ACCEPT_EULA=Y apt-get install -y msodbcsql18

RUN apt-get install -y \
    wkhtmltopdf \
    xvfb \
    libfontconfig \
    libxrender1 \
    libjpeg62-turbo

COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "run.py"]

I read that 'Bind Mounts' could help in this in some way but the documentation doesn't really shed much light on my use case.

My run.py file:

import os
from app import app, db

with app.app_context():
    db.create_all() # Create defined SQL DB Tables
    
if __name__ == '__main__':
    if os.name == 'nt':
        app.run(host='0.0.0.0', port=5000, debug=True, ssl_context=('xyz.crt', 'xyz.key'))
    else:
        import subprocess

        cmd = ['gunicorn', '-w', '1', '--threads', '10', '-b', '0.0.0.0:5000', '--certfile', 'xyz.crt', '--keyfile', 'xyz.key', '--timeout', '300', 'app:app']
        subprocess.call(cmd)
7
  • You haven't said what is in your run.py. Are you familiar with this: flask.palletsprojects.com/en/stable/quickstart/#debug-mode Commented Mar 11 at 9:16
  • @RichardHuxton I have added run.py and am familiar with debug mode Commented Mar 11 at 9:34
  • You've told it to run the gunicorn branch inrun.py. Docker doesn't run directly on Windows - the desktop app installs a linux VM and redirects commands to that. Commented Mar 11 at 9:52
  • Yes. But it should be able to run on gunicorn with the new code changes right? Commented Mar 11 at 11:08
  • You haven't told gunicorn to reload on code changes though, have you? docs.gunicorn.org/en/latest/settings.html#reload. You need to do that (and make sure that your source-code is mounted into the container, obviously). Commented Mar 11 at 11:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.