我尝试通过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?
谢谢你,谢谢
2条答案
按热度按时间pgky5nke1#
我最近也遇到了同样的问题,我的解决方案是使用容器的默认网关IP。
要找到它,可以在迁移脚本中使用以下命令:
字符串
首先安装iproute2:
型
piok6c0g2#
问题是,在初始化阶段,PostgreSQL数据库默认只监听UNIX套接字,而这是不能通过Flyway使用的TCP/IP网络连接访问的。
解决这个问题最简单的方法是在
flyway-migrate.sh
脚本中手动重启数据库,并使用listen_addresses='localhost'
启动它,这将配置Postgres以侦听TCP/IP网络接口上的连接。所以,
flyway-migrate.sh
是:字符串
在重新启动之前,日志显示服务器只监听Unix套接字:
型
重新启动后,日志表明服务器现在正在侦听IPv4和Unix套接字:
型