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

标签 postgresql docker dockerfile

我是 docker 新手。我仍在努力解决所有这些问题。

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

我花了几天时间学习 docker,但我不确定我是否按照我应该的方式做事。

所以这是我的问题:

  1. 我使用官方的 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 start?

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

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

  4. 还有什么需要注意的吗?

谢谢

编辑:让我更加困惑的是,我刚刚从 debian 官方镜像运行了一个新容器(没有 Dockerfile,只是 docker run -i -t -d --name debian/bin/bash)。随着容器在后台运行,我使用 docker attach debest 附加到它,然后继续 apt-get install postgresql。安装后,我运行(仍然从容器中)psql 并在默认的 postgres 数据库中创建了一个表,并用 1 条记录填充它。然后我退出 shell,容器自动停止,因为 shell 不再运行。我使用 docker start debest 重新启动容器,然后附加到它,最后再次运行 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 来说绝不是正常的。 Docker 进程容器通常应该是 ephemeral ,很容易被扔掉并重新开始。

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

那是因为您正在重复使用同一个容器。移除容器,数据就没有了。

  1. 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 基础镜像而不丢失数据时(尤其是数据容器开始发挥作用的地方)。

  1. Is there anything else I should pay atention to?

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

关于postgresql - 我应该如何使用 Postgresql docker 镜像/容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35232242/

相关文章:

PostgreSQL - 更新查询中的语法错误

python - Django 模型一个外键到多个表

sql - Postgresql COPY 和\复制函数

java - 将命令行参数传递给在 Docker 中运行的 Java 应用程序 (Spring Boot)

mysql - SQL : How to select values from multiple tables that contains specified column

docker - 无法从我的 Docker 容器内部连接到主机的 localhost

docker - GitLab CI 对 Docker 构建作业的无效参数

google-app-engine - 更快的 Google App Engine 托管 VM 部署(Python 兼容环境)?

python - 在 Docker 容器中找不到模块

docker - 如何从 docker run 命令给 Dockerfile 输入参数