8

Need to copy multiple directories in my Dockerfile. Currently, I'm doing:

COPY dir1 /opt/dir1
COPY dir2 /opt/dir2
COPY dir3 /opt/dir3

I would prefer to consolidate those into one single statement, specifying all the sources in one go. However, this way the contents are copied, and I lose the dir1, dir2, dir3 structure:

COPY dir1 dir2 dir3 /opt/

Same in this case:

COPY dir1/ dir2/ dir3/ /opt/

Is there some way to achieve this with one line?

qqilihq
  • 10,794
  • 7
  • 48
  • 89
  • Duplicate of https://stackoverflow.com/questions/37715224/copy-multiple-directories-with-one-command and https://stackoverflow.com/questions/30256386/how-to-copy-multiple-files-in-one-layer-using-a-dockerfile – Tarun Lalwani Aug 05 '17 at 10:53
  • @TarunLalwani second link is not a copy. In docerfile copy there is a nightmare difference between file and folder – bm13kk Apr 29 '21 at 22:36

1 Answers1

3

You should consider ADD instead of COPY: see Dockerfile ADD

If <src> is a local tar archive in a recognized compression format (identity, gzip, bzip2 or xz) then it is unpacked as a directory.

That means you can wrap your docker build step into a script which would first tar -cvf dirs.tar dir1 dir2 dir3

Your Dockerfile can then ADD dirs.tar: you will find your folders in your image.

See also Dockerfile Best Practices: ADD or COPY.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Admittedly, I had hoped for something simpler, but: Interesting idea, thank you! – qqilihq Aug 05 '17 at 10:08
  • 2
    @qqilihq another alternative would be for your local folders to be in a common parent folder, ready to be `COPY`'ed, and once copied, moved by `RUN mv` commands in the Dockerfile to their final place. – VonC Aug 05 '17 at 20:29
  • @VonC this solution is better from all perspectives. – bm13kk Apr 29 '21 at 22:33