修复thingsboard官方镜像容易出现启动失败的BUG

发布于:2023-09-14 ⋅ 阅读:(234) ⋅ 点赞:(0)

thingsboard的官方镜像thingsboard/tb-postgres,非常坑,除了第一次启动容器,后面启动,基本上都会出现启动失败的情况,部分错误日志如下

2022-03-01 00:59:16,036 [main] WARN  o.h.e.j.e.i.JdbcEnvironmentInitiator - HHH000342: Could not obtain connection to query metadata
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303)

明显是连接数据异常。之前的办法,是删除数据库文件再来一次,但是保存的数据都没有了,还得再配置一遍,非常麻烦。后面检查他的启动脚本start-db.sh,发现了问题,

exec setsid nohup postgres >> ${PGLOG}/postgres.log 2>&1 &

上面是后台去启动postgres, 就立刻去启动thingboard程序,如果数据库没有启动好,就会报连接异常。怎么办了,我给加了下面的判断

sleep 2
while ! psql -U thingsboard -d postgres -c "select version() "
do
sleep 1
done

echo "数据库启动成功"

一个循环去检查postgres 状态,重新打包镜像,果然就没有出现连接postgres的情况。完整dockerfile详见我的码云

开源代码