Changes on template files inside volume not showing on Flask frontend

Question:

I am using a docker-compose Flask implementation with the following configuration

docker-compose:

version: '3'
services:
  dashboard:
    build:
      context: dashboard/
      args:
        APP_PORT: "8080"
    container_name: dashboard
    ports:
      - "8080:8080"
    restart: unless-stopped
    environment:
      APP_ENV: "prod"
      APP_DEBUG: "False"
      APP_PORT: "8080"
    volumes:
      - ./dashboard/:/usr/src/app

dashboard/Dockerfile:

FROM python:3.7-slim-bullseye

ENV PYTHONUNBUFFERED True

ARG APP_PORT

ENV APP_HOME /usr/src/app
WORKDIR $APP_HOME
COPY requirements.txt ./requirements.txt

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

CMD exec gunicorn --bind :$APP_PORT --workers 1 --threads 8 --timeout 0 main:app

dashboard/main.py:

import os

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

If I apply any change to the index.html file in my host system using VSCode, these changes won’t apply when I refresh the page. However, I have tried getting into the container with docker exec -it dashboard bash and cat /usr/src/app/templates/index.html and they are reflected inside the container, since the volume is shared between the host and the container.

If I stop the container and run it again the changes are applied, but as I am working on frontend doing that all the time is pretty annoying.

Why the changes won’t show on the browser but they are replicated on the container?

Asked By: Luiscri

||

Answers:

You should use: TEMPLATES_AUTO_RELOAD=True

From https://flask.palletsprojects.com/en/2.0.x/config/

It appears that the templates are preloaded and won’t update until you enable this feature.

Answered By: SCcagg5

In Dockerfile you need to make a necessary modification. Right now you’re coping just requirements.txt.

WORKDIR $APP_HOME
COPY . . #you may just add necessary folders too instead coping everything 
RUN pip install --no-cache-dir -r requirements.txt

Let me know if that doesn’t work for you!

Answered By: Santosh Gopane