如何在Docker中使用Flyway初始化Postgres DB?

时间:2018-08-26 01:30:28

标签: postgresql docker-compose flyway

我正在尝试在docker中托管的django应用程序。在启动django应用之前,我无法启动我的postgres服务器。这是我的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

服务dbapp看起来都不错,但是我无法通过flyway提升postgres的默认值。这是我遇到的错误:

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.

我找不到如何在Postgres中使用飞行通道的好例子。我该如何开始工作呢? TIA

2 个答案:

答案 0 :(得分:1)

我想,原因是事实,depends_on服务的flyway并没有实际检查db-container中的数据库是否已启动并正在运行,而是检查了容器是否已启动。这是完全不同的-容器中的数据库启动并且尚未接受连接时,容器可以启动并运行。

在这种情况下,您应该指定进行健康检查,以确保您的数据库正在接受连接。您甚至可以在official docker-compose docs中找到使用PostgreSQL进行操作的示例。

答案 1 :(得分:0)

assets块中的docker-compose文件doesn't support参数condition的版本'3+',但版本'2.1+'does。因此,您可以创建如下的组成文件,该文件使用depends_on部分中的healthcheck,例如:

postgres