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?
To copy files or folders from host to docker container or docker container to host we can use docker cp command.
Docker Dockerfiles COPY InstructionAll new files and directories are created with a UID and GID of 0.
Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container. The second parameter of the docker copy command is the location to save the file on the host.
ADD
(docs)The ADD
command can accept as a <src>
parameter:
ADD folder /path/inside/your/container
or
tar -cvzf newArchive.tar.gz /path/to/your/folder
You would then add a line to your Dockerfile like this:
ADD /path/to/archive/newArchive.tar.gz /path/inside/your/container
Notes:
ADD
will automatically extract your archive.Like @Vonc said, there is no possibility to add a command like as of now. The only workaround is to mention the folder, to create it and add contents to it.
# add contents to folder
ADD src $HOME/src
Would create a folder called src in your directory and add contents of your folder src into this.
use ADD
instead of COPY
. Suppose you want to copy everything in directory src from host to directory dst from container:
ADD src dst
Note: directory dst will be automatically created in container.
As mentioned in your ticket:
You have
COPY files/* /test/
which expands toCOPY files/dir files/file1 files/file2 files/file /test/
.
If you split this up into individualCOPY
commands (e.g.COPY files/dir /test/
) you'll see that (for better or worse)COPY
will copy the contents of each argdir
into the destination directory. Not the argdir
itself, 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 COPY
/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 ADD
(one for each folder to fill).
(ADD
, not COPY
, as per comments)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With