![](/img/trans.png)
[英]docker-compose postgres restart after running scripts in docker-entrypoint-initdb.d
[英]Why are scripts in docker-entrypoint-initdb.d running before the database starts listening for connections causing pg_restore failure?
根據https://hub.docker.com/_/postgres的“初始化腳本”部分,我在 docker-entrypoint-initdb.d 中有兩個文件:
init-db.sh 包含:
#!/bin/sh
pg_restore -h localhost -p 5432 -U postgres -d postgres -v "/docker-entrypoint-initdb.d/backup.dump"
exit
它似乎正在執行,但出現服務器連接錯誤,因為服務器似乎尚未開始偵聽端口 5432,如下面的日志文件似乎表明的那樣。
我究竟做錯了什么? 一旦 postgres docker 容器已初始化並開始運行,我基本上想恢復數據庫。
一旦容器運行,如果我在終端 session 中手動運行 init-db.sh 腳本,它將完全按照我的意願創建數據庫......
2019-10-23T17:09:43.032659100Z /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/backup.dump
2019-10-23T17:09:43.032699100Z
2019-10-23T17:09:43.032704200Z /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init-db.sh
2019-10-23T17:09:43.069092100Z pg_restore: connecting to database for restore
2019-10-23T17:09:43.069518400Z pg_restore: error: connection to database "postgres" failed: could not connect to server: Connection refused
2019-10-23T17:09:43.069534000Z Is the server running on host "localhost" (127.0.0.1) and accepting
2019-10-23T17:09:43.069538000Z TCP/IP connections on port 5432?
2019-10-23T17:09:43.069540800Z could not connect to server: Cannot assign requested address
2019-10-23T17:09:43.069543700Z Is the server running on host "localhost" (::1) and accepting
2019-10-23T17:09:43.069546600Z TCP/IP connections on port 5432?
2019-10-23T17:09:45.514371200Z 2019-10-23 17:09:45.514 UTC [1] LOG: starting PostgreSQL 12.0 (Debian 12.0-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2019-10-23T17:09:45.514563000Z 2019-10-23 17:09:45.514 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2019-10-23T17:09:45.514629800Z 2019-10-23 17:09:45.514 UTC [1] LOG: listening on IPv6 address "::", port 5432
2019-10-23T17:09:45.522068300Z 2019-10-23 17:09:45.521 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-10-23T17:09:45.547131800Z 2019-10-23 17:09:45.547 UTC [23] LOG: database system was interrupted; last known up at 2019-10-23 17:09:42 UTC
2019-10-23T17:09:45.977904500Z 2019-10-23 17:09:45.977 UTC [23] LOG: database system was not properly shut down; automatic recovery in progress
2019-10-23T17:09:45.980523000Z 2019-10-23 17:09:45.980 UTC [23] LOG: invalid record length at 0/16453B0: wanted 24, got 0
2019-10-23T17:09:45.980590000Z 2019-10-23 17:09:45.980 UTC [23] LOG: redo is not required
2019-10-23T17:09:45.994889300Z 2019-10-23 17:09:45.994 UTC [1] LOG: database system is ready to accept connections
docker-compose.yml:
version: '3.4'
networks:
dev01:
driver: bridge
services:
webapplication2:
image: ${DOCKER_REGISTRY-}webapplication2
depends_on:
- "pgdev01"
build:
context: .
dockerfile: WebApplication2/Dockerfile
networks:
- dev01
pgdev01:
image: postgres
restart: always
volumes:
- db_volume:/var/lib/postgresql/data
- ./dbscripts/:/docker-entrypoint-initdb.d/
networks:
- dev01
volumes:
db_volume:
docker-compose.override.yml:
version: '3.4'
services:
webapplication2:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:443;http://+:80
- ASPNETCORE_HTTPS_PORT=44361
ports:
- "61225:80"
- "44361:443"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
pgdev01:
ports:
- "5434:5432"
environment:
POSTGRES_PASSWORD: "redacted"
您遇到此問題是因為您在init-db.sh
文件中包含了-h localhost
。 如果你刪除它,一切都會奏效。
發生這種情況是因為在postgres
映像提供的默認docker-entrypoint.sh
文件中,將在初始化階段清空listen_addresses
。 因此,它不會在localhost
上監聽。
披露:我為Enterprisedb (EDB)工作
改變
pg_restore -h localhost -p 5432 -U postgres -d postgres -v "/docker-entrypoint-initdb.d/backup.dump"
至
pg_restore -U postgres -d postgres -v "/docker-entrypoint-initdb.d/backup.dump"
解決了這個問題。 我不知道為什么......這是一個瘋狂的猜測,基於顯示無法使用主機和端口連接到 postgres 的日志......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.