人们如何处理Docker容器的持久存储?

我目前正在使用这种方法:构建映像,例如PostgreSQL,然后启动容器

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

恕我直言,这有缺点,我不能(意外地)删除容器“c0dbc34fd631”。

另一个想法是将主机卷“-v”挂载到容器中,然而,容器中的用户id不一定与来自主机的用户id匹配,然后权限可能会被打乱。

注意:除了——volumes-from 'cryptic_id',你还可以使用——volumes-from my-data-container,其中my-data-container是你分配给仅数据容器的名称,例如docker run——name my-data-container…(见公认答案)


当前回答

在Docker v1.0版本中,可以通过以下命令绑定主机上的文件或目录的挂载:

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

上面的卷可以用作运行Docker的主机上的持久存储。

其他回答

使用Docker Compose时,只需附加一个命名卷,例如:

version: '2'
services:
  db:
    image: mysql:5.6
    volumes:
      - db_data:/var/lib/mysql:rw
    environment:
      MYSQL_ROOT_PASSWORD: root
volumes:
  db_data:

@tommasop的回答很好,并解释了使用仅数据容器的一些机制。但是,有些人最初认为数据容器很愚蠢,因为可以将一个卷绑定到主机上(正如其他几个答案所建议的那样),但现在意识到实际上只有数据的容器非常整洁,我可以推荐我自己的博客文章:为什么Docker数据容器(卷!)很好

请参见:我对“管理Docker共享卷权限的(最佳)方法是什么?”的回答,以获得如何使用数据容器来避免权限和与主机的uid/gid映射等问题的示例。

To address one of the OP's original concerns: that the data container must not be deleted. 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 i.e. any container that mounted the volume via --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. You can always recreate the data container by doing --volumes-from any container that has a reference to that volume.

和往常一样,做备份!

更新:Docker现在有了可以独立于容器管理的卷,这进一步简化了管理。

我最近写了一篇关于潜在解决方案和演示该技术的应用程序的文章。我发现它在开发和生产过程中非常有效。希望它能帮助或激发一些想法。

回购:https://github.com/LevInteractive/docker-nodejs-example 文章:http://lev-interactive.com/2015/03/30/docker-load-balanced-mongodb-persistence/

我只是在主机上使用一个预定义的目录来持久化PostgreSQL的数据。同样,通过这种方式,可以轻松地将现有的PostgreSQL安装迁移到Docker容器:https://crondev.com/persistent-postgresql-inside-docker/

在Docker v1.0版本中,可以通过以下命令绑定主机上的文件或目录的挂载:

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

上面的卷可以用作运行Docker的主机上的持久存储。