Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So I have a Django project I am deploying with docker-compose. There is a simple error in my profile_form.html template with line 3 highlighted.

{% include "header.html" %}
{% load i18n %}
{% load url from future %}

'future' is not a registered tag library

So I simple delete the load url line, save profile_form.html, and then try to build a new container reflecting the code change.

docker-compose build
docker-compose start

This did not resolve the issue and I get the same error. I went into the container by running

docker-compose exec -i -t <containerid> /bin/bash

and examined the profile_form.html and sure enough line 3 is still there.

Unless I am missing something completely obvious this tells me that my understanding of docker-compose build is incorrect. As I thought docker-compose build would be able to determine "yes there is a code change in the 'web' directory" and then rebuild the container.

like image 307
david Avatar asked Jan 03 '17 19:01

david


People also ask

What is the difference between docker build and docker compose?

The key difference between the Dockerfile and docker-compose is that the Dockerfile describes how to build Docker images, while docker-compose is used to run Docker containers.

What does docker compose up -- build do?

Description. Builds, (re)creates, starts, and attaches to containers for a service. Unless they are already running, this command also starts any linked services. The docker compose up command aggregates the output of each container (like docker compose logs --follow does).

Does docker compose build a new image?

Step 4: Build and run your app with Compose From your project directory, start up your application by running docker compose up . Compose pulls a Redis image, builds an image for your code, and starts the services you defined. In this case, the code is statically copied into the image at build time.


1 Answers

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.

like image 166
n2o Avatar answered Oct 19 '22 19:10

n2o