Hot reload of Django development in Docker is delayed

The hot reload is delayed by 1 cycle. So for example, if I have print("Hi"), nothing changes, but then if I have print("Hello"), then print("Hi") appears on the screen. If I have a third command print("Goodbye"), then print("Hello") appears. So it is always delayed by a cycle.

How can I fix the code below so it is not delayed by 1 cycle but the update happens instantly.

Here is my dockerfile.

###########
# BUILDER #
###########

# pull official base image
FROM python:3.9.9-slim-bullseye as builder

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt


#########
# FINAL #
#########

# pull official base image
FROM python:3.9.9-slim-bullseye

# installing netcat (nc) since we are using that to listen to postgres server in entrypoint.sh
RUN apt-get update && apt-get install -y --no-install-recommends netcat && \
    apt-get install ffmpeg libsm6 libxext6  -y &&\
    apt-get autoremove -y && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# install dependencies
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --no-cache /wheels/*

# set work directory
WORKDIR /usr/src/app

# copy our django project
ADD ./backend_apps .


# run entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]

Here is the docker-compose part of it.

services:
  web:
    build: ./backend
    volumes:
      - django_static_volume:/usr/src/app/static
      - media:/usr/src/app/media
      - ./backend/backend_apps:/usr/src/app
    expose:
      - 8000
    command: python manage.py runserver 0.0.0.0:8000

Entry point script

#!/bin/sh

if [ "$DATABASE" = "postgres" ]; then
  echo "Waiting for postgres..."

  while ! nc -z $SQL_HOST $SQL_PORT; do
    sleep 0.1
  done

  echo "PostgreSQL started"
fi

python manage.py makemigrations --no-input
python manage.py migrate

exec "$@"

I disabled celery and redis but the problem still presists. I am using nginx to serve the django.

Back to Top