Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize postgres db with flyway in docker?

I have django app that I am attempting to host in docker. I have been unsuccessful in launching my postgres server before standing up the django app. Here is my docker-compose.yaml

version: '3'
services:
  flyway:
    image: boxfuse/flyway
    command: -url=jdbc:postgresql://db/dbname -schemas=schemaName -user=user -password=pwd migrate
    volumes:
      - ./flyway:/flyway/sql
    depends_on:
      - db
  db:
    image: postgres:9.6
    restart: always
    ports:
      - 5432:5432
    environment:
    - POSTGRES_PASSWORD=pwd
    healthcheck:
      test: "pg_isready -q -U postgres"
  app:
    image: myimage
    ports:
      - 8000:8000

Services db and app both seem to stand up fine but I am unable to spin up the postgres defaults with flyway. Here are the errors that I'm getting:

flyway_1  | SEVERE: Connection error: 
flyway_1  | org.postgresql.util.PSQLException: Connection to db:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

ERROR: 
flyway_1  | Unable to obtain connection from database (jdbc:postgresql://db/dbname) for user 'user': Connection to db:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

I couldn't find a good example on how to use flyway with Postgres. How do I go about getting this to work? TIA

like image 317
dweeb Avatar asked Aug 26 '18 01:08

dweeb


2 Answers

depends_on of the flyway service does not actually check that the database within db-container is up and running, but instead only checks that the container is up. This is quite different. The container could be up and running at the moment the database within it is starting but not yet accepting connections.

For such a case, you should specify a health check to make sure your database is accepting connections. You can even find an example how to do it with PostgreSQL in the official docker-compose docs.

like image 64
Stanislav Avatar answered Sep 21 '22 08:09

Stanislav


Version '3+' of the docker-compose file doesn't support parameter condition in the depends_on block, but version '2.1+' does. So you can create compose file like the following, that uses healthcheck from the postgres section, for example:

version: '2.1'

services:
  my-app:
#   ...  
#   ... 
    depends_on:
      - flyway

  flyway:
    image: boxfuse/flyway:5-alpine
    command: -url=jdbc:postgresql://postgres:5432/mydb -schemas=public -user=postgres -password=postgres migrate
    volumes:
      - ./migration:/flyway/sql
    depends_on:
      postgres:
        condition: service_healthy

  postgres:
    domainname: postgres
    build: ./migration
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    healthcheck:
      test: ["CMD", "pg_isready", "-q", "-U", "postgres"]
      interval: 5s
      timeout: 1s
      retries: 2
like image 20
Cepr0 Avatar answered Sep 22 '22 08:09

Cepr0