ADD or COPY a folder in Docker

Docker

Docker Problem Overview


My directory Structure as follows

 Dockerfile downloads

I want to add downloads to /tmp

 ADD downloads /tmp/
 COPY down* /tmp
 ADD ./downloads /tmp

Nothings works. It copies the contents of downloads into tmp. I want to copy the downloads floder. Any idea?

 ADD . tmp/

copies Dockerfile also. i dont want to copy Dockerfile into tmp/

Docker Solutions


Solution 1 - Docker

I believe that you need:

COPY downloads/ /tmp/downloads/

That will copy the contents of the downloads directory into a directory called /tmp/downloads/ in the image.

Solution 2 - Docker

Note: The directory itself is not copied, just its contents

From the dockerfile reference about COPY and ADD, it says Note: The directory itself is not copied, just its contents., so you have to specify a dest directory explicitly.

RE: https://docs.docker.com/engine/reference/builder/#copy

e.g.

Copy the content of the src directory to /some_dir/dest_dir directory.

COPY ./src    /some_dir/dest_dir/

Solution 3 - Docker

You can use:

RUN mkdir /path/to/your/new/folder/
COPY /host/folder/* /path/to/your/new/folder/

I could not find a way to do it directly with only one COPY call though.

Solution 4 - Docker

First tar the directory you want to ADD as a single archive file:

tar -zcf download.tar.gz download

Then ADD the archive file in Dockerfile:

ADD download.tar.gz tmp/

Solution 5 - Docker

The best for me was:

COPY . /tmp/

With following .dockerignore file in root

Dockerfile
.dockerignore
# Other files you don't want to copy

This solution is good if you have many folders and files that you need in container and not so many files that you don't need. Otherwise user2807690' solution is better.

Solution 6 - Docker

If you folder does not end with a /it is considered a file, so you should something like write ADD /abc/ def/ if you want to copy a folder.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGibbsView Question on Stackoverflow
Solution 1 - DockerJames BielbyView Answer on Stackoverflow
Solution 2 - DockerROYView Answer on Stackoverflow
Solution 3 - DockerZelphir KaltstahlView Answer on Stackoverflow
Solution 4 - Dockeruser1251216View Answer on Stackoverflow
Solution 5 - Dockerromash1408View Answer on Stackoverflow
Solution 6 - Dockeruser2915097View Answer on Stackoverflow