英文:
Flyway not connecting to postgresql
问题
I have a problem for a time and I yet have to find out why it is happening.
我有一个问题,一段时间以来我还没有找出它为什么会发生。
I'm trying to build a "instagram clone" to study Golang, but I cannot find out why my flyway does not connect to my postgresql.
我正在尝试构建一个“Instagram克隆”来学习Golang,但我无法找出为什么我的Flyway无法连接到我的PostgreSQL。
Here is my docker compose:
这是我的Docker Compose文件:
version: "3.8"
services:
server:
build: .
ports:
- "3000:3000"
volumes:
- .:/usr/src/app
db:
image: postgres:14.5
environment:
POSTGRES_DB: instagram
POSTGRES_USER: instagram
POSTGRES_PASSWORD: instagram
ports:
- "5432:5432"
flyway:
image: flyway/flyway:9.9.0
entrypoint:
- sh
- -c
- flyway -defaultSchema=public -baselineOnMigrate=true -url=jdbc:postgresql://localhost:5432/instagram -user=instagram -password=instagram migrate
depends_on:
- db
volumes:
- ./data/sql:/flyway/sql
The database seems to be correctly created, I can access it through DBeaver, however, my flyway cannot connect to it.
数据库似乎已经正确创建,我可以通过DBeaver访问它,但是我的Flyway无法连接到它。
I'm running it on a WSL2, I have already clear my docker deleting every container, volume and image from it once or twice. I'm running it with docker compose up db followed by docker compose up flyway. Does anyone have any ideas?
我在WSL2上运行它,我已经清理了Docker,删除了其中的每个容器、卷和映像一次或两次。我使用docker compose up db后跟docker compose up flyway来运行它。有人有什么想法吗?
EDIT: This is my docker compose file after the resolution for anyone that may need it
编辑:这是我的Docker Compose文件,在解决后,供可能需要的人使用。
英文:
I have a problem for a time and I yet have to find out why it is happening.
I'm trying to build a "instagram clone" to study Golang, but I cannot find out why my flyway does not connect to my postgresql.
Here is my docker compose:
version: "3.8"
services:
server:
build: .
ports:
- "3000:3000"
volumes:
- .:/usr/src/app
db:
image: postgres:14.5
environment:
POSTGRES_DB: instagram
POSTGRES_USER: instagram
POSTGRES_PASSWORD: instagram
ports:
- "5432:5432"
flyway:
image: flyway/flyway:9.9.0
entrypoint:
- sh
- -c
- flyway -defaultSchema=public -baselineOnMigrate=true -url=jdbc:postgresql://localhost:5432/instagram -user=instagram -password=instagram migrate
depends_on:
- db
volumes:
- ./data/sql:/flyway/sql
The database seems to be correctly created, I can access it through DBeaver, however, my flyway cannot connect to it.
I'm running it on a WSL2, I have already clear my docker deleting every container, volume and image from it once or twice. I'm running it with docker compose up db followed by docker compose up flyway. Does anyone have any ideas?
EDIT: This is my docker compose file after the resolution for anyone that may need it
version: "3.8"
services:
server:
build: .
ports:
- "3000:3000"
volumes:
- .:/usr/src/app
db:
image: postgres:14.5
env_file:
- ".env"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U instagram"]
interval: 10s
retries: 5
ports:
- "5432:5432"
flyway:
image: flyway/flyway:9.9.0
entrypoint:
- sh
- -c
- flyway -defaultSchema=public -baselineOnMigrate=true -connectRetries=60 -url=jdbc:postgresql://db:5432/instagram -user=instagram -password=instagram migrate
depends_on:
- db
volumes:
- ./data/sql:/flyway/sql
答案1
得分: 1
有时 DB 容器需要几秒钟才能启动。在命令中使用 -connectRetries=60 标志,每秒自动重试 x 秒。查看 GitHub Flyway docker。
使用健康检查:
healthcheck:
test: ["CMD-SHELL", "pg_isready -U instagram -d instagram -h db"]
interval: 5s
timeout: 5s
retries: 10
此外,您可以尝试在命令中使用无限循环等待:
flyway:
image: flyway/flyway:9.9.0
command: ["sh", "-c", "while ! nc -z db 5432; do sleep 1; done; flyway -defaultSchema=public -baselineOnMigrate=true -url=jdbc:postgresql://db:5432/instagram -user=instagram -password=instagram migrate"]
depends_on:
- db
healthcheck:
test: ["CMD-SHELL", "pg_isready -U instagram -d instagram -h db"]
interval: 5s
timeout: 5s
retries: 10
volumes:
- ./data/sql:/flyway/sql
英文:
Sometimes DB container takes a few seconds to spin up. Use -connectRetries=60 flag in the command for automatic retries every second for x seconds. Check GitHub Flyway docker
Using health check:
healthcheck:
test: ["CMD-SHELL", "pg_isready -U instagram -d instagram -h db"]
interval: 5s
timeout: 5s
retries: 10
Also, you can try to wait in an infinite loop in the command:
flyway:
image: flyway/flyway:9.9.0
command: ["sh", "-c", "while ! nc -z db 5432; do sleep 1; done; flyway -defaultSchema=public -baselineOnMigrate=true -url=jdbc:postgresql://db:5432/instagram -user=instagram -password=instagram migrate"]
depends_on:
- db
healthcheck:
test: ["CMD-SHELL", "pg_isready -U instagram -d instagram -h db"]
interval: 5s
timeout: 5s
retries: 10
volumes:
- ./data/sql:/flyway/sql
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论