如何处理 Docker 中的持久存储(例如数据库) [英] How to deal with persistent storage (e.g. databases) in Docker
问题描述
人们如何处理 Docker 容器的持久存储?
How do people deal with persistent storage for your Docker containers?
我目前正在使用这种方法:构建图像,例如对于 PostgreSQL,然后用
I am currently using this approach: build the image, e.g. for PostgreSQL, and then start the container with
docker run --volumes-from c0dbc34fd631 -d app_name/postgres
恕我直言,有一个缺点,我不能(偶然)删除容器c0dbc34fd631".
IMHO, that has the drawback, that I must not ever (by accident) delete container "c0dbc34fd631".
另一个想法是将主机卷-v"挂载到容器中,但是,容器内的用户 ID 不一定与主机的 用户 ID 匹配,然后权限可能会搞砸.
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.
注意:除了 --volumes-from 'cryptic_id'
您还可以使用 --volumes-from my-data-container
where my-data-container
是您分配给仅数据容器的名称,例如docker run --name my-data-container ...
(见已接受的答案)
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, e.g. docker run --name my-data-container ...
(see the accepted answer)
推荐答案
Docker 1.9.0 及以上
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.
实际上,volume API 只是实现数据容器模式的更好方法.
Actually the volume API is only a better way to achieve what was the data-container pattern.
如果您使用 -v volume_name:/container/fs/path
创建容器,Docker 将自动为您创建一个命名卷,它可以:
If you create a container with a -v volume_name:/container/fs/path
Docker will automatically create a named volume for you that can:
- 通过
docker volume ls
列出 - 通过
docker volume inspect volume_name
来识别 - 备份为普通目录
- 像以前一样通过
--volumes-from
连接进行备份
- Be listed through the
docker volume ls
- Be identified through the
docker volume inspect volume_name
- Backed up as a normal directory
- Backed up as before through a
--volumes-from
connection
新的volume API 添加了一个有用的命令,可以让您识别悬空的volumes:
The new volume API adds a useful command that lets you identify dangling volumes:
docker volume ls -f dangling=true
然后通过它的名字删除它:
And then remove it through its name:
docker volume rm <volume name>
正如@mpugach 在评论中强调的那样,你可以用一个漂亮的单线摆脱所有悬垂的卷:
As @mpugach underlines in the comments, you can get rid of all the dangling volumes with a nice one-liner:
docker volume rm $(docker volume ls -f dangling=true -q)
# Or using 1.13.x
docker volume prune
Docker 1.8.x 及以下
似乎最适合生产的方法是使用纯数据容器.
纯数据容器在准系统镜像上运行,除了暴露数据卷之外,实际上什么都不做.
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 文档现在将 容器的 DEFINITIVE 描述为 volume/s强> 模式.
以下是 Docker 1.8.x 及以下版本的备份/恢复过程.
Following is the backup/restore procedure for Docker 1.8.x and below.
备份:
sudo docker run --rm --volumes-from DATA -v $(pwd):/backup busybox tar cvf /backup/backup.tar /data
- --rm:在容器退出时移除容器
- --volumes-from DATA:附加到 DATA 容器共享的卷
- -v $(pwd):/backup:绑定挂载当前目录到容器中;将tar文件写入
- busybox:一个简单的小图像 - 便于快速维护
- tar cvf/backup/backup.tar/data:为/data目录下的所有文件创建一个未压缩的tar文件
恢复:
# 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
这是一篇很棒的来自优秀的 Brian Goff 的文章 解释为什么容器和数据容器使用相同的图像是好的.
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.
这篇关于如何处理 Docker 中的持久存储(例如数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!