Boris Burkov
Boris Burkov

Reputation: 14436

docker-compose build doesn't re-create some slices of my container, resulting in partially obsolete code

I have a Django-Angular web project (called workflows), which I build with Jenkins CI. Jenkins monitors the repository and upon new commit re-builds my project's containers with the following 2 scripts that perform cleanup and re-build:

#!/bin/sh

docker rm -v $(docker ps -a -q -f status=exited) 2>/dev/null
docker rmi $(docker images -q -f 'dangling=true') 2>/dev/null

exit 0

And the re-build script is:

!/bin/sh

DOCKER_COMPOSE=/usr/local/bin/docker-compose

[ -x $DOCKER_COMPOSE ] || (
  echo "docker-compose not found, install it from https://docs.docker.com/compose/install/"
  exit 1
)

$DOCKER_COMPOSE build --pull || exit 1
$DOCKER_COMPOSE up -d || exit 2
$DOCKER_COMPOSE ps

I build javascript frontend of my django site right in the Docker image within Dockerfile instruction like this:

FROM debian:jessie

ADD . /srv/workflows

# Install dependencies

RUN apt-get update && apt-get install -y git \
    curl \
    uwsgi \
    uwsgi-plugin-python \
    python-django \
    python-psycopg2 \
    python-django-celery \
    python-django-jsonfield

RUN easy_install pip

RUN pip install djangorestframework \
    python-social-auth

RUN pip install git+https://github.com/st4lk/django-rest-social-auth.git

# Set up node.js and build frontend

RUN curl --silent --location https://deb.nodesource.com/setup_5.x | bash -
RUN apt-get install -y nodejs

RUN npm install -g webpack gulp-cli bower

WORKDIR /srv/workflows/workflows/frontend
RUN npm install
RUN bower install --allow-root
RUN mkdir -p ../static
RUN mkdir -p ../../static
RUN gulp dist

# Get everything ready and run

WORKDIR /srv/workflows
RUN python manage.py validate
RUN python manage.py collectstatic --clear --noinput

CMD /bin/bash -c "python manage.py makemigrations --noinput; python manage.py migrate --noinput; /usr/bin/uwsgi --emperor /etc/uwsgi/apps-enabled/bostongene.ini"

But for some reason when docker-compose rebuilds this image, results of django collectatic command (python manage.py collectstatic --clear --noinput) still contain the very first ancient version of files that were contained in the very first image, when I first created the container from it. Why?

Related section of docker-compose.yml:

workflows-django:
    restart: always
    build: ./workflows
    links:
        - "workflows-db: workflows-db"
        - "workflows-rabbitmq: workflows-rabbitmq"
    volumes:
        - /srv/workflows/static/
    volumes_from:
        - workflows-db
    ports:
        - "8000:8000"
    command: /bin/bash -c "python manage.py makemigrations --noinput; python manage.py migrate --noinput; /usr/bin/uwsgi --emperor /etc/uwsgi/apps-enabled/bostongene.ini"

Upvotes: 0

Views: 347

Answers (1)

dnephin
dnephin

Reputation: 28050

(as we discussed in the comments)

When using a volume Compose preserves volumes on container recreate.

Even if you rebuild the image, if it defines the same volume, it will continue to be used. In general I would recommend not putting source code (or in this case assets) into volumes. Volumes are for runtime state, but the assets and source code should change with the image, not preserved between runs.

If you want to continue to use volumes you can run docker-compose rm to remove the old container, which will remove the reference to the volume.

Upvotes: 1

Related Questions