10

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

dweeb
  • 539
  • 2
  • 6
  • 21

3 Answers3

3

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
Cepr0
  • 28,144
  • 8
  • 75
  • 101
2

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.

Brandon Essler
  • 710
  • 8
  • 21
Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • 1
    Thanks to your point, I managed to circumvent the issue by a `docker-compose up` for each service in the desired order. This seemed to do away with the dependency issue. – dweeb Sep 17 '18 at 14:46
0

Please use -connectRetries to wait for postgres, example (wait for 60s): -connectRetries=60

More details here https://github.com/flyway/flyway-docker

ndh103
  • 96
  • 1
  • 6