Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker COPY and keep directory

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?

like image 824
qqilihq Avatar asked Aug 05 '17 09:08

qqilihq


People also ask

Does Docker copy overwrite directory?

When copying a single file to an existing LOCALPATH, the docker cp command will either overwrite the contents of LOCALPATH if it is a file or place it into LOCALPATH if it is a directory, overwriting an existing file of the same name if one exists. For example, this command: $ docker cp sharp_ptolemy:/tmp/foo/myfile.

Does Docker copy make directory?

Docker ADD CommandThe command copies files/directories to a file system of the specified container. It includes the source you want to copy ( <src> ) followed by the destination where you want to store it ( <dest> ). If the source is a directory, ADD copies everything inside of it (including file system metadata).

Is Docker a recursive copy?

The cp command behaves like the Unix cp -a command in that directories are copied recursively with permissions preserved if possible. Ownership is set to the user and primary group at the destination. For example, files copied to a container are created with UID:GID of the root user.


1 Answers

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.

like image 147
VonC Avatar answered Oct 04 '22 12:10

VonC