I am trying to set up a custom Dockerfile for postgres that runs flyway migrations on startup. So far I've managed to get everything built, installed and executable, but I am unable to get flyway to connect to the postgres database inside the docker container on startup due to connection errors.
Before you tell me to set it up in a docker-compose, file I would like to add that I am doing this so that I can use the docker image in gitlab ci/cd as a service container, so using docker-compose is not an option.
Here is my dockerfile
FROM postgres:12 AS db
WORKDIR /home
# Install wget
RUN apt-get update -y \
&& apt-get install wget -y \
&& apt-get install sudo -y \
&& apt-get install lsof
RUN usermod -aG sudo postgres
RUN bash -c 'echo "postgres ALL=(ALL:ALL) NOPASSWD: ALL" | (EDITOR="tee -a" visudo)'
# Install latest version of flyway 7
RUN wget -qO- https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/7.9.2/flyway-commandline-7.9.2-linux-x64.tar.gz | tar xvz && \
ln -s `pwd`/flyway-7.9.2/flyway /usr/local/bin
# Copy database migrations for flyway
COPY reds/migrations migrations
# Set environment variables for flyway
ENV FLYWAY_LOCATIONS="/home/migrations"
ENV FLYWAY_SCHEMAS="reds"
ENV FLYWAY_CONNECT_RETRIES=5
ENV FLYWAY_BASELINE_ON_MIGRATE=false
ENV FLYWAY_OUT_OF_ORDER=false
# Make postgres startup script run flyway migrations
COPY reds/init.sh /docker-entrypoint-initdb.d/init.sh
RUN chmod +x /docker-entrypoint-initdb.d/init.sh && \
chown -R postgres /docker-entrypoint-initdb.d
EXPOSE 5432
CMD ["postgres"]
And the following is the script that is executed on container startup
#!/bin/bash
set -eou pipefail
# I've tried localhost, 0.0.0.0 and 127.0.0.1
host="$(hostname -i)"
sudo flyway migrate \
-url="jdbc:postgresql://$host:5432/$POSTGRES_DB" \
-schemas="$FLYWAY_SCHEMAS" \
-locations="$FLYWAY_LOCATIONS" \
-connectRetries="$FLYWAY_CONNECT_RETRIES" \
-baselineOnMigrate="$FLYWAY_BASELINE_ON_MIGRATE" \
-outOfOrder="$FLYWAY_OUT_OF_ORDER" \
-user="$POSTGRES_USER" \
-password="$POSTGRES_PASSWORD"
My build command:
$ docker build -t reds-docker:latest -f "Dockerfile.reds" .
My run command:
$ docker run --rm \
--env POSTGRES_USER="postgres" \
--env POSTGRES_PASSWORD="postgres" \
--env POSTGRES_DB="postgres" \
reds-docker:latest
I get the following error for flyway trying to connect to the database when I run the container:
WARNING: Connection error: Connection to 172.17.0.2: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))
Full log from startup:
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 dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgresql/data -l logfile start
initdb: 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.
waiting for server to start....2023-02-06 13:36:59.228 UTC [48] LOG: starting PostgreSQL 12.13 (Debian 12.13-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2023-02-06 13:36:59.229 UTC [48] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-02-06 13:36:59.237 UTC [49] LOG: database system was shut down at 2023-02-06 13:36:59 UTC
2023-02-06 13:36:59.239 UTC [48] LOG: database system is ready to accept connections
done
server started
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sh
WARNING: This version of Flyway is out of date. Upgrade to Flyway 9.14.1:https://flywaydb.org/documentation/learnmore/staying-up-to-date/?ref=v7.9.2_cmd-line
Flyway Community Edition 7.9.2 by Redgate
WARNING: Connection error: Connection to 172.17.0.2: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...
WARNING: Connection error: Connection to 172.17.0.2: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 2 sec...