0

I have hard time when developing django on my docker. When I make any changes to the code, I need to restart whole container for the changes to take effect.

I have a filesystem mounted locally and the changes are made locally. But even if I make changes directly in the container, make migrations or touch an affected or wsgi file, the changes do not take any effect.

This is the image in compose file

backend:
    container_name: 'backend'
    image: dronetag/alpha-docker/backend
    build: ./images/backend/
    command: >
        sh -c  "
              python manage.py collectstatic --no-input;
              python manage.py migrate;
              gunicorn backend.wsgi -b 0.0.0.0:80;"
    ports:
      - "10080:80"
      - "10443:443"
    volumes:
      - ./src/backend:/src
    depends_on:
      - postgres
    links:
      - redis
      - postgres

Dockerfile

FROM python:3.6
ENV PYTHONUNBUFFERED 1
ENV C_FORCE_ROOT true
RUN mkdir /src
WORKDIR /src
COPY requirements.txt .
RUN pip install -r requirements.txt
Charlestone
  • 1,248
  • 1
  • 13
  • 27
  • How'bout this: https://stackoverflow.com/q/25446055/4636715 – vahdet Mar 06 '19 at 07:39
  • 1
    Django won't auto refresh unless you're using the inbuilt dev server. It's not like PHP which is interpreted at runtime, check out this and see if you can use this setting http://docs.gunicorn.org/en/latest/settings.html#reload – Trent Mar 06 '19 at 07:44
  • https://docs.djangoproject.com/en/2.1/ref/django-admin/#runserver – bruno desthuilliers Mar 06 '19 at 08:25

1 Answers1

2

As long as you are in a development environment you can use the django development server and it will refresh everything accordingly.

Just exchange gunicorn backend.wsgi -b 0.0.0.0:80 with python manage.py runserver.

Please note, that this is not suitable for a productive environment. But there you usually do not need hot code reload.

WeGi
  • 1,908
  • 1
  • 21
  • 33
  • Is there a difference between `gunicorn backend.wsgi -b 0.0.0.0:80 --reload` and your suggestion (notice the --reload flag)? – Charlestone Mar 06 '19 at 09:50
  • 1
    No, as far as I am aware, it should not make any difference. I just chose the easy build-in solution as far as development is concerned. – WeGi Mar 06 '19 at 13:19