在Docker中,如何从容器到主机共享卷? [英] In Docker, how do I share a volume from a container to a host?

查看:207
本文介绍了在Docker中,如何从容器到主机共享卷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以使用VOLUMES指令与容器共享主机上存在的目录,但是我想做相反的事情 - 与主机共享容器中存在的文件,只要没有存在在主机上的目录中。

I know you can share a directory that exists on the host with a container using the VOLUMES directive, but I'd like to do the opposite--share the files that exist in the container with the host, provided that nothing exists in that directory on the host.

这个用例是我们有承包商,我们希望为他们提供一个包含所有代码的docker容器和他们需要使用的基础设施,但这将允许他们通过在本地机器上运行PyCharm或其他程序来更改代码,而无需担心提交容器等。

The use case for this is that we have contractors, and we'd like to provide them with a docker container that contains all the code and infrastructure they need to work with, but that would allow them to alter that code by running PyCharm or some other program on their local machine without having to worry about committing the container, ...etc.

推荐答案

为了能够保存(持久)数据,Docker提出了卷的概念。基本上是在默认的联合文件系统之外的目录(或文件),并且作为主机文件系统上的普通目录和文件存在。

In order to be able to save (persist) data, Docker came up with the concept of volumes. basically Volumes are directories (or files) that are outside of the default Union File System and exist as normal directories and files on the host filesystem.

您可以使用-v标志在运行时声明一个卷:

You can declare a volume at run-time with the -v flag:

$ docker run -it --name container1 -v /data debian /bin/bash

这将使目录<$容器内的c $ c> / data 在Union File System之外,可以直接在主机上访问。图像保存在 / data 目录中的任何文件都将被复制到卷中。我们可以通过在主机上使用docker inspect命令来找出主机上的卷的位置。

This will make the directory /data inside the container live outside the Union File System and directly accessible on the host. Any files that the image held inside the /data directory will be copied into the volume. We can find out where the volume lives on the host by using the docker inspect command on the host.

打开一个新的终端,离开前一个容器运行并运行:

Open a new terminal and leave the previous container running and run:

$ docker inspect container1

输出将提供有关容器配置(包括卷)的详细信息。
输出应该类似于以下内容:

The output will provide details on the container configurations including the volumes. The output should look something similar to the following:

...
Mounts": [
    {
        "Name": "fac362...80535",
        "Source": "//var/lib/docker/vfs/dir/cde167197ccc3e/_data",
        "Destination": "/data",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
]
...

告诉我们该Docker已将 / data 在容器内作为主机中 / var / lib / docker 之下的目录。

Telling us that Docker has mounted /data inside the container as a directory somewhere under /var/lib/docker in the host.

这篇关于在Docker中,如何从容器到主机共享卷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆