Docker – 检查postgres是否准备就绪
我有一个Kong API网关容器和一个postgres容器,我需要在运行迁移之前检查postgres是否已经启动并准备好了Kong容器。 我正在考虑使用RUN yum install postgresql -y && yum clean all
在我的Dockerfile中RUN yum install postgresql -y && yum clean all
清理并使用psql
或pg_isready
来实现此pg_isready
,将postgres客户端实用程序安装到基于官方Kong映像的自定义映像中。 我创build了一个名为polling
的postgres用户,其中使用了一个空密码,专门用于通过这两个实用程序来检查服务器的状态。 他们都没有工作。
我试图从自定义的Kong镜像执行这些命令:
-
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的映像中安装完整的服务器。
我们通过5432端口的简单TCP检查来解决这个问题,而不需要任何PG工具。 我们只是使用wait-for-it.sh
,它运作良好。 Postgres不会打开端口,直到服务器实际上准备好服务,所以这显然是好的。
示例Dockerfile: https : //github.com/apim-haufe-io/wicked.kong/blob/master/Dockerfile
对应的启动脚本(只有最后一行对这个特定的问题很有意思): https : //github.com/apim-haufe-io/wicked.kong/blob/master/startup.sh
片段:
wait-for-it.sh -h $KONG_PG_HOST -p 5432 -t 30 -- kong start --run-migrations
等待: https : //github.com/vishnubob/wait-for-it
我的解决scheme是创build一个基于官方形象的新形象,并重写像这样的入口点:
#!/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 [ "$1" = "/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 "$@"