ADD or COPY a folder in Docker

2019-04-20 04:45发布

问题:

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/

回答1:

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.



回答2:

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/



回答3:

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/


回答4:

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.



回答5:

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.



回答6:

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.



标签: docker