docker 无法让flyway通过unix套接字连接到postgres

9rbhqvlz  于 12个月前  发布在  Docker
关注(0)|答案(2)|浏览(106)

我尝试通过docker-entrypoint-initdb. d中的初始化脚本运行flyway迁移来扩展docker postgres镜像。
正如here所指出的,在初始化阶段,数据库只在unix套接字上列出。
我在运行flyway migrate时遇到了麻烦,因为我无法通过unix套接字连接到数据库,并且不断得到以下变体:

WARNING: Connection error: Connection to :5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. (Caused by Connection refused (Connection refused)) Retrying in 1 sec...

字符串
Dockerfile

FROM postgres:10.14-alpine

ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=password
ENV POSTGRES_DB=mydb
ENV FLYWAY_VERSION="6.0.8"
ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk/jre
ENV PATH $PATH:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
ENV JAVA_VERSION 8u181
ENV JAVA_ALPINE_VERSION 8.252.09-r0

RUN { \
    echo '#!/bin/sh'; \
    echo 'set -e'; \
    echo; \
    echo 'dirname "$(dirname "$(readlink -f "$(which javac || which java)")")"'; \
    } > /usr/local/bin/docker-java-home \
    && chmod +x /usr/local/bin/docker-java-home

RUN set -x \
    && apk add --no-cache \
    openjdk8-jre="$JAVA_ALPINE_VERSION" \
    && [ "$JAVA_HOME" = "$(docker-java-home)" ]

# Install Flyway
WORKDIR /flyway

RUN apk --no-cache add --update bash openssl \
    && wget https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/${FLYWAY_VERSION}/flyway-commandline-${FLYWAY_VERSION}.tar.gz \
    && tar -xzf flyway-commandline-${FLYWAY_VERSION}.tar.gz \
    && mv flyway-${FLYWAY_VERSION}/* . \
    && rm flyway-commandline-${FLYWAY_VERSION}.tar.gz \
    && ln -s /flyway/flyway /usr/local/bin/flyway

# Change ownership of flyway to postgres user
RUN chown postgres /flyway/flyway

# Copy flyway sql migrations and configuration
COPY flyway-sql-migrations/sql_migrations /flyway/sql_migrations
COPY test-utils/flyway.conf /flyway/flyway.conf

# Copy flyway initialization script
COPY test-utils/flyway-migrate.sh /docker-entrypoint-initdb.d/flyway-migrate.sh


flyway-migrate.sh

#!/bin/sh

flyway -configFiles=/flyway/flyway.conf migrate


flyway.conf

flyway.url=jdbc:postgresql://:5432/mydb
flyway.user=postgres
flyway.password=password
flyway.locations=filesystem:/flyway/sql_migrations
flyway.table=flyway_schema_history
flyway.connectRetries=60


在做了一些研究和咨询theseposts(同样的问题,问题确定,但没有给出答案),我尝试连接以下变体的flyway.url字符串,没有成功:

flyway.url=jdbc:postgresql://:5432/mydb
flyway.url=jdbc:postgresql:///mydb
flyway.url=jdbc:postgresql://%2Fvar%2Flib%2Fpostgresql%2F.s.PGSQL.5432/mydb
flyway.url=jdbc:postgresql://%2Fvar%2Frun%2Fpostgresql%2F.s.PGSQL.5432/mydb
flyway.url=jdbc:postgresql://:5432/mydb?host=/var/run/postgresql
flyway.url=jdbc:postgresql:///mydb?host=/var/run/postgresql


有没有人对如何配置JDBC URL有任何建议,以便我可以在初始化阶段连接flyway到postgresql?
谢谢你,谢谢

pgky5nke

pgky5nke1#

我最近也遇到了同样的问题,我的解决方案是使用容器的默认网关IP。
要找到它,可以在迁移脚本中使用以下命令:

ip route | awk '/default/ { print $3 }'

字符串
首先安装iproute2:

apt-get install -y iproute2

piok6c0g

piok6c0g2#

问题是,在初始化阶段,PostgreSQL数据库默认只监听UNIX套接字,而这是不能通过Flyway使用的TCP/IP网络连接访问的。
解决这个问题最简单的方法是在flyway-migrate.sh脚本中手动重启数据库,并使用listen_addresses='localhost'启动它,这将配置Postgres以侦听TCP/IP网络接口上的连接。
所以,flyway-migrate.sh是:

#!/bin/bash

echo '----    Restarting DB to enable IPv4 database listener    ----'
pg_ctl -D "$PGDATA" -m fast -w stop
pg_ctl -D "$PGDATA" \
            -o "-c listen_addresses='localhost'" \
            -w start
echo '-----------------    Restart completed    ---------------------'

flyway -configFiles=/flyway/flyway.conf migrate

字符串
在重新启动之前,日志显示服务器只监听Unix套接字:

2024-01-04 15:47:40.876 UTC [47] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"


重新启动后,日志表明服务器现在正在侦听IPv4和Unix套接字:

2024-01-04 15:47:41.141 UTC [63] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2024-01-04 15:47:41.141 UTC [63] LOG:  could not bind IPv6 address "::1": Cannot assign requested address
2024-01-04 15:47:41.142 UTC [63] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"

相关问题