-1

I want a conditional COPY in my Dockerfile but since this option does not exist I came across this workaround. Assuming I have following case (filenames simplified):

COPY file1.txt another_folder/file1.txt* /etc/some_folder/

will the first file1.txt be overwritten by the second another_folder/file1.txt if the second exists? So that /etc/some_folder/file1.txt will be the one of another_folder if it exists, otherwise the first.

Both filenames will be the same name but not the same content.

To make it clear upfront, I want the described behaviour.

supersize
  • 13,764
  • 18
  • 74
  • 133
  • IMO it'd be better to move the conditionals to a build script outside the dockerfile – stacksonstacks Jan 17 '19 at 22:19
  • @stacksonstacks would you think it is a downside to do it like above? This seems very elegant if possible, but teach me if I'm wrong. – supersize Jan 17 '19 at 22:22
  • It see it as a code smell. This will create a single dockerfile which can produce two distinct images. Typically, 1:1 dockerfile to image is the way to go. Pressumably, the different config will be important in the final image and should be tagged as so – stacksonstacks Jan 17 '19 at 22:34
  • Checkout my answer on the question, https://stackoverflow.com/a/54245466/4980651. It improves the workaround and it's easier to understand. – Siyu Jan 17 '19 at 22:56
  • @Siyu thanks, but I'd like to have an answer to my question first as I'd like to understand if it works and why it would be not a good solution if it does work. – supersize Jan 17 '19 at 23:03

1 Answers1

0

Just run and see for yourself. Short answer: YES it does what you want

FROM alpine
COPY f1.txt t/f1.txt*  /
RUN cat f1.txt

In f1.txt, I put "aaa". In t/f1.txt, I put "bbb".

The build shows "bbb".

If I delete t/f1.txt, the build shows "aaa"

Siyu
  • 11,187
  • 4
  • 43
  • 55