Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 463
Alex H Avatar asked Jan 06 '17 03:01

Alex H


People also ask

Does Docker copy overwrite existing file?

It seems that docker build won't overwrite a file it has previously copied. I have a dockerfile with several copy instructions, and files touched in earlier COPY directives don't get overwritten by later ones. After building this, $BASE/config/thatfile. yml contains the contents of file1.

Do Docker containers retain file changes?

Docker has two options for containers to store files on the host machine, so that the files are persisted even after the container stops: volumes, and bind mounts. Docker also supports containers storing files in-memory on the the host machine. Such files are not persisted.


1 Answers

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 
like image 76
Harsh Vakharia Avatar answered Oct 11 '22 10:10

Harsh Vakharia