4

I'm new at the development with django and docker and I have a problem when I change a file in the project. My problem is as follows:

I make changes in the content of any file in the django project (Template, view, urls) but it does not update in my current running app. Always I want to see my changes I need to restart the server (I'm using nginx) doing docker-compose up. Is there a package or a alteration that I should install/do to make it able to accept change in running time?

This is my Dockerfile:

FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /opt/services/djangoapp/src

COPY Pipfile Pipfile.lock /opt/services/djangoapp/src/
WORKDIR /opt/services/djangoapp/src
RUN pip install pipenv && pipenv install --system

RUN pip install pipenv && pipenv install --system

RUN pip install django-livereload 

COPY . /opt/services/djangoapp/src
RUN cd hello && python manage.py collectstatic --no-input

EXPOSE 8000
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "hello", "hello.wsgi:application"]

Let me know any other information that I might provide to give a better glimpse of the problem (if it is not clear enough).

version: '3'

services:

  # database containers, one for each db
  database1:
    image: postgres:10
    volumes:
      - database1_volume:/var/lib/postgresql/data
    env_file:
      - config/db/database1_env
    networks:
      - database1_network

  # web container, with django + gunicorn
  djangoapp:
    build: .
    environment:
      - DJANGO_SETTINGS_MODULE
    volumes:
      - .:/opt/services/djangoapp/src
      - static:/opt/services/djangoapp/static
      - media:/opt/services/djangoapp/media
      - .:/code
    networks:
      - database1_network
      - nginx_network
    depends_on:
      - database1

  # reverse proxy container (nginx)
  nginx:
    image: nginx:1.13
    ports:
      - 8000:80
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - static:/opt/services/djangoapp/static
      - media:/opt/services/djangoapp/media
    networks:
      - nginx_network
    depends_on:
      - djangoapp

networks:
  database1_network:
    driver: bridge
  database2_network:
    driver: bridge
  nginx_network:
    driver: bridge

volumes:
  database1_volume:
  static:
  media:

1 Answers1

5

This is pretty simple. What happens here now

  • You have the Dockerfile and you COPY your current folder(at the time you build your image) to the container. So while you are running the container it DOES NOT sync with you host(current working folder) if you change something in the host after create the container.
  • If you want to sync your host with the container you have to mount it as volume with, either -v in single container or with volumes in docker compose.

docker run -v /host/directory:/container/directory

docker run -v ./:/opt/services/djangoapp/src

or using docker-compose if you have multiple containers

version: '3'
services:
  web-service:
    build: . # path to Dockerfile
    image: your-image
    volumes:
    - /host/directory:/container/directory
   #- ./:/opt/services/djangoapp/src
Ntwobike
  • 2,406
  • 1
  • 21
  • 27
  • I tried running "docker run -v ./:/opt/services/djangoapp/src" however I'm running it inside of the project folder and it gives the error that it requires at least one argument – Felipe Oliveira Oct 30 '18 at 21:45
  • I added the part quoted above in the docker-compose, but it still not synchronizing – Felipe Oliveira Oct 30 '18 at 21:55