Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile COPY takes a stale copy of the file?

I have the following sequence of commands in my Dockerfile; note the replacement in place by sed:

RUN sed -i "s/replace this/with that/g" ./dir/file.yaml
COPY ./dir/file.yaml /usr/src/app/node_modules/serve-swagger-editor/node_modules/swagger-editor/spec-files/

This runs ALMOST good, that is the file.yaml gets copied to the target location, but:

  • ./dir/file.yaml contains changes done by sed,
  • /usr/src/.../spec-files/file.yaml does not.

It turns out that this workaround fixes the COPY problem:

RUN sed -i "s/replace this/with that/g" ./dir/file.yaml
RUN cp ./dir/file.yaml /usr/src/app/node_modules/serve-swagger-editor/node_modules/swagger-editor/spec-files/

Would anyone explain this bizarre behaviour?

like image 204
MaDa Avatar asked Sep 17 '25 18:09

MaDa


1 Answers

RUN executes command within the container, so sed is applied to the file ./dir/file.yaml that is in it. You probably have a/the same file in your WORKDIR/dir/file.yaml (WORKDIR), that explains why the second option works.

The COPY in your first version overrides the /usr/src/app/node_modules/serve-swagger-editor/node_modules/swagger-editor/spec-files/ file with the ./dir/file.yaml that is in your host build directory. This one has not been affected by the sed command before, as it is outside the container.

So what you can do is COPY the file first inside the container, and then use your RUN command to modify it. Or modify it before running docker build.

like image 173
Nicolas Avatar answered Sep 20 '25 12:09

Nicolas