简体   繁体   English

Docker-访问主机上的现有容器文件

[英]Docker - access existing container files on host machine

I have a docker container which has some data in let's say /opt/files. 我有一个Docker容器,其中有一些数据,例如/ opt / files。 File A and B. How can I start that container and access these files on my host machine? 文件A和B。如何启动该容器并访问主机上的这些文件?

I'm using Docker for Windows (Hyper-V). 我正在使用适用于Windows(Hyper-V)的Docker。 When i start the container with: 当我用以下方法启动容器时:

docker run -it -v C:/tmp:/opt/files myImage

I see an empty folder on my windows machine and inside of the container. 我在Windows机器上和容器内部看到一个空文件夹。 Any new files I create there are of course reflected on both sides but how can I access files that are already in the container (eg because they're added in the Dockerfile)? 我在那里创建的任何新文件都在两侧都有体现,但是如何访问容器中已经存在的文件(例如,因为它们已添加到Dockerfile中)?

Once your container is started, you can copy files inside it to your host. 启动容器后,您可以将其中的文件复制到主机。

Use docker cp for this ( https://docs.docker.com/engine/reference/commandline/cp/ ). 为此使用docker cphttps://docs.docker.com/engine/reference/commandline/cp/ )。

Example : docker cp CONTAINER:SRC_PATH DEST_PATH|- 示例: docker cp CONTAINER:SRC_PATH DEST_PATH|-

You can't share from inside container to host. 您不能从容器内部共享到主机。 There are two ways to do it 有两种方法可以做到这一点

Copy the files from container 从容器复制文件

docker cp <containerid>:<file_path_inside_container> localpath

Share a folder other than the one where files will be generated 共享一个文件夹,而不是将生成文件的文件夹

docker run -it -v C:/tmp:/opt/files_temp myImage

Then you get inside the container copy files from /opt/files to /opt/files_temp 然后进入容器,将文件从/opt/files/opt/files_temp

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM