Docker COPY not updating files when rebuilding container

I have a docker-compose-staging.yml file which I am using to define a PHP application. I have defined a data volume container (app) in which my application code lives, and is shared with other containers using volumes_from.

docker-compose-staging.yml:

version: '2'
services:
    nginx:
        build:
            context: ./
            dockerfile: docker/staging/nginx/Dockerfile
        ports:
            - 80:80
        links:
            - php
        volumes_from:
            - app

    php:
        build:
            context: ./
            dockerfile: docker/staging/php/Dockerfile
        expose:
            - 9000
        volumes_from:
            - app

    app:
        build:
            context: ./
            dockerfile: docker/staging/app/Dockerfile
        volumes:
            - /var/www/html
        entrypoint: /bin/bash

This particular docker-compose-staging.yml is used to deploy the application to a cloud provider (DigitalOcean), and the Dockerfile for the app container has COPY commands which copy over folders from the local directory to the volume defined in the config.

docker/staging/app/Dockerfile:

FROM php:7.1-fpm
COPY ./public /var/www/html/public
COPY ./code /var/www/html/code

This works when I first build and deploy the application. The code in my public and code directories are present and correct on the remote server. I deploy using the following command:

docker-compose -f docker-compose-staging.yml up -d

However, next I try adding a file to my local public directory, then run the following command to rebuild the updated code:

docker-compose -f docker-compose-staging.yml build app

The output from this rebuild suggests that the COPY commands were successful:

Building app
Step 1 : FROM php:7.1-fpm
 ---> 6ed35665f88f
Step 2 : COPY ./public /var/www/html/public
 ---> 4df40d48e6a5
Removing intermediate container 7c0fbbb7f8b6
Step 3 : COPY ./code /var/www/html/code
 ---> 643d8745a479
Removing intermediate container cfb4f1a4f208
Successfully built 643d8745a479

I then deploy using:

docker-compose -f docker-compose-staging.yml up -d

With the following output:

Recreating docker_app_1
Recreating docker_php_1
Recreating docker_nginx_1

However when I log into the remote containers, the file changes are not present.

I'm relatively new to Docker so I'm not sure if I've misunderstood any part of this process! Any guidance would be appreciated.

Answers


You are trying to update an existing volume with the contents from a new image, that does not work.

https://docs.docker.com/engine/tutorials/dockervolumes/#/data-volumes

States:

Changes to a data volume will not be included when you update an image.


This is because of cache.

Run,

docker-compose build --no-cache

This will rebuild images without using any cache.

And then,

docker-compose -f docker-compose-staging.yml up -d

I had similar issue if not same while working on dotnet core application.

What I was trying to do was rebuild my application and get it update my docker image so that I can see my changes reflected in the containerized copy.

So I got going by removing the underlying image generated by docker-compose up using the command to get my changes reflected:

docker rmi *[imageId]*

I believe there should be support for this in docker-compose but this was enough for my need at the moment.


I am not sure it is caching, because (a) it is usually noted in the build output, whether cache was used or not and (b) 'build' should sense the changed content in your directory and nullify the cache.

I would try to bring up the container on the same machine used to build it to see if that is updated or not. if it is, the changed image is not propagated. I do not see any version used in your files (build -t XXXX:0.1 or build -t XXXX:latest) so it might be that your staging machine uses a stale image. Or, are you pushing the new image so the staging server will pull it from somewhere?


None of the above solutions worked for me, but what did finally work was the following steps:

  1. Copy/Move file outside of docker app folder
  2. Delete File you want to update
  3. Rebuild the docker img without updated file
  4. Move copied file back into docker app folder
  5. Rebuild again the docker image

    Now the image will contain the updates to the file.


Need Your Help

How do I change a glyphicon upon clicking it in Bootstrap 3.2?

javascript jquery html css twitter-bootstrap

I want to have an arrow pointing to the right to allow the user to expand the sidebar, and then change that glyphicon to point to the left. That way, it points to the left so that they understand h...

How to scroll to top of page with JavaScript/jQuery?

javascript jquery html scroll

Is there a way to control browser scrolling with JavaScript/jQuery?