Copying data from and to Docker containers

谁说我不能喝 提交于 2019-12-24 08:57:07

问题


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 ?


回答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




回答2:


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/



回答3:


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-


来源:https://stackoverflow.com/questions/44119507/copying-data-from-and-to-docker-containers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!