[Fixed]-Why does docker-compose build not reflect my django code changes?

37👍

You do not need to rebuild a docker image, which is used for development, on each change of your code. This would take too much time. But since you are developing django and I assume, that you are using the dev-server shipped with django (python manage.py runserver), you can directly send your new code changes to your container and let the dev-server hot load the code as you are used to when you are developing a django application on your local machine. You need to map a volume from your host to your container, which receives the latest code and uses it to update your application.

Since you did not provide any code samples, I can only guess what you are doing. Take a look at the example directly provided by the docker developers: https://docs.docker.com/compose/django/

Their Dockerfile:

FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

ADD . /code/ initially copys your local code to the container. This is the initial state of your application when you build the image for the first time. But we do not want to build the image on each code change, since this takes some minutes. You are using docker-compose and you can map volumes to your container for hot code reloading as it can be seen in the tutorial by the docker devs:

version: '2'
services:
  db:
    image: postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code  # <--  THIS line enables hot code reloading!
    ports:
      - "8000:8000"
    depends_on:
      - db

I marked the important line for code reloading. This line ensures, that each change on your local machine is also reflected inside the container. The dev-server runserver recognizes the local code change and restarts the webserver, which takes only few seconds.

This is the way to go with a django application inside a docker container.

Why are there no changes after running docker-compose start?

docker-compose start starts the old containers of your old image with the wrong line in your code. You need to create new containers with the new image. For this case is the command docker-compose up. From the docs of docker-compose start:

Usage: start [SERVICE…]
Starts existing containers for a service.

To be sure, you can remove your old containers with docker-compose rm. This removes only your containers, not the image.

👤n2o

Leave a comment