copy directories in docker container excluding any one directory

+2 votes

I am having some directories/files, and I want to copy all directories/files except my_data directory from host to Docker container. The command mentioned below is working fine and is fulfilling my purpose to copy all the directories/files in the container:

docker cp ./ container_name:/my-dir

I want to copy all the directories/files from host to container except my_data dir. How can I do that?

Apr 3, 2018 in Docker by Atul
• 10,240 points
34,993 views

6 answers to this question.

0 votes

For now, there is no such way to do a --exclude while doing a docker cp. If you have a Dockerfile, you can achieve this by using .dockerignore.

Below is what i can suggest now as a quick hack/workaround -

mkdir /tmp/to_be_copied
rsync -avzh ./ /tmp/to_be_copied --exclude my_data
docker cp /tmp/to_be_copied 0132381bc8d6:/my_dir

rm -rf /tmp/to_be_copied

You need to just copy your folder structure to a different location by using rsync with the directory my_data excluded.Post this and you will be able to easily use docker cp with the newly created directory, and also do not forget to cleanup at the end so that it do not cause any issue.

answered Apr 3, 2018 by shubham
• 7,340 points
0 votes

Follow these steps:

Step 1- make a file called temp in the temp directory

Step 2- copy all the files to temp, except the one you don’t need.

Step 3- copy all these files from temp to the location you want to copy.

Step 4- delete the folder temp.

answered Aug 2, 2018 by lina
• 8,220 points
0 votes

Create a file .dockerignore in your docker build context directory with the following line in  it:

**/node_modules
answered Dec 10, 2018 by Kunal
0 votes

You can use the .dockerignore file.

The CLI interprets the .dockerignore file as a newline-separated list of patterns similar to the file globs of Unix shells. For the purposes of matching, the root of the context is considered to be both the working and the root directory. For example, the patterns /foo/bar and foo/bar both exclude a file or directory named bar in the foo subdirectory of PATH or in the root of the git repository located at URL. Neither excludes anything else.

answered Dec 10, 2018 by Lisha
0 votes

Here is an example .dockerignore file:

# comment
*/temp*
*/*/temp*
temp?
answered Dec 10, 2018 by Arpit
0 votes
One of the way could be copy and prune. Copy everything and delete whatever is not needed.
answered Dec 10, 2018 by Prateek

Related Questions In Docker

+2 votes
1 answer

How to link one running container with other running container in docker.

Hi @Yuvraj! Yes, this is possible. You can ...READ MORE

answered Dec 16, 2019 in Docker by Kalgi
• 52,350 points
2,214 views
+1 vote
1 answer

How to mount a host directory in a Docker container?

Hi@akhtar, You can use the -v option in ...READ MORE

answered Dec 23, 2020 in Docker by MD
• 95,460 points
2,418 views
+1 vote
4 answers

COPY is not working in Docker

I had the exact same issue. I ...READ MORE

answered May 6, 2019 in Docker by Emilia
22,400 views
0 votes
1 answer

Different file owner inside Docker container and in host machine Ask

Filesystems, at least in Unix- and Linux-like ...READ MORE

answered Jun 25, 2018 in Docker by Damon Salvatore
• 5,980 points
960 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,020 points
2,731 views
0 votes
1 answer

Enter in a Docker container already running with a new TTY?

Here is what you can try. For docker ...READ MORE

answered Oct 27, 2018 in Docker by shubham
• 7,340 points
2,263 views
0 votes
1 answer

How do I scale in Docker Swarm Mode W/Terraform Digital Ocean Load Balancing

The solution you could build for Digital ...READ MORE

answered Jun 19, 2018 in Docker by shubham
• 7,340 points
1,521 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP