如何处理Docker中的持久存储(例如数据库)

本文翻译自:How to deal with persistent storage (e.g. databases) in Docker

How do people deal with persistent storage for your Docker containers? 人们如何处理Docker容器的持久存储?

I am currently using this approach: build the image, eg for PostgreSQL, and then start the container with 我目前正在使用这种方法:构建映像,例如PostgreSQL,然后启动容器

docker run --volumes-from c0dbc34fd631 -d app_name/postgres

IMHO, that has the drawback, that I must not ever (by accident) delete container "c0dbc34fd631". 恕我直言,有缺点,我不能(偶然)删除容器“c0dbc34fd631”。

Another idea would be to mount host volumes "-v" into the container, however, the userid within the container does not necessarily match the userid from the host, and then permissions might be messed up. 另一个想法是将主机卷“-v”安装到容器中,然而,在容器内的用户ID并不一定来自主机匹配的用户ID,然后权限可能会混乱。

Note: Instead of --volumes-from 'cryptic_id' you can also use --volumes-from my-data-container where my-data-container is a name you assigned to a data-only container, eg docker run --name my-data-container ... (see the accepted answer) 注意:代替--volumes-from 'cryptic_id'您也可以使用--volumes-from my-data-container ,其中my-data-container是您分配给仅my-data-container的名称,例如docker run --name my-data-container ... (参见接受的答案)


#1楼

参考:https://stackoom.com/question/1Fbtk/如何处理Docker中的持久存储-例如数据库


#2楼

While this is still a part of Docker that needs some work , you should put the volume in the Dockerfile with the VOLUME instruction so you don't need to copy the volumes from another container. 虽然这仍然是Docker的一部分需要一些工作 ,但您应该使用VOLUME指令将卷放入Dockerfile中,这样您就不需要从另一个容器中复制卷。

That will make your containers less inter-dependent and you don't have to worry about the deletion of one container affecting another. 这将使您的容器减少相互依赖,并且您不必担心删除一个影响另一个容器的容器。


#3楼

It depends on your scenario (this isn't really suitable for a production environment), but here is one way: 这取决于您的场景(这不适合生产环境),但这是一种方式:

Creating a MySQL Docker Container 创建MySQL Docker容器

This gist of it is to use a directory on your host for data persistence. 它的要点是使用主机上的目录来实现数据持久性。


#4楼

Docker 1.9.0 and above Docker 1.9.0及更高版本

Use volume API 使用volume API

docker volume create --name hello
docker run -d -v hello:/container/path/for/volume container_image my_command

This means that the data-only container pattern must be abandoned in favour of the new volumes. 这意味着必须放弃仅数据容器模式以支持新卷。

Actually the volume API is only a better way to achieve what was the data-container pattern. 实际上,卷API只是实现数据容器模式的更好方法。

If you create a container with a -v volume_name:/container/fs/path Docker will automatically create a named volume for you that can: 如果使用-v volume_name:/container/fs/path创建容器-v volume_name:/container/fs/path Docker将自动为您创建一个命名卷,它可以:

  1. Be listed through the docker volume ls 通过docker volume ls
  2. Be identified through the docker volume inspect volume_name 通过docker volume inspect volume_name识别
  3. Backed up as a normal directory 备份为普通目录
  4. Backed up as before through a --volumes-from connection 像以前一样通过--volumes-from连接--volumes-from

The new volume API adds a useful command that lets you identify dangling volumes: 新的卷API添加了一个有用的命令,可以识别悬空卷:

docker volume ls -f dangling=true

And then remove it through its name: 然后通过其名称删除它:

docker volume rm <volume name>

As @mpugach underlines in the comments, you can get rid of all the dangling volumes with a nice one-liner: 正如@mpugach在评论中强调的那样,你可以用一个漂亮的单行代码摆脱所有悬空音量:

docker volume rm $(docker volume ls -f dangling=true -q)
# Or using 1.13.x
docker volume prune

Docker 1.8.x and below Docker 1.8.x及以下版本

The approach that seems to work best for production is to use a data only container . 似乎最适合生产的方法是使用仅数据容器

The data only container is run on a barebones image and actually does nothing except exposing a data volume. 仅数据容器在准系统映像上运行,除了暴露数据卷之外实际上什么都不做。

Then you can run any other container to have access to the data container volumes: 然后,您可以运行任何其他容器来访问数据容器卷:

docker run --volumes-from data-container some-other-container command-to-execute
  • Here you can get a good picture of how to arrange the different containers. 在这里,您可以很好地了解如何安排不同的容器。
  • Here there is a good insight on how volumes work. 在这里 ,您可以很好地了解卷的工作原理。

In this blog post there is a good description of the so-called container as volume pattern which clarifies the main point of having data only containers . 这篇博文中,有一个很好的描述所谓的容量作为卷模式 ,它阐明了拥有数据容器的要点。

Docker documentation has now the DEFINITIVE description of the container as volume/s pattern. Docker文档现在将容器的DEFINITIVE描述为volume / s模式。

Following is the backup/restore procedure for Docker 1.8.x and below. 以下是Docker 1.8.x及更低版本的备份/恢复过程。

BACKUP: 备份:

sudo docker run --rm --volumes-from DATA -v $(pwd):/backup busybox tar cvf /backup/backup.tar /data
  • --rm: remove the container when it exits --rm:退出时移除容器
  • --volumes-from DATA: attach to the volumes shared by the DATA container --volumes-from DATA:附加到DATA容器共享的卷
  • -v $(pwd):/backup: bind mount the current directory into the container; -v $(pwd):/ backup:bind将当前目录挂载到容器中; to write the tar file to 写tar文件到
  • busybox: a small simpler image - good for quick maintenance busybox:一个简单的小图像 - 有助于快速维护
  • tar cvf /backup/backup.tar /data: creates an uncompressed tar file of all the files in the /data directory tar cvf /backup/backup.tar / data:创建/ data目录中所有文件的未压缩tar文件

RESTORE: 恢复:

# Create a new data container
$ sudo docker run -v /data -name DATA2 busybox true
# untar the backup files into the new container᾿s data volume
$ sudo docker run --rm --volumes-from DATA2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar
data/
data/sven.txt
# Compare to the original container
$ sudo docker run --rm --volumes-from DATA -v `pwd`:/backup busybox ls /data
sven.txt

Here is a nice article from the excellent Brian Goff explaining why it is good to use the same image for a container and a data container. 这是来自优秀的Brian Goff的一篇很好的文章,解释了为什么对容器和数据容器使用相同的图像是好的。


#5楼

In Docker release v1.0 , binding a mount of a file or directory on the host machine can be done by the given command: Docker发行版v1.0中 ,可以通过给定的命令在主机上绑定文件或目录的安装:

$ docker run -v /host:/container ...

The above volume could be used as a persistent storage on the host running Docker. 上述卷可以用作运行Docker的主机上的持久存储。


#6楼

@tommasop's answer is good, and explains some of the mechanics of using data-only containers. @ tommasop的答案很好,并解释了使用仅数据容器的一些机制。 But as someone who initially thought that data containers were silly when one could just bind mount a volume to the host (as suggested by several other answers), but now realizes that in fact data-only containers are pretty neat, I can suggest my own blog post on this topic: Why Docker Data Containers (Volumes!) are Good 但是,有人最初认为数据容器是愚蠢的,当一个人可以将卷装入主机(如其他几个答案所示),但现在意识到实际上只有数据的容器非常整洁,我可以建议我自己关于此主题的博客文章: 为什么Docker数据容器(卷!)是好的

See also: my answer to the question " What is the (best) way to manage permissions for Docker shared volumes? " for an example of how to use data containers to avoid problems like permissions and uid/gid mapping with the host. 另请参阅: 对“ 管理Docker共享卷权限的最佳方法是什么? ”这一问题的回答,以获取如何使用数据容器来避免诸如权限和与主机的uid / gid映射之类的问题的示例。

To address one of the OP's original concerns: that the data container must not be deleted. 解决OP的一个原始问题:不得删除数据容器。 Even if the data container is deleted, the data itself will not be lost as long as any container has a reference to that volume ie any container that mounted the volume via --volumes-from . 即使删除了数据容器,只要任何容器具有对该卷的引用,即通过--volumes-from安装卷的任何容器,数据本身都不会丢失。 So unless all the related containers are stopped and deleted (one could consider this the equivalent of an accidental rm -fr / ) the data is safe. 因此,除非停止并删除所有相关容器(可以认为这相当于意外的rm -fr / ),否则数据是安全的。 You can always recreate the data container by doing --volumes-from any container that has a reference to that volume. 您始终可以通过--volumes-from任何具有该卷引用的容器执行--volumes-from重新创建数据容器。

As always, make backups though! 一如既往,尽管做备份!

UPDATE: Docker now has volumes that can be managed independently of containers, which further makes this easier to manage. 更新:Docker现在拥有可以独立于容器进行管理的卷,这使得这更容易管理。

发布了0 篇原创文章 · 获赞 137 · 访问量 84万+

猜你喜欢

转载自blog.csdn.net/xfxf996/article/details/105323312