Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 298
jonalv Avatar asked Jun 13 '16 12:06

jonalv


People also ask

How do I copy a folder into a Docker container?

To copy files or folders from host to docker container or docker container to host we can use docker cp command.

Does Dockerfile copy create directories?

Docker Dockerfiles COPY InstructionAll new files and directories are created with a UID and GID of 0.

How do I copy a folder from host to container?

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.


4 Answers

Use ADD (docs)

The ADD command can accept as a <src> parameter:

  1. A folder within the build folder (the same folder as your Dockerfile). You would then add a line in your Dockerfile like this:
ADD folder /path/inside/your/container

or

  1. A single-file archive anywhere in your host filesystem. To create an archive use the command:
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.
  • presence/absence of trailing slashes is important, see the linked docs
like image 81
ryanrain Avatar answered Oct 27 '22 07:10

ryanrain


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.

like image 26
Balasubramani M Avatar answered Oct 27 '22 08:10

Balasubramani M


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.

like image 22
Hin Fan Chan Avatar answered Oct 27 '22 08:10

Hin Fan Chan


As mentioned in your ticket:

You have COPY files/* /test/ which expands to COPY files/dir files/file1 files/file2 files/file /test/.
If you split this up into individual COPY commands (e.g. COPY files/dir /test/) you'll see that (for better or worse) COPY will copy the contents of each arg dir into the destination directory. Not the arg dir 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)

like image 19
VonC Avatar answered Oct 27 '22 07:10

VonC