在Docker容器中处理持久数据时绑定绑定和卷之间有什么区别? [英] What Is The Difference Between Binding Mounts And Volumes While Handling Persistent Data In Docker Containers?

查看:100
本文介绍了在Docker容器中处理持久数据时绑定绑定和卷之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我们有两个不同的选项来做同一件事,两者之间有什么区别。

I want to know why we have two different options to do the same thing, What are the differences between the two.

推荐答案

我们基本上有3种类型的用于持久数据的卷或装载:

We basically have 3 types of volumes or mounts for persistent data:


  1. 绑定装载

  1. Bind mounts

命名卷

dockerfile中的卷

Volumes in dockerfiles

绑定装载基本上只是从容器内的主机绑定某个目录或文件( docker run -v / hostdir:/ containerdir IMAGE_NAME

Bind mounts are basically just binding a certain directory or file from the host inside the container (docker run -v /hostdir:/containerdir IMAGE_NAME)

命名卷是您使用 docker volume create VOLUME_NAME 手动创建的卷。它们是在 / var / lib / docker / volumes 中创建的,只能通过其名称进行引用。假设您创建了一个名为 mysql_data的卷,您可以像这样 docker run -v mysql_data:/ containerdir IMAGE_NAME 来引用它。

Named volumes are volumes which you create manually with docker volume create VOLUME_NAME. They are created in /var/lib/docker/volumes and can be referenced to by only their name. Let's say you create a volume called "mysql_data", you can just reference to it like this docker run -v mysql_data:/containerdir IMAGE_NAME.

然后是dockerfile中的卷,这些卷是由 VOLUME 指令创建的。这些卷也是在 / var / lib / docker / volumes 下创建的,但是没有特定的名称。他们的名字仅仅是某种哈希。在运行容器时将创建该卷,并且无论是否使用 -v 启动容器,该卷都可以方便地保存持久数据。开发人员会说出重要数据在哪里以及应该保留哪些数据。

And then there's volumes in dockerfiles, which are created by the VOLUME instruction. These volumes are also created under /var/lib/docker/volumes but don't have a certain name. Their "name" is just some kind of hash. The volume gets created when running the container and are handy to save persistent data, whether you start the container with -v or not. The developer gets to say where the important data is and what should be persistent.

我应该使用什么?

您要使用的东西主要取决于首选项或您的管理。如果要将所有内容保留在 docker区域( / var / lib / docker )中,则可以使用卷。如果要保留自己的目录结构,则可以使用绑定。

What you want to use comes mostly down to either preference or your management. If you want to keep everything in the "docker area" (/var/lib/docker) you can use volumes. If you want to keep your own directory-structure, you can use binds.

Docker建议使用卷而不是使用绑定,因为卷是由docker创建和管理的,并且绑定具有更大的失败可能性(同样由于第8层问题)。

Docker recommends the use of volumes over the use of binds, as volumes are created and managed by docker and binds have a lot more potential of failure (also due to layer 8 problems).

如果您使用绑定并想转移您的另一台主机上的容器/应用程序,则必须重建目录结构,因为每台主机上的卷都更加统一。

If you use binds and want to transfer your containers/applications on another host, you have to rebuild your directory-structure, where as volumes are more uniform on every host.

这篇关于在Docker容器中处理持久数据时绑定绑定和卷之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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