MaDa
MaDa

Reputation: 10762

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:

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?

Upvotes: 4

Views: 607

Answers (1)

Nicolas
Nicolas

Reputation: 2231

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.

Upvotes: 2

Related Questions