我应该如何使用 Postgresql docker image/container?
How am I supposed to use a Postgresql docker image/container?
我是 docker 的新手。我仍在努力解决所有这些问题。
我正在构建一个节点应用程序 (REST api),使用 Postgresql 来存储我的数据。
我花了几天时间了解 docker,但我不确定我是否按照我应该的方式做事。
所以这是我的问题:
我正在使用官方 docker postgres 9.5 映像作为构建我自己的映像的基础(我的 Dockerfile 仅在其上添加了 plpython,并安装了自定义python 在 plpython 存储过程中使用的模块)。我按照 postgres 图像文档的建议创建了我的容器:
docker 运行 --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
停止容器后,我无法使用上述命令再次运行它,因为容器已经存在。所以我使用 docker 开始而不是 docker 运行 来启动它。这是正常的做事方式吗?我一般会第一次用docker运行,以后每隔一段时间就用docker?
持久性:我创建了一个数据库并将其填充到 运行ning 容器中。我使用 pgadmin3 进行连接。我可以停止和启动容器,并且数据会保留下来,尽管我不确定这是为什么或如何发生的。我可以在官方 postgres 图像的 Dockerfile 中看到创建了一个卷 (VOLUME /var/lib/postgresql/data),但我不确定这就是持久性起作用的原因。您能否简要解释一下(或指出解释)这一切是如何运作的?
架构:据我了解,这种应用程序最合适的架构似乎是 运行 3 个独立的容器。一个用于数据库,一个用于持久化数据库数据,一个用于节点应用程序。这是一个好方法吗?使用数据容器如何改善事情?据我所知,我目前的设置没有一个也能正常工作。
还有什么需要注意的吗?
谢谢
编辑:更让我困惑的是,我只是 运行 来自 debian 官方镜像的一个新容器(没有 Dockerfile,只是 docker 运行 -i -t -d --名称 debest debian /bin/bash)。在后台使用容器 运行ning 后,我使用 docker attach debtest 附加到它,然后继续使用 apt-get install postgresql。安装后,我 运行(仍然在容器内)psql 并在默认的 postgres 数据库中创建了一个 table,并用 1 条记录填充它。然后我退出 shell 并且容器自动停止,因为 shell 不再是 运行ning。我再次使用 docker start debtest 启动容器,然后附加到它,最后再次 运行 psql。我发现自第一个 运行 以来一切都存在。安装了 Postgresql,我的 table 在那里,当然我插入的记录也在那里。我真的很困惑为什么我需要一个 VOLUME 来保存数据,因为这个快速测试没有使用一个并且一切看起来都可以正常工作。我在这里遗漏了什么吗?
再次感谢
简短:
- 您从官方 postgres 映像中获得的是一个随时可用的 postgres 安装以及一些可以通过环境变量配置的小技巧。使用
docker run
您可以创建一个容器。容器生命周期命令是 docker start/stop/restart/rm
是的,这是 Docker 的方式。
- 卷内的所有内容都已保留。每个容器都可以有任意数量的卷。卷是在 Docker 文件、父 Docker 文件或通过命令
docker run ... -v /yourdirectoryA -v /yourdirectoryB ...
定义的目录。 docker rm
卷外的所有内容都丢失了。 docker rm -v
包括卷在内的所有内容都丢失了
- 展示比解释容易。使用 Github 上的 Docker 命令查看此自述文件,阅读我如何使用 Jira 的官方 PostgreSQL 图像,并将 NGINX 添加到组合中:Jira with Docker PostgreSQL。此外,数据容器是一种廉价技巧,可以在不移动持久化数据的情况下移除、重建和更新容器。
- 恭喜,你已经掌握了基础知识!继续!尝试 docker-compose 以更好地管理那些讨厌的
docker run ...
-commands 并能够管理多容器和数据容器。
注意:您需要阻塞线程才能保留容器 运行!此命令必须在 Docker 文件中明确设置,请参阅 CMD,或者在 docker run -d ... /usr/bin/myexamplecommand
命令末尾给出。如果您的命令是非阻塞的,例如/bin/bash
,那么容器总是会在执行命令后立即停止。
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绝不正常。 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 或类似工具,它们将帮助您更轻松地 运行 多容器应用程序。
我是 docker 的新手。我仍在努力解决所有这些问题。
我正在构建一个节点应用程序 (REST api),使用 Postgresql 来存储我的数据。
我花了几天时间了解 docker,但我不确定我是否按照我应该的方式做事。
所以这是我的问题:
我正在使用官方 docker postgres 9.5 映像作为构建我自己的映像的基础(我的 Dockerfile 仅在其上添加了 plpython,并安装了自定义python 在 plpython 存储过程中使用的模块)。我按照 postgres 图像文档的建议创建了我的容器:
docker 运行 --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
停止容器后,我无法使用上述命令再次运行它,因为容器已经存在。所以我使用 docker 开始而不是 docker 运行 来启动它。这是正常的做事方式吗?我一般会第一次用docker运行,以后每隔一段时间就用docker?
持久性:我创建了一个数据库并将其填充到 运行ning 容器中。我使用 pgadmin3 进行连接。我可以停止和启动容器,并且数据会保留下来,尽管我不确定这是为什么或如何发生的。我可以在官方 postgres 图像的 Dockerfile 中看到创建了一个卷 (VOLUME /var/lib/postgresql/data),但我不确定这就是持久性起作用的原因。您能否简要解释一下(或指出解释)这一切是如何运作的?
架构:据我了解,这种应用程序最合适的架构似乎是 运行 3 个独立的容器。一个用于数据库,一个用于持久化数据库数据,一个用于节点应用程序。这是一个好方法吗?使用数据容器如何改善事情?据我所知,我目前的设置没有一个也能正常工作。
还有什么需要注意的吗?
谢谢
编辑:更让我困惑的是,我只是 运行 来自 debian 官方镜像的一个新容器(没有 Dockerfile,只是 docker 运行 -i -t -d --名称 debest debian /bin/bash)。在后台使用容器 运行ning 后,我使用 docker attach debtest 附加到它,然后继续使用 apt-get install postgresql。安装后,我 运行(仍然在容器内)psql 并在默认的 postgres 数据库中创建了一个 table,并用 1 条记录填充它。然后我退出 shell 并且容器自动停止,因为 shell 不再是 运行ning。我再次使用 docker start debtest 启动容器,然后附加到它,最后再次 运行 psql。我发现自第一个 运行 以来一切都存在。安装了 Postgresql,我的 table 在那里,当然我插入的记录也在那里。我真的很困惑为什么我需要一个 VOLUME 来保存数据,因为这个快速测试没有使用一个并且一切看起来都可以正常工作。我在这里遗漏了什么吗?
再次感谢
简短:
- 您从官方 postgres 映像中获得的是一个随时可用的 postgres 安装以及一些可以通过环境变量配置的小技巧。使用
docker run
您可以创建一个容器。容器生命周期命令是docker start/stop/restart/rm
是的,这是 Docker 的方式。 - 卷内的所有内容都已保留。每个容器都可以有任意数量的卷。卷是在 Docker 文件、父 Docker 文件或通过命令
docker run ... -v /yourdirectoryA -v /yourdirectoryB ...
定义的目录。docker rm
卷外的所有内容都丢失了。docker rm -v
包括卷在内的所有内容都丢失了
- 展示比解释容易。使用 Github 上的 Docker 命令查看此自述文件,阅读我如何使用 Jira 的官方 PostgreSQL 图像,并将 NGINX 添加到组合中:Jira with Docker PostgreSQL。此外,数据容器是一种廉价技巧,可以在不移动持久化数据的情况下移除、重建和更新容器。
- 恭喜,你已经掌握了基础知识!继续!尝试 docker-compose 以更好地管理那些讨厌的
docker run ...
-commands 并能够管理多容器和数据容器。
注意:您需要阻塞线程才能保留容器 运行!此命令必须在 Docker 文件中明确设置,请参阅 CMD,或者在 docker run -d ... /usr/bin/myexamplecommand
命令末尾给出。如果您的命令是非阻塞的,例如/bin/bash
,那么容器总是会在执行命令后立即停止。
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绝不正常。 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 或类似工具,它们将帮助您更轻松地 运行 多容器应用程序。