简体   繁体   English

为什么 docker-entrypoint-initdb.d 中的脚本在数据库开始侦听导致 pg_restore 失败的连接之前运行?

[英]Why are scripts in docker-entrypoint-initdb.d running before the database starts listening for connections causing pg_restore failure?

As per the "Initialization scripts" section of https://hub.docker.com/_/postgres , I have two files in docker-entrypoint-initdb.d:根据https://hub.docker.com/_/postgres的“初始化脚本”部分,我在 docker-entrypoint-initdb.d 中有两个文件:

  1. init-db.sh初始化-db.sh
  2. backup.dump备份转储

init-db.sh contains: init-db.sh 包含:

#!/bin/sh
pg_restore -h localhost -p 5432 -U postgres -d postgres -v "/docker-entrypoint-initdb.d/backup.dump"
exit

and it seems to execute but gets a server connection error because it seems the server hasn't started listening on port 5432 yet as the below logs files seem to indicate.它似乎正在执行,但出现服务器连接错误,因为服务器似乎尚未开始侦听端口 5432,如下面的日志文件似乎表明的那样。

What am I doing wrong?我究竟做错了什么? I essentially want to restore a database once the postgres docker container has been initialized and started running.一旦 postgres docker 容器已初始化并开始运行,我基本上想恢复数据库。

Once the container is running if I run the init-db.sh script manually in a terminal session, it creates the database exactly as I want...一旦容器运行,如果我在终端 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: 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: 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"

You are encountering this issue because you included -h localhost in your init-db.sh file.您遇到此问题是因为您在init-db.sh文件中包含了-h localhost If you remove it, things will work.如果你删除它,一切都会奏效。

This is happening because in the default docker-entrypoint.sh file provided by the postgres image will empty out listen_addresses during the initialization phase .发生这种情况是因为在postgres映像提供的默认docker-entrypoint.sh文件中,将在初始化阶段清空listen_addresses Therefore, it won't listen on localhost .因此,它不会在localhost上监听。

Disclosure: I work for Enterprisedb (EDB)披露:我为Enterprisedb (EDB)工作

Changing改变

pg_restore -h localhost -p 5432 -U postgres -d postgres -v "/docker-entrypoint-initdb.d/backup.dump"

to

pg_restore -U postgres -d postgres -v "/docker-entrypoint-initdb.d/backup.dump"

fixed this.解决了这个问题。 I have not idea why... It was a wild guess based the logs showing an inability to connect to postgres with the host and port...我不知道为什么......这是一个疯狂的猜测,基于显示无法使用主机和端口连接到 postgres 的日志......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 docker-compose postgres 在 docker-entrypoint-initdb.d 中运行脚本后重新启动 - docker-compose postgres restart after running scripts in docker-entrypoint-initdb.d 初始化postgres数据库(不使用/docker-entrypoint-initdb.d) - Initialize postgres database (without using /docker-entrypoint-initdb.d) 在Windows上运行Docker映像-docker-entrypoint.sh采购/docker-entrypoint-initdb.d - Running docker images on Windows - docker-entrypoint.sh sourcing /docker-entrypoint-initdb.d 在 docker-entrypoint-initdb.d 中创建 pg_cron 扩展失败 - creating pg_cron extension within docker-entrypoint-initdb.d fails docker alpine postgres在撰写中不执行docker-entrypoint-initdb.d脚本 - docker alpine postgres in compose not executing docker-entrypoint-initdb.d scripts Docker PostgreSQL - /docker-entrypoint-initdb.d中的脚本无法运行 - Docker PostgreSQL - Scripts in /docker-entrypoint-initdb.d doesn't run 在以前工作的配置中忽略 Postgres 数据库的 /docker-entrypoint-initdb.d 错误 - ignoring /docker-entrypoint-initdb.d error for Postgres database in a configuration that previously worked docker-entrypoint-initdb.d 错误解释器:权限被拒绝 - docker-entrypoint-initdb.d bad interpreter: Permission denied 为什么 postgres 容器在 Gitlab CI 中忽略 /docker-entrypoint-initdb.d/* - Why is postgres container ignoring /docker-entrypoint-initdb.d/* in Gitlab CI 使用 docker-entrypoint-initdb.d 脚本初始化 PostgreSQL 容器 - Initialize PostgreSQL Container with docker-entrypoint-initdb.d script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM