bash - 如何在 ENTRYPOINT 等待 postgres 启动?

标签 bash postgresql docker

在继续执行 nosetests 之前等待 postgres 在我的 ENTRYPOINT 内完全启动的最佳方法是什么?

现在,我已将机器上的启动时间设置为 50 秒左右。所以我只睡了60秒。这感觉不好,因为在另一台机器上运行时可能无法正常工作。

ENTRYPOINT \
           runuser -l postgres -c '/usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf & ' && \
           sleep 60 && \
           nosetests --verbose --cover-erase --with-coverage --cover-package=stalker

这是启动postgres的输出:

2017-02-13 13:46:49.541 UTC [9] LOG:  database system was interrupted; last known up at 2017-02-13 12:53:23 UTC
2017-02-13 13:47:37.951 UTC [9] LOG:  database system was not properly shut down; automatic recovery in progress
2017-02-13 13:47:37.994 UTC [9] LOG:  redo starts at 0/1783EA0
2017-02-13 13:47:37.995 UTC [9] LOG:  record with zero length at 0/17841E8
2017-02-13 13:47:37.995 UTC [9] LOG:  redo done at 0/17841B8
2017-02-13 13:47:37.995 UTC [9] LOG:  last completed transaction was at log time 2017-02-13 12:53:23.731984+00
2017-02-13 13:47:38.384 UTC [9] LOG:  MultiXact member wraparound protections are now enabled
2017-02-13 13:47:38.387 UTC [7] LOG:  database system is ready to accept connections
2017-02-13 13:47:38.387 UTC [13] LOG:  autovacuum launcher started

请,我明白这违反了在 ENTRYPOINT 中运行多个命令的惯例。在这种情况下,我有充分的理由这样做。

最佳答案

感谢@zeppelin 建议pg_isready。我最终使用了这个:

#!/bin/bash
# wait-for-postgres.sh

set -e

cmd="$@"
timer="5"

until runuser -l postgres -c 'pg_isready' 2>/dev/null; do
  >&2 echo "Postgres is unavailable - sleeping for $timer seconds"
  sleep $timer
done

>&2 echo "Postgres is up - executing command"
exec $cmd

我在我的 ENTRYPOINT 中使用它:

ENTRYPOINT \

            # Start PostgreSQL
            runuser -l postgres -c '/usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf & ' && \

            # Exectute tests when db is up
            ./wait-for-postgres.sh nosetests --verbose --cover-erase --with-coverage --cover-package=stalker

关于bash - 如何在 ENTRYPOINT 等待 postgres 启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42206131/

相关文章:

docker - 如何创建正确的.lando.yml自定义文件?

bash - OSX 恶意终端命令(冒号、括号、大括号、apersand 等)

bash - 即使变量未设置、为空或仅包含空格和制表符,[[ $var -eq 0 ]] 也会返回 true

postgresql - 当我的注册表中不存在用户 DSN 时,如何删除它们

python - SQL 重新格式化/更改 TableView ,将重复内容作为列

PostgreSQL {调用更新集...}获取 "syntax error at or near SET"

docker - 将数据保存为Docker镜像的一部分吗?

regex - 将 sed 一行代码转换为 perl

linux - 如何创建备份脚本来比较日期和删除最旧的文件

python setup.py install 在 Alpine 图像上无法正常工作