gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:36:36 27 4
gpt4 key购买 nike

我有一个 django 应用程序,我正试图在 docker 中托管它。在启动 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.

我找不到关于如何将 flyway 与 Postgres 结合使用的好示例。我该如何着手让它发挥作用?时间差

最佳答案

docker-compose 文件的版本“3+”doesn't support depends_on block 中的参数 condition,但版本为“2.1+”does .因此,您可以创建如下所示的组合文件,它使用 postgres 部分中的 healthcheck,例如:

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

关于postgresql - 如何在 docker 中使用 flyway 初始化 postgres db?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52022494/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com