Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active February 14, 2023 12:36
Show Gist options
  • Save drmalex07/21a9acba4eb1a39cfc87eb6c0cd698b3 to your computer and use it in GitHub Desktop.
Save drmalex07/21a9acba4eb1a39cfc87eb6c0cd698b3 to your computer and use it in GitHub Desktop.
Run flyway migration from a Docker container. #flyway

README - Run a Flyway migration from a Docker container

See also: https://flywaydb.org/documentation/configuration/configfile

Prepare basic Flyway configuration (no sensitive data inside), say at config/flyway.conf:

# vim: set syntax=jproperties:
# See https://flywaydb.org/documentation/configuration/configfile
flyway.schemas = foo
flyway.defaultSchema = foo
flyway.url = jdbc:postgresql://postgres-1:5432/helloworld?
flyway.table = _schema_version
flyway.baselineVersion = 0
flyway.baselineOnMigrate = true

Prepare another configuration file with sensitive data (connection credentials), say at config/secret.conf:

flyway.user = tester
flyway.password = tester

Prepare your migration scripts under sql/ (so this directory will contain all V<version>__<description>.sql files).

Prepare a docker-compose recipe for running migration-related operations:

# vim: syntax=yaml:

version: '3.6'
services:

  # This is not a service, but an one-off command. Run like:
  # docker-compose run --rm flyway migrate

  'flyway':
    image: 'flyway/flyway:7.7'
    #command: ['info']
    volumes:
    - type: 'bind'
      source: ./config/flyway.conf
      target: /flyway/conf/flyway.conf
      read_only: true
    - type: 'bind'
      source: ./config/secret.conf
      target: /flyway/conf/secret.conf
      read_only: true
    - type: 'bind'
      source: ./sql
      target: /flyway/sql
      read_only: true
    environment:
      FLYWAY_CONFIG_FILES: /flyway/conf/flyway.conf,/flyway/conf/secret.conf
    
    # note: this is just a quick'n'dirty way to access the database!
    network_mode: "bridge"
    extra_hosts:
    - 'postgres-1:172.17.0.2'

Print info:

docker-compose run --rm flyway info

Migrate schema:

docker-compose run --rm flyway migrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment