无法通过unix套接字获取flyway连接到postgres

问题描述 投票:0回答:1

我正在通过 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

在做了一些研究和咨询这些帖子之后(同样的问题,问题已确定,但没有给出答案),我尝试连接以下版本的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 吗?

谢谢!

postgresql docker flyway
1个回答
0
投票

我最近遇到了完全相同的问题。我的解决方案是使用容器的默认网关IP。

要找到它,可以在迁移脚本中使用以下命令:

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

作为先决条件安装 iproute2:

apt-get install -y iproute2
© www.soinside.com 2019 - 2024. All rights reserved.