具有初始数据的码头邮政经纪人不会持续超过提交 [英] docker postgres with initial data is not persisted over commits

查看:105
本文介绍了具有初始数据的码头邮政经纪人不会持续超过提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在docker环境中创建了一个rails应用程序,并链接到postgres实例。我编辑
postgres容器来添加初始数据(通过从rails应用程序运行rake db:setup)。现在我提交了postgres数据库,但是当我创建一个新的容器(被提交的postgres映像)似乎不记得我的数据。



不是吗可能在提交中保存数据,然后再次使用?



我使用了postgres图片: https://registry.hub.docker.com/_/postgres/

解决方案

问题是Postgres Dockerfile将/ var / lib / postgresql / data声明为一个卷。这只是一个正常的目录,它不在图像所使用的联盟文件系统之外。卷直到没有容器链接到它们并被明确删除。



你有几个选择:




  • 使用 - volumes-from 命令与新容器共享数据。这只能在一次只有一个运行的postgres映像的情况下工作,但这是最好的解决方案。

  • 在声明卷之前编写自己的Dockerfile来创建数据。

  • 编写一个entrypoint或cmd脚本,在运行时填充数据库。

  • ul>

    所有这些建议都要求您在容器运行后使用卷来管理数据。或者,您可以编写自己的Dockerfile,而不是声明一个卷。然后,您可以使用 docker commit 在添加数据后创建新映像。这可能会在短期内工作,但绝对不是你应该如何处理容器 - 这不是可重复的,你最终将在联盟文件系统中用尽。



    有关详细信息,请查看管理容器中的数据的官方Docker文档。


    I created a rails app in a docker environment and it links to a postgres instance. I edited the postgres container to add initial data (by running rake db:setup from the rails app). Now I commited the postgres database, but it doesn't seem to remember my data when I create a new container (of the commited postgres image).

    Isn't it possible to save data in a commit and then reuse it afterwards?

    I used the postgres image: https://registry.hub.docker.com/_/postgres/

    解决方案

    The problem is that the postgres Dockerfile declares "/var/lib/postgresql/data" as a volume. This is a just a normal directory that lives outside of the Union File System used by images. Volumes live until no containers link to them and they are explicitly deleted.

    You have a few choices:

    • Use the --volumes-from command to share data with new containers. This will only work if there is only one running postgres image at a time, but it is the best solution.
    • Write your own Dockerfile which creates the data before declaring the volume. This data will then be copied into the volume when the container is created.
    • Write an entrypoint or cmd script which populates the database at run time.

    All of these suggestions require you to use Volumes to manage the data once the container is running. Alternatively, you could write your own Dockerfile and simply not declare a volume. You could then use docker commit to create a new image after adding data. This will probably work in the short term, but is definitely not how you should work with containers - it isn't repeatable and you will eventually run out of layers in the Union File System.

    Have a look at the official Docker docs on managing data in containers for more info.

    这篇关于具有初始数据的码头邮政经纪人不会持续超过提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆