Docker named volume not updating

Shulhi Sapli picture Shulhi Sapli · Jul 18, 2016 · Viewed 12.6k times · Source

I am confused on how the named data-volume (not data container) should be used.

I have a named data volume app_src that is mounted to /usr/src/app using docker compose file. However, after making changes to my source code (locally), building the image doesn't update the volume.

I am building the image like so,

docker-compose -f development.yml build and running it docker-compose -f development.yml up -d.

To confirm that the volume doesn't change, I attached into the running container and true enough, the source code isn't updated.

Here is my docker compose file development.yml and Dockerfile for my web service. version: '2'

services:
  web:
    restart: always
    build: ./web
    expose:
      - "8000"
    volumes:
      - app_src:/usr/src/app
    links:
      - postgres:postgres
    env_file: development.env
    command: ./start_web.sh

volumes:
   app_src: {}


FROM python:3.4.4

WORKDIR /usr/src/app
RUN rm -rf /usr/src/app/*
COPY . /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt

I could make it work by mounting the host like so,

volumes:
    - ./web/src:/usr/src/app

I'm on Ubuntu 16.04 running docker 1.11.2. Is my understanding wrong? I did take a look at the documentation but I could find anything that explained the volume really well.

Answer

BMitch picture BMitch · Jul 18, 2016

It looks like you're trying to use docker-compose with a named volume mount and a Dockerfile to modify the contents of that location. This won't work because the Dockerfile is creating an image. The docker-compose is defining the running container that runs on top of that image. You won't be able to modify the volume within the image creation since that volume is only mounted after the image is created an you run the container.

If you want to update your named volume, consider a side container:

docker run -v app_src:/target -v `pwd`/web/src:/source --rm \
  busybox /bin/sh -c "tar -cC /source . | tar -xC /target"

You can run that container on demand to update your named volume. You can also replace the tar with something like a git clone to pull from a source repository, or even an rsync (which you'd need installed on your image) if you are making small changes to a large repository.

You can also workaround this by emptying your named volume (either rm -rf /vol/dir or removing the volume and creating a new one) and then restarting the container. On the startup of the container, if an empty named volume is included, the default is to copy the image contents at that location into the volume.