我应该如何使用Postgresql docker映像/容器? [英] How am I supposed to use a Postgresql docker image/container?

查看:58
本文介绍了我应该如何使用Postgresql docker映像/容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新手.我仍在努力将所有事情都包裹住.

I'm new to docker. I'm still trying to wrap my head around all this.

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

I'm building a node application (REST api), using Postgresql to store my data.

我花了几天时间学习有关docker的知识,但是我不确定我是否按照自己的方式做事.

I've spent a few days learning about docker, but I'm not sure whether I'm doing things the way I'm supposed to.

这是我的问题:

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

  1. I'm using the official docker postgres 9.5 image as base to build my own (my Dockerfile only adds plpython on top of it, and installs a custom python module for use within plpython stored procedures). I created my container as suggedsted by the postgres image docs:

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

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

停止容器后,无法使用上述命令再次运行它,因为该容器已存在.所以我使用docker start而不是docker run来启动它.这是正常的做事方式吗?我通常会使用docker第一次运行,而docker每隔一次启动?

After I stop the container I cannot run it again using the above command, because the container already exists. 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?

持久性:我创建了一个数据库,并将其填充到正在运行的容器中.我使用pgadmin3进行连接.尽管我不确定为什么或如何发生,但我可以停止并启动容器并保留数据.我可以在官方postgres映像的Dockerfile中看到已创建卷(VOLUME/var/lib/postgresql/data),但是我不确定这是持久性起作用的原因.您能简要解释一下(或指向一个解释)这一切如何工作吗?

Persistance: I created a database and populated it on the running container. I did this using pgadmin3 to connect. I can stop and start the container and the data is persisted, although I'm not sure why or how is this happening. I can see in the Dockerfile of the official postgres image that a volume is created (VOLUME /var/lib/postgresql/data), but I'm not sure that's the reason persistance is working. Could you please briefly explain (or point to an explanation) about how this all works?

体系结构:从我阅读的内容来看,似乎最适合此类应用程序的体系结构是运行3个独立的容器.一种用于数据库,一种用于持久存储数据库数据,另一种用于节点应用程序.这是一个好方法吗?使用数据容器如何改善情况?AFAIK,我当前的设置没有一个就可以正常工作.

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.

还有什么我需要注意的吗?

Is there anything else I should pay atention to?

谢谢

令我感到困惑的是,我只是从debian官方映像中运行了一个新容器(没有Dockerfile,只有docker run -i -t -d --namebtest debian/bin/bash).在容器在后台运行的情况下,我使用docker attachbtstest附加到了该容器,然后继续进行apt-get install postgresql的安装.安装完成后,我运行了psql(仍从容器中),并在默认的postgres数据库中创建了一个表,并用1条记录填充该表.然后我退出了外壳程序,由于该外壳程序不再运行,因此容器自动停止.我使用docker startbtnest重新启动了容器,然后将其连接到容器,最后再次运行psql.我发现自首次运行以来,所有内容都将保留下来.安装了Postgresql,我的表在那里,并且当然,我插入的记录也在那里.我真的很困惑为什么我需要一个VOLUME来保存数据,因为这种快速测试并没有使用任何方法,而所有apears都可以正常工作.我在这里想念什么吗?

adding to my confusion, I just ran a new container from the debian official image (no Dockerfile, just docker run -i -t -d --name debtest debian /bin/bash). With the container running in the background, I attached to it using docker attach debtest and the proceeded to apt-get install postgresql. Once installed I ran (still from within the container) psql and created a table in the default postgres database, and populated it with 1 record. Then I exited the shell and the container stopped automatically since the shell wasn't running anymore. I started the container againg using docker start debtest, then attached to it and finally run psql again. I found everything is persisted since the first run. Postgresql is installed, my table is there, and offcourse the record I inserted is there too. I'm really confused as to why do I need a VOLUME to persist data, since this quick test didn't use one and everything apears to work just fine. Am I missing something here?

再次感谢

推荐答案

1.

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

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

Correct. You named it (--name some-postgres) hence before starting a new one, the old one has to be deleted, e.g. docker rm -f some-postgres

所以我开始使用docker start而不是docker run.这是正常的做法吗事物?我通常会在第一次运行docker时使用docker每隔一次开始?

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而言,这绝非正常.Docker进程容器通常应该是

No, it is by no means normal for docker. Docker process containers are supposed normally to be ephemeral, that is easily thrown away and started anew.

持久性:...我可以停止并开始容器和数据被保留,尽管我不确定为什么或这是怎么回事....

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

那是因为您正在重复使用相同的容器.取出容器,数据消失了.

That's because you are reusing the same container. Remove the container and the data is gone.

建筑:根据我的阅读,看来最合适这种应用的架构是要单独运行3个容器.一种用于数据库,一种用于持久化数据库数据,一个用于节点应用程序.这是一个好方法吗?如何使用数据容器可以改善事情吗?AFAIK我当前的设置是没有一个人就可以正常工作.

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基本映像时(尤其是在数据容器开始发挥作用的情况下).

Yes, this is the good way to go by having separate containers for separate concerns. This comes in handy in many cases, say when for example you need to upgrade the postgres base image without losing your data (that's in particular where the data container starts to play its role).

还有什么我需要注意的吗?

Is there anything else I should pay atention to?

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

When acquainted with the docker basics, you may take a look at Docker compose or similar tools that will help you to run multicontainer applications easier.

这篇关于我应该如何使用Postgresql docker映像/容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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