I am extending the docker postgres image by attempting to run flyway migrations via an initialization script in docker-entrypoint-initdb.d.
As pointed out here, during the initialization phase the database is listing only on the unix socket.
I am having trouble running flyway migrate as I am unable to connect to the database via unix socket, and just keep getting variations of:
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
After doing some researching and consulting these posts (same issue, problem identified but no answer was given), I have tried connecting with the following variations of flyway.url strings, with no success:
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
Does anyone have any recommendations on how to configure the JDBC url so that I can connect flyway to postgresql during the initialization phase?
Thank you!