我该如何使用Postgresql docker镜像/容器?

我是码头工人的新手.我仍然试图绕过这一切.

我正在构建一个节点应用程序(REST api),使用Postgresql来存储我的数据.

我花了几天时间学习码头工具,但我不确定我是否按照我应该的方式做事.

所以这是我的问题:

>我正在使用官方docker postgres 9.5映像作为基础来构建我自己的(我的Dockerfile只在其上添加了plpython,并安装了一个自定义python模块以便在plpython存储过程中使用).我创建了我的容器作为postgres图像文档的建议:

docker run –name some-postgres -e POSTGRES_PASSWORD = mysecretpassword -d postgres

在我停止容器之后,我无法使用上面的命令再次运行它,因为容器已经存在.所以我开始使用docker start而不是docker run.这是正常的做事方式吗?我一般会在第一次使用docker run并且每隔一段时间启动docker吗?
> Persistance:我创建了一个数据库,并在正在运行的容器上填充它.我使用pgadmin3进行连接.我可以停止并启动容器并保持数据,但我不确定为什么或如何发生这种情况.我可以在Dockerfile中看到官方postgres图像创建了一个卷(VOLUME / var / lib / postgresql / data),但我不确定这是继续工作的原因.你能否简要解释(或指出一个解释)这一切是如何运作的?
>架构:根据我的阅读,似乎这种应用程序最合适的架构是运行3个独立的容器.一个用于数据库,一个用于持久化数据库数据,一个用于节点应用程序.这是一个很好的方法吗?如何使用数据容器改进事物? AFAIK我当前的设置没有一个正常工作.
>还有什么我应该注意的吗?

谢谢

编辑:加入我的困惑,我刚刚从debian官方图片中运行了一个新容器(没有Dockerfile,只有docker run -i -t -d -name debtest debian / bin / bash).随着容器在后台运行,我使用docker attach debtest连接到它,然后继续apt-get install postgresql.一旦安装,我运行(仍然从容器内)psql并在认的postgres数据库中创建一个表,并用1个记录填充它.然后我退出shell,容器自动停止,因为shell不再运行了.我使用docker start debtest启动容器,然后附加到它,最后再次运行psql.我发现自第一次运行以来一切都持续存在.安装了Postgresql,我的桌子就在那里,而且我所插入的记录也在那里.我真的很困惑为什么我需要一个VOLUME来保存数据,因为这个快速测试没有使用一个,所有的东西都可以很好地工作.我在这里错过了什么吗?

再次感谢

1.

docker run –name some-postgres -e POSTGRES_PASSWORD=mysecretpassword
-d postgres

After I stop the container I cannot run it again using the above
command,because the container already exists.

正确.你将它命名为(–name some-postgres),因此在开始新的之前,必须删除旧的,例如docker rm -f some-postgres

So I start it using
docker start instead of docker run. Is this the normal way to do
things? I will generally use docker run the first time and docker
start every other time?

不,这对码头工作者来说并不正常. Docker进程容器通常应该是ephemeral,很容易丢弃并重新开始.

>

Persistance: … I can stop and start
the container and the data is persisted,although I’m not sure why or
how is this happening. …

那是因为你正在重用同一个容器.删除容器,数据消失了.

>

Architecture: from what I read,it seems that the most appropriate
architecture for this kind of app would be to run 3 separate
containers. One for the database,one for persisting the database
data,and one for the node app. Is this a good way to do it? How does
using a data container improve things? AFAIK my current setup is
working ok without one.

是的,这是通过为不同的问题分别设置容器的好方法.这在许多情况下都很方便,例如,当您需要升级postgres基础映像而不丢失数据时(特别是在数据容器开始发挥作用的情况下).

>

Is there anything else I should pay atention to?

熟悉docker基础知识后,您可以查看Docker compose或类似的工具,这些工具可以帮助您更轻松地运行多容器应用程序.

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...