gpt4 book ai didi

postgresql - flyway 无法连接到 docker-entrypoint-initdb.d 脚本中的 postgres 容器

转载 作者:行者123 更新时间:2023-11-29 11:37:19 25 4
gpt4 key购买 nike

我正在尝试扩展 postgres Docker图像可能(通过环境变量标志)在 DB init 上执行 flyway DB 迁移。我的 Dockerfile 在这里:

FROM postgres:9.6

# Install curl and java (for Flyway)
RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends ca-certificates curl openjdk-8-jre

# Install Flyway
ENV FLYWAY_VERSION 4.2.0
ENV FLYWAY_INSTALL_DIR /usr/src/flyway
ENV FLYWAY_CONF ${FLYWAY_INSTALL_DIR}/flyway-${FLYWAY_VERSION}/conf/flyway.conf
ENV FLYWAY_EXE ${FLYWAY_INSTALL_DIR}/flyway-${FLYWAY_VERSION}/flyway
RUN mkdir -p ${FLYWAY_INSTALL_DIR} && \
curl -L https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/${FLYWAY_VERSION}/flyway-commandline-${FLYWAY_VERSION}.tar.gz | \
tar -xzC ${FLYWAY_INSTALL_DIR} && \
chmod +x ${FLYWAY_EXE}

# Copy migration scripts
ENV MIGRATIONS_LOCATION /flyway/migrations
COPY migrations $MIGRATIONS_LOCATION

COPY init_db.sh /docker-entrypoint-initdb.d/init_db.sh

使用我的 init_db.sh 启动脚本:

#!/bin/bash
set -e

RUN_MIGRATIONS="${RUN_MIGRATIONS:-false}"
DB_URL="jdbc:postgresql://localhost:5432/$DB_NAME"

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE $DB_NAME;
EOSQL

if [ "$RUN_MIGRATIONS" == "true" ]; then
echo "running migrations ..."
${FLYWAY_EXE} -user=$POSTGRES_USER -password=$POSTGRES_PASSWORD -url=$DB_URL -locations="filesystem:$MIGRATIONS_LOCATION" migrate
fi

但是,当使用 RUN_MIGRATIONS=true 运行容器时,flyway 无法连接到 postgres:

docker build . -t postgres-flyway && docker run -e DB_NAME=db -e RUN_MIGRATIONS=true -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres postgres-flyway
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

pg_ctl -D /var/lib/postgresql/data -l logfile start

waiting for server to start....LOG: database system was shut down at 2018-08-06 02:19:32 UTC
LOG: MultiXact member wraparound protections are now enabled
LOG: autovacuum launcher started
LOG: database system is ready to accept connections
done
server started
ALTER ROLE


/usr/local/bin/docker-entrypoint.sh: sourcing /docker-entrypoint-initdb.d/init_db.sh
CREATE DATABASE
running migrations ...
Flyway 4.2.0 by Boxfuse

ERROR:
Unable to obtain Jdbc connection from DataSource (jdbc:postgresql://localhost:5432/db) for user 'postgres': Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL State : 08001
Error Code : 0
Message : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

postgres 图像在端口 5432 上运行 postgres(像往常一样)所以我不知道为什么 flyway 无法通过 localhost:5432 连接到 postgres。

我还注意到,在此上下文中,pg_isready 声明 postgres 正在接受连接,但是当将主机名指定为 localhost127.0.0.1 时它也无法到达 postgres。也就是说,通过在我的 init_db.sh 脚本中插入一些 pg_isready 命令:

...
pg_isready
pg_isready -p 5432
pg_isready -h localhost -p 5432
...

我在 postgres init 上看到以下日志输出:

...
/var/run/postgresql:5432 - accepting connections
/var/run/postgresql:5432 - accepting connections
localhost:5432 - no response
...

我怀疑我已经达到了 postgres 初始化上下文的限制,但我想了解为什么在初始化时无法通过 localhost/127.0.0.1:5432 访问 postgres .

最佳答案

在基于 postgres:10.5 图像为我的数据库创建 docker 图像时,我在运行 flyway 时遇到了同样的问题。在运行 flyway 之前,我将以下内容添加到我的 entrypoint.sh 中,以确认我看到的问题是由@Nick Maraston 在他的回答中发布的 docker-entrypoint.sh 更改引起的:

echo "$(date) - waiting for database to start"
while ! pg_isready -h localhost -p 5432 -d $POSTGRES_DB
do
echo "$(date) - waiting for database to start"
sleep 10
done

结果是上面的代码一直循环下去。然后我将其替换为以下代码以重新启动数据库以监听本地主机上的 TCP/IP 连接:

pg_ctl -D "$PGDATA" -m fast -w stop
pg_ctl -D "$PGDATA" \
-o "-c listen_addresses='localhost'" \
-w start

与其像这样重启数据库,更简洁的解决方案是使用 JDBC -socketFactory 选项解释 here .

关于postgresql - flyway 无法连接到 docker-entrypoint-initdb.d 脚本中的 postgres 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51699847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com