如何处理停泊处的持久存储(例如数据库) [英] How to deal with persistent storage (e.g. databases) in docker
问题描述
$ code> docker run --volumes从c0dbc34fd631 -d app_name / postgres
启动容器
IMHO,那有缺点,我永远不会(意外)删除容器c0dbc34fd631。 >
另一个想法是将主机卷-v加载到容器中,但容器中的 userid 不一定与 userid ,然后权限可能会被搞砸。
注意:而不是 - volumes-from'cryptic_id'
您还可以使用 - 卷 - 来自my-data-container
其中 my-data-container
是您分配给仅数据的容器的名称,例如 docker run --name my-data-container ...
(见接受的答案)
Docker 1.9.0及以上
使用卷API
docker volume create --name hello
docker运行-d -v hello:/ container / path / for / volume container_image my_command
这意味着必须放弃数据容器模式,以支持新的卷。
实际上,卷API只是更好的方式来实现数据容器模式。 p>
如果您创建一个容器,具有 -v volume_name:/ container / fs / path
docker将自动创建一个命名卷对于您可以:
- 通过
docker volume ls
- 通过
docker volume检查volume_name
- 备份为正常目录
- 通过
- 从 -
连接
新卷api添加了一个有用的命令,让您识别悬挂卷:
docker volume ls -f dangling = true
然后通过它的名字删除它:
docker volume rm< volume name>
as @mpugach在评论中强调可以摆脱所有的悬挂卷与一个好的一个衬垫:
docker volume rm $(docker volume ls -f dangling = true -q)
#或使用1.13。 x
码头货币修剪
Docker 1.8.x及更低版本
似乎最适合生产的方法是使用仅数据容器。
仅数据容器运行在正确的镜像上,实际上除了暴露
a数据卷之外什么都不做。
然后,您可以运行任何其他容器来访问数据容器卷:
docker run --volumes-from data-container some-other-container command-to-execute
- 你可以得到如何安排差异的好照片t容器
- 这里有一个很好的见解关于卷的工作方式
在这个博文有一个很好的描述所谓的容器作为体积模式,这澄清了
Docker文档现在将容器的DEFINITIVE描述作为volume / s 模式。
以下是Docker 1.8.x及更低版本的备份/恢复过程
备份:
sudo docker运行--rm --volumes-from DATA -v $(pwd):/ backup busybox tar cvf /backup/backup.tar / data
- - rm:删除容器退出
- - 卷 - 从DATA:附加到DATA容器共享的卷
- -v $(pwd):/ backup:bind将当前目录挂载到容器中;将tar文件写入
- busybox:一个简单的图像 - 适合快速维护
- tar cvf /backup/backup.tar / data:创建/ data目录中所有文件的未压缩tar文件
RESTORE:
#创建一个新的数据容器
$ sudo docker运行-v / data-name DATA2 busybox true
#untar备份文件进入新容器数据卷
$ sudo docker运行--rm --volumes-from DATA2 -v $(pwd):/ backup busybox tar xvf /backup/backup.tar
data /
data / sven.txt
#与原始容器比较
$ sudo docker run --rm --volumes-from DATA -v`pwd`:/ backup busybox ls / data
sven.txt
这是一个很好的来自优秀的Brian Goff的文章解释了为什么对于容器和数据容器使用相同的图像是很好的
How do people deal with persistent storage for your docker containers? I am currently using this approach: build the image, e.g. for Postgres, and then start the container with
docker run --volumes-from c0dbc34fd631 -d app_name/postgres
IMHO, that has the drawback, that I must not ever (by accident) delete container "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.
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 accepted answer)
Docker 1.9.0 and above
Use 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.
If you create a container with a -v volume_name:/container/fs/path
docker will automatically create a named volume for you that can:
- Be listed through the
docker volume ls
- Be identified through the
docker volume inspect volume_name
- Backed up as a normal dir
- Backed up as before through a
--volumes-from
connection
The new volume api adds a useful command that let you identify dangling volumes:
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:
docker volume rm $(docker volume ls -f dangling=true -q)
# or using 1.13.x
docker volume prune
Docker 1.8.x and below
The approach that seems to work best for production is to use a data only container.
The data only container is run on a barebone 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.
Following is backup/restore procedure for Docker 1.8.x and below
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
- --volumes-from DATA: attach to the volumes shared by the DATA container
- -v $(pwd):/backup: bind mount the current directory into the container; to write the tar file to
- busybox: a small simpler image - good for quick maintenance
- tar cvf /backup/backup.tar /data: creates an uncompressed tar file of all the files in the /data directory
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.
这篇关于如何处理停泊处的持久存储(例如数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!