1

I have 2 docker containers running on my system.

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

i know that to copy data from container to host we have to use

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

Now i am trying to copy the data directly from one container to another, is there any way to do that ??

i tried doing this.

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

but the above command failed saying its not supported.

i should not copy the data to my local machine, thats my requirement.

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

Vishnu Ranganathan
  • 1,277
  • 21
  • 45
  • have you tried docker volumes? – Robert May 22 '17 at 18:16
  • as far i know docker volume can be the common place, which can be reffered by containers using mount source and mount target.i really don't have any idea about docker volume to help me on this scenario. @Robert please give me the suggestion how can i use it here ? – Vishnu Ranganathan May 22 '17 at 18:27
  • 2
    You can use netcat if your containers are in the same network – user2915097 May 22 '17 at 18:32

4 Answers4

1

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

The ideal solution if the two containers should remain in sync is to store the data inside a 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 pipes and docker exec to move files between the two if both containers include tar (you can change the . in the first command to vishtest.txt to only copy that one file):

docker exec container-1 tar -cC /usr/local/nginx/vishnu . \
| docker exec -i container-2 tar -xC /home/smadmin/vishnusource/
BMitch
  • 231,797
  • 42
  • 475
  • 450
1

You should use volume for that.

First, create a volume:

docker volume create --name shared

Then, run containers like this:

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

This way, /shared-folder will be synced between these two containers.

Read more about it here

Hope it helps

Jahongir Rahmonov
  • 13,083
  • 10
  • 47
  • 91
  • i have created the volume . when i run "docker run -v shared:/shared-folder " it says "Unable to find image 'a75df1c6cfce:latest' locally" it tries to pull the image. i want to do this on a running container what should i do ?? – Vishnu Ranganathan May 22 '17 at 19:20
0

This is what I use to copy folders between dockers using a single command:

docker exec container-1 tar -c -f- folder/path/from/root | \
docker exec -i container-2 tar -x -f-
Sahil Ahuja
  • 2,225
  • 1
  • 16
  • 12
0

Using pipes is a simple solution too.

As root or within a script:

docker cp container-1:/data-path/. - | docker cp - container-2:/data-path

As sudoer:

sudo docker cp container-1:/data-path/. - | (sudo docker cp - container-2:/data-path)
4b0
  • 21,981
  • 30
  • 95
  • 142