简体   繁体   English

如何在docker容器和主机之间共享数据?

[英]How to share data between the docker container and the host?

I tried to share data between the docker container and the host, for example by adding the parameter -v /Users/name/Desktop/Tutorials:/cntk/Tutorials to the docker run command, but I noticed that it also deletes all the files on the docker contained in /cntk/Tutorials.我尝试在 docker 容器和主机之间共享数据,例如通过将参数 -v /Users/name/Desktop/Tutorials:/cntk/Tutorials 添加到 docker run 命令,但我注意到它还会删除所有文件在 /cntk/Tutorials 中包含的 docker 上。

My question is how to make the same link, but having instead all the files in /cntk/Tutorials copied to the host (at /Users/name/Desktop/Tutorials)我的问题是如何创建相同的链接,而是将 /cntk/Tutorials 中的所有文件复制到主机(位于 /Users/name/Desktop/Tutorials)

Thank you谢谢

Unfortunately that it is not possible, take a look here .不幸的是,这是不可能的, 请看这里 That is because this is how mounting works in Linux.那是因为这就是 Linux 中挂载的工作方式。

It is not correct to say that the files were deleted.说文件被删除是不正确的。 They are still present in the underlying image, but the act of mounting another directory at the same path has obscured them.它们仍然存在于底层映像中,但是在同一路径上安装另一个目录的行为已经掩盖了它们。 They exist, but are not accessible in this condition.它们存在,但在这种情况下无法访问。

One way you can accomplish this is by mounting a volume into your container at a different path , and then copying the container's files to that path.实现此目的的一种方法是将卷安装到容器中的不同路径,然后将容器的文件复制到该路径。 Something like this.像这样的东西。

Mount a host volume using a different path than the one the container already has for the files you are interested in.使用与容器已有的用于您感兴趣的文件的路径不同的路径挂载主机卷。

docker run -v /Users/name/Desktop/Tutorials:/cntk/Tutorials2 [...]

Now, execute a command that will copy the files already in the docker image, into the mounted volume from the outside host.现在,执行一个命令,将 docker 镜像中已有的文件从外部主机复制到挂载的卷中。

docker exec <container-id> cp -r /cntk/Tutorials /cntk/Tutorials2

The docker cp command allows you to copy files/folders on demand between host and the container: docker cp命令允许您在主机和容器之间按需复制文件/文件夹:

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

docker cp ContainerName:/home/data.txt . <== copy from container to host

docker cp ./test.txt ContainerName:/test.txt <== copy from host to container
docker cp ContainerName:/test.txt ./test2.txt <== copy from container to host

For details run docker cp --help有关详细信息,请运行docker cp --help

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 docker 容器和主机之间共享数据 - how to share data between docker container and host 如何在主机操作系统和Docker容器之间共享文件夹 - how to share folder between host os and docker container 如何使用Docker Compose在主机和容器之间共享文件系统? - How can I share the filesystem between host and container with Docker Compose? Docker和--userns-remap,如何管理卷权限以在主机和容器之间共享数据? - Docker and --userns-remap, how to manage volume permissions to share data between host and container? 如何在多个Docker容器之间共享主机目录? - How to share a host directory between multiple docker container? Docker容器无法与主机共享数据 - Docker container cannot share data with host 如何使用Docker Compose中的卷在主机和容器之间共享数据 - How to share data between host and containers using volumes in Docker Compose 如何将文件从 docker 容器共享到主机 - How to share file from docker container to host 如何在 docker 主机和 docker 容器之间共享端口 8080 而不会发生端口冲突? - How to share the port 8080 between docker host and docker container with out port conflict? 在 Kubernetes 中的本地和 Docker 容器之间共享数据 - Share data between Local and Docker container in Kubernetes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM