如何在docker中使用flyway初始化postgres db?

dwe*_*eeb 9 postgresql flyway docker-compose

我有我试图在 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
Run Code Online (Sandbox Code Playgroud)

服务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.
Run Code Online (Sandbox Code Playgroud)

我找不到关于如何在 Postgres 中使用 flyway 的好例子。我该如何让它发挥作用?TIA

Sta*_*lav 2

depends_onflyway服务实际上并不检查 db-container 中的数据库是否已启动并正在运行,而是仅检查容器是否已启动。这是完全不同的。当容器内的数据库启动但尚未接受连接时,容器可能已启动并运行。

对于这种情况,您应该指定运行状况检查以确保您的数据库正在接受连接。您甚至可以在官方 docker-compose 文档中找到如何使用 PostgreSQL 执行此操作的示例。