我有一个 docker compose,其中一个容器是 PostgreSQL,另一个容器是我的应用程序。我需要将两个数据库恢复到 PostgreSQL 中,然后启动应用程序。
目前我已经尝试过这个docker compose:
version: "3.7"
services:
dataspace_postgres:
container_name: custompostgres
platform: linux/amd64
build:
context: ./
dockerfile: Dockerfile.CustomPostgres
networks:
- dataspace
environment:
PGUSER: postgres
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
- 5434:5432
volumes:
- disk-postgres:/var/lib/postgresql/data
- ./dumps/psql/managementreporting.sql:/managementreporting.sql
- ./dumps/psql/idams_new.sql:/idams_new.sql
- ./dumps/init.sh:/docker-entrypoint-initdb.d/init.sh
healthcheck:
test: ["CMD-SHELL", "sh -c 'pg_isready -U postgres -d managementreporing'"]
interval: 10s
timeout: 120s
retries: 10
dataspace_consolidatedreportingunit:
container_name: consolidatedreportingunit
platform: linux/amd64
build:
context: ./
dockerfile: Dockerfile.ReportingTool
networks:
- dataspace
ports:
- 8080:80
- 8081:443
depends_on:
dataspace_postgres:
condition: service_healthy
networks:
dataspace:
driver: bridge
volumes:
disk-postgres:
driver: local
还有我的 init.sh 文件:
pg_restore -v -c -d managementreporting managementreporting.sql
pg_restore -v -c -d idams_new idams_new.sql
当前启动时,无论我尝试什么,它都会失败并出现以下错误:
pg_restore:连接到数据库以恢复custompostgres
| 2023-09-29 14:27:03.007 UTC [64] 致命:数据库 “管理报告”不存在 pg_restore:错误:在套接字上连接到服务器 “/var/run/postgresql/.s.PGSQL.5432”失败:致命:数据库 “managementreporting”不存在 custompostgres 退出,代码为 1
对我来说,恢复数据库的唯一方法是将它们直接安装到 /docker-entrypoint-initdb.d/ (用于转储),但是我无法让其他容器等待 postgesql 完成恢复。
有什么想法吗?
您可以在应用程序和数据库之间插入“等待”服务。例如:
services:
db:
image: postgres
wait:
image: willwill/wait-for-it
command: -t 60 db:5432
depends_on:
- db
app:
image: some/image
depends_on:
- wait
“app”服务将等待“wait”服务准备就绪,“wait”服务将等待 db 服务上的端口 5432 上打开套接字。
这里有一篇博客文章演示了它(虽然是 Flyway 而不是应用程序,但想法相同。)