How to copy folders to docker image from Dockerfile?
I tried the following command in my Dockerfile:
COPY * / and got mighty surprised at the result. Seems the naive docker code traverses the directories from the glob and then dumps the each file in the target directory while respectfully ignoring my directory structure.
At least that is how I understand this ticket and it certainly corresponds to the result I got.
I guess the only reason this behavior can still exist must be that there is some other way this should be done. But it is not so easy for a bear of very little brain to understand how, does anyone know?
2 Solutions collect form web for “How to copy folders to docker image from Dockerfile?”
As mentioned in your ticket:
COPY files/* /test/which expands to
COPY files/dir files/file1 files/file2 files/file /test/.
If you split this up into individual
COPY files/dir /test/) you’ll see that (for better or worse)
COPYwill copy the contents of each arg
dirinto the destination directory. Not the arg
diritself, but the contents.
I’m not thrilled with that fact that COPY doesn’t preserve the top-level dir but its been that way for a while now.
so in the name of preserving a backward compatibility, it is not possible to
ADD a directory structure.
The only workaround would be a series of
RUN mkdir -p /x/y/z to build the target directory structure, followed by a series of
docker COPY (one for each folder to fill).
ADD instead of
COPY. Suppose you want to copy everything in directory src from host to directory dst from container:
ADD src dst
Note: b will be automatically created in container.