如何访问Docker容器外部的文件

If containers are isolated, how can they communicate to the host machine, perhaps to store data? Because when we create a container from an image, any data generated is lost when the container is removed.

如果容器是隔离的,它们如何与主机通信,也许可以存储数据? 因为当我们从图像创建容器时,删除容器后生成的所有数据都会丢失。

So we need a way to have permanent storage.

因此,我们需要一种永久存储的方法。

We can do so using Bind Mounts and Volumes.

我们可以使用Bind Mounts and Volumes来做到这一点。

There’s not a lot of difference between the two, except Bind Mounts can point to any folder on the host computer, and are not managed by Docker directly.

两者之间没有太大区别,除了Bind Mounts可以指向主机上的任何文件夹,并且不直接由Docker管理。

Let’s start with them. One classic example is logs. Suppose your app creates a log file, inside the container, in /usr/src/app/logs. You can map that to a folder on the host machine, using the -v (same as --volume) flag when you run the container with docker run, like this: -v ~/logs:/usr/src/app/logs

让我们从他们开始。 一个经典的例子是日志。 假设您的应用在/usr/src/app/logs的容器内创建了一个日志文件。 您可以映射到主机上的文件夹,使用-v (同--volume当你运行容器)标志docker run :像这样-v ~/logs:/usr/src/app/logs

This will map that folder to the logs subfolder in the user’s home directory.

这会将该文件夹映射到用户主目录中的logs子文件夹。

Node: the -m or --mount flag works in a very similar way

节点: -m--mount标志的工作方式非常相似

This is the flag used with the examplenode image we created previously:

这是我们先前创建的examplenode图像使用的标志:

docker run -d -p 80:3000 -v ~/logs:/usr/src/app/logs --name node-app examplenode

So now we can run our Node app, and any log will be stored in the host computer, rather than inside the Docker container.

因此,现在我们可以运行Node应用程序,所有日志都将存储在主机中,而不是Docker容器中。

Note that the examplenode app does not generate any log in /usr/src/app/logs, it’s just an example and you would need to set that logging up first.

请注意, examplenode应用程序不会在/usr/src/app/logs生成任何日志,这只是一个示例,您需要首先设置该日志记录。

The details about the volume will be listed when you run docker inspect on the container name, under “Mounts”:

当您对容器名称运行docker inspect ,将在“挂载”下列出有关卷的详细信息:

"Mounts": [
    {
        "Type": "bind",
        "Source": "/Users/flavio/logs",
        "Destination": "/usr/src/app/logs",
        "Mode": "",
        "RW": true,
        "Propagation": "rprivate"
    }
],

Can you see "Type": "bind"? That means that we have created a bind mount.

您可以看到"Type": "bind"吗? 这意味着我们已经创建了一个绑定安装

Now, let’s talk about Volumes.

现在,让我们谈谈Volumes

The difference between Bind Mounts and Volumes is that by creating volumes, Docker will store the data in a folder it manages, which means it will take care of file permissions and ownership, and it will give you the tools to manage those volumes. While bind mounts are based on filesystem paths, and Docker can’t provide the tooling around them.

Bind Mounts和Volumes之间的区别在于,通过创建卷,Docker会将数据存储在它管理的文件夹中,这意味着它将处理文件权限和所有权,并为您提供管理这些卷的工具。 尽管绑定挂载基于文件系统路径,但Docker无法提供围绕它们的工具。

For example, Docker lets you remove all unused volumes by running docker volume prune or docker system prune --volumes.

例如,Docker可让您通过运行docker system prune --volumes docker volume prunedocker system prune --volumes删除所有未使用的卷。

To create a volume, we first need to run docker volume create:

要创建一个卷,我们首先需要运行docker volume create

docker volume create logs

Now you can use docker volume ls and docker volume inspect to get more data about the system volumes:

现在,您可以使用docker volume lsdocker volume inspect来获取有关系统卷的更多数据:

如何访问Docker容器外部的文件_第1张图片

Now run docker run with the option -v logs:/usr/src/app/logs (tell the volume name instead of a folder)

现在使用-v logs:/usr/src/app/logs选项运行docker run (告诉卷名而不是文件夹)

docker run -d -p 80:3000 -v logs:/usr/src/app/logs --name node-app examplenode

Now running docker inspect on the image will show the mounted volume:

现在在图像上运行docker inspect将显示已安装的卷:

"Mounts": [
    {
        "Type": "volume",
        "Name": "logs",
        "Source": "/var/lib/docker/volumes/logs/_data",
        "Destination": "/usr/src/app/logs",
        "Driver": "local",
        "Mode": "z",
        "RW": true,
        "Propagation": ""
    }
],

See? Now the logs will be stored in the /var/lib/docker/volumes/logs/_data folder.

看到? 现在,日志将存储在/var/lib/docker/volumes/logs/_data文件夹中。

Volumes will be essential when it will be time to deploy a container on a cloud service, for example.

例如,当需要在云服务上部署容器时,卷将至关重要。

You can remove a volume running docker volume rm .

您可以删除运行docker volume rm

翻译自: https://flaviocopes.com/docker-access-files-outside-container/

你可能感兴趣的:(docker,java,linux,python,大数据)