fre*_*rik 11 postgresql bash docker
在继续执行之前等待postgres完全启动我的最佳方法是什么?ENTRYPOINTnosetests
现在我把我的机器上的启动时间定在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
Run Code Online (Sandbox Code Playgroud)
这是启动的输出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
Run Code Online (Sandbox Code Playgroud)
请,我明白这违反了运行多个命令的惯例ENTRYPOINT.在这种情况下,我有充分的理由这样做.
fre*_*rik 13
感谢@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
Run Code Online (Sandbox Code Playgroud)
我在我的使用中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
Run Code Online (Sandbox Code Playgroud)
我通常会使用一个小的“ tcp-port-wait”脚本,如下所示:
#!/bin/bash
set -e
if [ -z "$1" -o -z "$2" ]
then
echo "tcp-port-wait - block until specified TCP port becomes available"
echo "Usage: ntcp-port-wait HOST PORT"
exit 1
fi
echo Waiting for port $1:$2 to become available...
while ! nc -z $1 $2 2>/dev/null
do
let elapsed=elapsed+1
if [ "$elapsed" -gt 90 ]
then
echo "TIMED OUT !"
exit 1
fi
sleep 1;
done
echo "READY !"
Run Code Online (Sandbox Code Playgroud)
等待直到特定服务启动并准备就绪。
如果使用Postgresql,它将监听的默认端口为5432,因此命令为:
tcp-port-wait localhost 5432
Run Code Online (Sandbox Code Playgroud)
这将一直阻塞,直到Postgresql服务准备好在容器内的环回接口上的:5432上为连接提供服务时(在ENTRYPOINT脚本的上下文中运行时)。
您当然必须通过向Dockerfile添加这样的一行来将此脚本复制到您的容器中:
COPY tcp-port-wait /usr/local/bin/
Run Code Online (Sandbox Code Playgroud)
才能使用它。
并安装netcat实用程序。
您也可以将其用于其他类型的服务,例如Tomcat或Mysql。
并且,如果需要,还可以在“容器外部”等待,如下所示:
tcp-port-wait localhost 5432
Run Code Online (Sandbox Code Playgroud)
请注意,当然还有其他方法可以执行此操作,例如,通过使用一些编排工具,例如带有healthcheck指令的docker-compose或容器本身内部的某些进程管理器。
| 归档时间: |
|
| 查看次数: |
4450 次 |
| 最近记录: |