使用单个数据卷容器

时间:2015-07-27 19:37:40

标签: docker containers

我最近一直在调查docker,然后我开始研究数据量容器,以及如何在会话之间保留信息,我很困惑。

大多数情况下是否可以使用单个数据卷容器?

让我们看一个简单的库/ postgres图像,在它的dockerfile中公开一个卷/ var / lib / postgresql / data假设我在本地映射到/ docker / db_data在一个容器设置中,我明白了/ var / lib / postgresql / data的内容可以在我的laptops / docker / db_data文件夹中找到。

但是如果你想分支出另外一个容器运行Jenkins会发生什么,例如,它将/ var / jenkins_home暴露为一个卷。最初我会将其映射到笔记本电脑上的另一个文件夹,例如/ docker / jenkins_data。

但是,如果我想启用数据卷容器来管理这两者,那怎么办呢?

让我们说我们要将两个容器映射到我的笔记本电脑上的目录,/ docker / db_data和/ docker / jenkins_data

或者我做错了,如果我使用2个数据量容器并将其保持简单,它会被视为最佳做法吗?

1 个答案:

答案 0 :(得分:5)

Lets take a simple one, the library/postgres image, in its dockerfile it exposes a volume /var/lib/postgresql/data Suppose I mapped that locally to /docker/db_data in a single container setup, I understand that the contents of /var/lib/postgresql/data would be found in my laptops /docker/db_data folder.

Not exactly. You can mount a directory from your laptop into a container with -v /docker/db_data:/var/lib/postgressql/data. If /var/lib/postgressql/data already exists in the container, it will be overwritten with the content of /docker/db_data.

However, if you use -v /var/lib/postgressql/data (not mounting a host directory), any files that the image has in /var/lib/postgressql/data will be copied into the volume.

You can see the actual location of the data for a volume with the docker inspect command, eg docker inspect postgressql will show you something like…

"Volumes": { "/var/lib/postgressql/data": "/var/lib/docker/volumes/[some_guid_like_string]" }

But if I want to enable a data volume container to manage both of these, how can this be done?

A container can have multiple volumes with multiple -v arguments. In your case if you wanted one data volume container with 2 volumes, you would first create a container to serve as the data volume container with:

docker create -v /var/lib/postgressql/data -v /var/jenkins_home --name mydata ubuntu /bin/true

Note the 2 volumes. You docker create because the data container does not need to be running to store data.

Now your postgres and jenkins containers can access the volumes using the --volumes-from flag. Here's an example for postgres:

docker run --volumes-from mydata --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

With --volumes-from, the postgres container will use both the volumes from the mydata container, although the postgres one only really cares about /var/lib/postgressql/data.

would it be considered best practises if I used 2 data volume containers, and kept it simple?

I'm not sure about best practice, but I'd say there's no reason for 1 data volume container to manage the data of two unrelated containers. I'd go with separate data volume containers for each.

If, later on, you needed to do something like backup or migrate the data for both postgres and jenkins, you could create another container on the fly with multiple --volumes-from and back up the whole thing. It's pretty flexible.

相关问题