docker-compose -f docker-compose.migration.yml run flyway /opt/scripts/migrate.sh
Last active
October 23, 2023 20:10
-
-
Save hartmannr76/5469e55ca910e4e84bbe09dbb39afb03 to your computer and use it in GitHub Desktop.
Flyway/Postgres Migration with Docker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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