How can I get docker build to overwrite a file?

Solution 1:

I experienced this too, but after much testing discovered that I had a typo in one of my COPY directives' destination paths.

Double-check where those files are really going

-COPY nginx.conf /etc/ngnix/nginx.conf
+COPY nginx.conf /etc/nginx/nginx.conf

Solution 2:

I can't understand the behavior of your question. You must have a typo, use a different version, or you have a docker-compose volume which mounts something over something?

https://github.com/bor8/lolquadrat

git clone https://github.com/bor8/lolquadrat
cd lolquadrat/
docker build --no-cache .

Last output:

Step 1/6 : FROM busybox:latest
 ---> 19485c79a9bb
Step 2/6 : ARG BASE=/opt/hihi
 ---> Running in e4a01850931e
Removing intermediate container e4a01850931e
 ---> 193009cc4df9
Step 3/6 : COPY docker/config/file1.yml $BASE/config/thatfile.yml
 ---> bea7783ade65
Step 4/6 : RUN echo 'first thatfile:' && cat ${BASE}/config/thatfile.yml
 ---> Running in 201e86de06bc
first thatfile:
file1content
Removing intermediate container 201e86de06bc
 ---> ca7676a83a16
Step 5/6 : COPY docker/config/file2.yml $BASE/config/thatfile.yml
 ---> da5985cd90fb
Step 6/6 : RUN echo 'second thatfile:' && cat ${BASE}/config/thatfile.yml
 ---> Running in f70153917070
second thatfile:
file2content
Removing intermediate container f70153917070
 ---> 5a4b17a1e77e
Successfully built 5a4b17a1e77e

It can be clearly seen that the file thatfile.yml has the contents of the second file (file2content) after the second COPY command.

The running container shows file2content also. To test this, you can make a CMD tail -f /dev/null in the last line of your Dockerfile and then go in with docker exec -it <CONTAINER_ID_VIA_DOCKER_PS> /bin/sh. And show the content with cat thatfile.yml.

What I haven't fully tested yet is whether the same behavior exists for a folder (instead of file1.yml).