flyway无法在docker-entrypoint-initdb.d脚本中连接到Postgres容器

时间:2018-08-06 02:26:38

标签: postgresql docker flyway

我正在尝试扩展postgres Docker映像,以可能(通过环境变量标志)在数据库初始化上执行飞行数据库迁移。我的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。也就是说,通过在我的pg_isready脚本中插入几个init_db.sh命令:

...
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。

3 个答案:

答案 0 :(得分:0)

我在浏览图像entry point脚本时发现了问题。图片的最新更改限制了Postgres在内部初始化期间仅监听Unix域套接字上的连接:https://github.com/docker-library/postgres/pull/440

答案 1 :(得分:0)

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

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

结果是上述代码永远循环。然后,我将其替换为以下代码,以重新启动数据库,以侦听localhost上的TCP / IP连接:

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

不是像这样重新启动数据库,更干净的解决方案是使用here解释的JDBC -socketFactory选项。

答案 2 :(得分:0)

的确,postgres docker正在监听unix套接字,例如。 /var/run/postgresql/.s.PGSQL.5432。但是不必强制服务器切换其侦听地址。 Postgres数据库URI允许连接字符串指向UNIX套接字。

参考:Connect to a database over a unix socket using SQLAlchemy

提供的示例:

export DATABASE_URL=postgres://user:password@/dbname?host=/path/to/unix/socket

我能够省略主机,并决定在我的 /docker-entrypoint-initdb.d / *。sh 脚本中使用此环境变量。请注意,@符号后面没有字符串,这里也没有主机查询字符串。您可能需要根据应用程序明确定义主机。

解决方案:

export DATABASE_URL="postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@/$POSTGRES_DB"
相关问题