Skip to content

Instantly share code, notes, and snippets.

@hartmannr76
Last active October 23, 2023 20:10
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hartmannr76/5469e55ca910e4e84bbe09dbb39afb03 to your computer and use it in GitHub Desktop.
Save hartmannr76/5469e55ca910e4e84bbe09dbb39afb03 to your computer and use it in GitHub Desktop.
Flyway/Postgres Migration with Docker

Running

docker-compose -f docker-compose.migration.yml run flyway /opt/scripts/migrate.sh

version: '2'
services:
db:
image: postgres
environment:
- POSTGRES_USER=foo
- POSTGRES_PASSWORD=foobar
- POSTGRES_DB=baz
ports:
- '5432:5432'
volumes:
- ./db-data:/var/lib/postgresql/data
flyway:
image: flyway:migration
build:
context: ./
dockerfile: flyway.Dockerfile
links:
- db
depends_on:
- db
environment:
- PGPASSWORD=foobar
- PGUSER=foo
- PGDATABASE=baz
volumes:
- ./migrations:/opt/migrations
- ./scripts:/opt/scripts
command: /opt/scripts/migrate.sh
FROM centos:centos7
# Get flyway
RUN ["curl", "-O", "http://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/3.0/flyway-commandline-3.0.tar.gz"]
RUN ["yum", "install", "-y", "tar"]
RUN ["tar", "-xzf", "flyway-commandline-3.0.tar.gz"]
# Install java and the jdbc postgres driver
RUN ["yum", "install", "-y", "java-1.7.0-openjdk-headless"]
RUN ["yum", "install", "-y", "postgresql-jdbc"]
RUN ["yum", "install", "-y", "nc"]
RUN ["yum", "install", "-y", "postgresql"]
WORKDIR flyway-3.0
# Copy the postgres driver to its required location
RUN ["cp", "/usr/share/java/postgresql-jdbc.jar", "jars/"]
CMD ["./flyway"]
#!/bin/bash
# Make sure it will accept connections
until nc --send-only db 5432 < /dev/null
do
echo "waiting for postgres container..."
sleep 2
done
# Make sure the DB is ready to accept commands
until psql -w -p 5432 -h db -c "select 1"
do
echo "waiting for postgres to accept connections..."
sleep 2
done
./flyway migrate -user=$PGUSER -password=$PGPASSWORD -url="jdbc:postgresql://db:5432/${PGDATABASE}" -locations='filesystem:/opt/migrations'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment