Implementing Flyway with Docker

Sample Set-up

Here is an example docker-compose.yml file based on a simple back-end application that has an API layer and a Postgres database layer.

version: '3'
services:
api:
container_name: my-api
env_file:
- .env
build: .
ports:
- 5050:${SERVER_PORT}/tcp
links:
- postgres
postgres:
container_name: my-db
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD
- POSTGRES_DB=${POSTGRES_DB}
image: library/postgres:latest
ports:
- ${POSTGRES_PORT}:${POSTGRES_PORT}/tcp
flyway:
container_name: my-flyway
environment:
- FLYWAY_USER=${POSTGRES_USER}
- FLYWAY_PASSWORD=${POSTGRES_PASSWORD}
- FLYWAY_URL=jdbc:postgresql://postgres:${POSTGRES_PORT}/${POSTGRES_DB}
- FLYWAY_SCHEMAS=flyway,${POSTGRES_SCHEMA}
- FLYWAY_GROUP=true
- FLYWAY_LICENSE_KEY=${FLYWAY_LICENSE_KEY}
- FLYWAY_EDITION=${FLYWAY_EDITION}
image: flyway/flyway:latest
command: -locations=filesystem:/flyway/sql -connectRetries=60 migrate
volumes:
- $PWD/sql_versions:/flyway/sql
depends_on:
- postgres
image: flyway/flyway:latest
command: -locations=filesystem:/flyway/sql -connectRetries=60 migrate
volumes:
- $PWD/sql_versions:/flyway/sql
depends_on:
- postgres

Environment Variable Configuration

You can be more or less exhaustive with your environment variables — the important thing is that Flyway needs to know the following information under its environment header, and, since you need the same information for your database container, DRY!

  • I chose to make a new schema for Flyway called “flyway” because of Postgres’s default wide open permissions on the “public” schema.
  • This is an important variable because I found that Flyway’s “clean command, which wipes your database, ignores any schemas (and their children) which are created in migration scripts. In order forclean” to truly, well, clean, I needed to give Flyway explicit access to the schemas I wanted it to be able to clean.
- FLYWAY_EDITION=${FLYWAY_EDITION}
- FLYWAY_LICENSE_KEY=${FLYWAY_LICENSE_KEY}

docker-compose.yml Configuration

container_name and image are pretty standard Docker stuff, but here are a few Flyway-specific details:

  • This can be a list of locations, comma-separated and case-sensitive. Even if you store your files into multiple locations, Flyway still executes them in version order. Therefore, you cannot have version overlap between them.

Building the Containers

At this point, you should be able to simply run docker-compose up along with any desired options (my current project uses the following options: docker-compose up -d — -build (that’s supposed to be two hyphens before build, but Medium’s autoformatting is foiling me)). Please note that Flyway does not persist for longer than it takes to run the migrations (at least not in any way I’ve figured out yet!). The Flyway container will be created, run the migrations, and close itself (if you are fast enough with docker container ls, you might see it before it exits).

Flyway Commands Besides migrate

  • clean: Flyway documentation. Drops all schemas and children objects per your configuration in FLYWAY_SCHEMAS. This is best reserved for local dev environments. As the Flyway documentation aptly puts it:
  • validate: Flyway documentation. Checks your current database structure against your SQL scripts. For instance, if I ran Flyway’s migrate command against the following SQL script:
CREATE TABLE some_schema.some_table (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE some_schema.some_table (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
date_created timestamp
);

Migration Files

See Flyway documentation here. The “__” in each filename is two underscores, although this is not necessary unless you are also adding a description. In a nutshell:

Other Resources

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alana Thomas

Alana Thomas

20 Followers

Developer 👩‍💻 & tabletop gamer 🎲. I mainly post character backstories and Postgres tips.