Copy data from containers and dockers

I have 2 docker containers in my system.

I wanted to copy data from one container to another container from my host system itself.

I know that to copy data from the container to the host, we must use

docker cp <Source path> <container Id>:path in container 

Now I'm trying to copy data directly from one container to another, is there any way to do this?

I tried to do it.

 docker cp <container-1>:/usr/local/nginx/vishnu/vishtest.txt <container-2>:/home/smadmin/vishnusource/ 

but the above command did not say that they did not support it.

I do not have to copy data to my local computer, this is my requirement.

Anyone have an idea to do this, thanks in advance?

+1
docker docker-container dockerfile docker-swarm
May 22 '17 at 18:14
source share
2 answers

For this you must use volume .

First create a volume:

 docker volume create --name shared 

Then run the containers as follows:

 docker run -v shared:/shared-folder <container-1> docker run -v shared:/shared-folder <container-2> 

Thus, /shared-folder will be synchronized between these two containers.

Read more about it here.

Hope this helps

+1
May 22 '17 at 18:56
source share

The docker cp only works between the container and the host, not between the two containers. To use this, you will need to have a copy on the host.

The ideal solution if two containers should remain in sync is to store data inside the volume:

 docker run --name container-1 -v vishnu-source:/usr/local/nginx/vishnu/ ... docker run --name container-2 -v vishnu-source:/home/smadmin/vishnusource/ ... 

You can also abuse channels and docker exec to move files between them if both containers include tar (you can change the . In the first command to vishtest.txt to copy only one file):

 docker exec container-1 tar -cC /usr/local/nginx/vishnu . \ | docker exec -i container-2 tar -xC /home/smadmin/vishnusource/ 
0
May 22 '17 at 18:43
source share



All Articles