postgresql Docker - 检查 postgres 是否准备好
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46516584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Docker - check if postgres is ready
提问by super.t
I have a Kong API Gatewaycontainer and a postgrescontainer and I need to check whether postgres has started up and ready from the Kong container before running the migrations. I was thinking of installing the postgres client utilities into a custom image based on the official Kong image using RUN yum install postgresql -y && yum clean all
in my Dockerfile and using either psql
or pg_isready
to achieve this. I've created a postgres user called polling
with an empty password specifically for checking the status of the server by these two utilities. Neither of them work.
我有一个Kong API 网关容器和一个postgres容器,我需要在运行迁移之前检查 postgres 是否已从 Kong 容器启动并准备就绪。我正在考虑将 postgres 客户端实用程序安装到基于RUN yum install postgresql -y && yum clean all
我的 Dockerfile 中使用的官方 Kong 映像的自定义映像中,并使用psql
或pg_isready
来实现这一点。我创建了polling
一个使用空密码调用的 postgres 用户,专门用于通过这两个实用程序检查服务器的状态。它们都不起作用。
I tried to execute these commands from the custom Kong image:
我尝试从自定义 Kong 图像执行这些命令:
psql. The command
psql -h postgres -U polling -w -c '\l'
fails with the errorpsql: fe_sendauth: no password supplied
. But the user has no password. What am I doing wrong? The full shell script checking whether the server is ready using psql is described here.pg_isready. I don't get how to install this utility separately into a custom image based on the official Kong image which in turn based on the
centos:7
image, thepostgresql
package doesn't includepg_isready
. Only these utilities are installed and can be found in/usr/bin
:pg_config
,pg_dump
,pg_dumpall
,pg_restore
,psql
. How to installpg_isready
? I don't want to have the full server installation in the Kong image.
psql。命令
psql -h postgres -U polling -w -c '\l'
失败并显示错误psql: fe_sendauth: no password supplied
。但是用户没有密码。我究竟做错了什么?此处描述了使用 psql 检查服务器是否准备就绪的完整 shell 脚本。pg_isready。我不知道如何将此实用程序单独安装到基于官方 Kong 映像的自定义映像中,而该映像又基于该
centos:7
映像,该postgresql
软件包不包含pg_isready
. 仅安装了这些实用程序,并且可以在以下位置找到/usr/bin
:pg_config
、pg_dump
、pg_dumpall
、pg_restore
、psql
。如何安装pg_isready
?我不想在 Kong 映像中安装完整的服务器。
回答by donmartin
We solve this with a simple TCP check on port 5432, without any PG tooling. We just use wait-for-it.sh
, and it works well. Postgres does not open the port until the server is actually ready to serve, so this is apparently fine.
我们通过对端口 5432 的简单 TCP 检查解决了这个问题,无需任何 PG 工具。我们只是使用wait-for-it.sh
,它运行良好。Postgres 在服务器真正准备好服务之前不会打开端口,所以这显然没问题。
Sample Dockerfile: https://github.com/apim-haufe-io/wicked.kong/blob/master/Dockerfile
示例 Dockerfile:https: //github.com/apim-haufe-io/wicked.kong/blob/master/Dockerfile
Corresponding start script (only the last line is interesting for this specific problem): https://github.com/apim-haufe-io/wicked.kong/blob/master/startup.sh
对应的启动脚本(对于这个特定问题,只有最后一行有趣):https: //github.com/apim-haufe-io/wicked.kong/blob/master/startup.sh
Snippet:
片段:
wait-for-it.sh -h $KONG_PG_HOST -p 5432 -t 30 -- kong start --run-migrations
Wait for it: https://github.com/vishnubob/wait-for-it
回答by super.t
My solution was to create a new image based on the official kong image and override the entrypoint like this:
我的解决方案是基于官方 kong 图像创建一个新图像并像这样覆盖入口点:
#!/usr/bin/env bash
set -e
# Disabling nginx daemon mode
export KONG_NGINX_DAEMON="off"
# Setting default prefix (override any existing variable)
export KONG_PREFIX="/usr/local/kong"
# Prepare Kong prefix
if [ "" = "/usr/local/openresty/nginx/sbin/nginx" ]; then
kong prepare -p "/usr/local/kong"
fi
#waiting for postgres
until psql --host=$KONG_PG_HOST --username=$POLLING_USER $POLLING_DATABASE -w &>/dev/null
do
echo "Waiting for PostgreSQL..."
sleep 1
done
echo "Postgres is ready, running the migrations..."
kong migrations up
echo "READY TO START UP KONG USING CMD" $@;
exec "$@"
回答by Thuthu
I tried wait-for-it, but it may be a problem to run it in docker container as in the docker image may not be installed nc (sometimes there is not even ping, telnet, curl..). So to check if the DB is up and running I have used HealthCheck in docker compose file what was checking return value of pg_isready, what is part of postgres database, so you do not need to install anything into docker images:
我试过等待它,但在 docker 容器中运行它可能会出现问题,因为在 docker 镜像中可能没有安装 nc(有时甚至没有 ping、telnet、curl ......)。因此,为了检查数据库是否已启动并正在运行,我在 docker compose 文件中使用了 HealthCheck 什么是检查 pg_isready 的返回值,什么是 postgres 数据库的一部分,因此您不需要在 docker 图像中安装任何东西:
version: '2.3'
services:
postgres-db:
image: postgresImage
healthcheck:
test: /usr/bin/pg_isready
interval: 5s
timeout: 10s
retries: 120
ports:
- '5432:5432'
aplication:
image: applicationImage
depends_on:
postgres-db:
condition: service_healthy