pschiffe / docker-pdns

Docker images for PowerDNS
MIT License
274 stars 81 forks source link

Can't connect to db(at least pgsql and admin image) #140

Closed maru0123-2004 closed 5 months ago

maru0123-2004 commented 5 months ago

I tried to use pdns-pgsql and pdns-admin image. But these error happened:

psql: error: connection to server at "db_admin" (172.23.0.3), port 5432 failed: FATAL:  database "dns" does not exist
DB is unavailable - sleeping

and it repeated.

I checked docker-entrypoint.sh and found this line:

generatePostgreSQLCommand() {
  PGSQL_COMMAND="psql -h ${PDNS_gpgsql_host} -p ${PDNS_gpgsql_port} -U ${PDNS_gpgsql_user}"
}

I think it is missing "dbname" arg. So I tried with this one(on pdns-pgsql image):

generatePostgreSQLCommand() {
  PGSQL_COMMAND="psql -h ${PDNS_gpgsql_host} -p ${PDNS_gpgsql_port} -U ${PDNS_gpgsql_user} -d ${PDNS_gpgsql_dbname}"
}

After fixing this, pdns works successfully.

pschiffe commented 5 months ago

Hm, interesting. The suggested solution won't work in general, as the DB is created only after the check for pgsql availability passes (if it doesn't exist) (line 110)

Could you give me more information about your setup? What version of pgsql are you using and how are you creating the pdns-pgsql container?

If you try the provided docker-compose file, do you see the same problem?

maru0123-2004 commented 5 months ago

Sorry, it seems just my configuration problem. I initialize db with env var on postgres image, so there was no default name databases on db container.

generatePostgreSQLCommand

PGPASSWORD="${PDNS_gpgsql_password}"
export PGPASSWORD

# Wait for pgsql to respond
until $PGSQL_COMMAND -c ';' ; do
    >&2 echo 'Pgsql is unavailable - sleeping'
    sleep 3
done

createDatabaseIfRequested
initDatabase
initSuperslave

if [ "${USE_EXISTING_CONFIG_FILE:-false}" = 'false' ]; then
  echo "(re-)generating config file from environment variables"
  generateAndInstallConfigFileFromEnvironment
fi

In docker-entrypoint.sh, first generate and check connection for db. It was bad in my configuration.

maru0123-2004 commented 5 months ago

For reference, this is my docker-compose.yaml

services:
  pdns:
    image: pschiffe/pdns-pgsql:4.9-alpine
    environment:
      - PDNS_primary=yes
      - PDNS_api=yes
      - PDNS_api_key
      - PDNS_webserver=yes
      - PDNS_webserver_address=0.0.0.0
      - PDNS_webserver_password
      - PDNS_gpgsql_host=db
      - PDNS_gpgsql_port=${POSTGRES_PORT:-5432}
      - PDNS_gpgsql_user=${POSTGRES_USER}
      - PDNS_gpgsql_password=${POSTGRES_PASSWORD}
      - PDNS_gpgsql_dbname=${POSTGRES_DB}
      - PDNS_gpgsql_dnssec=yes
    ports:
      - "53:53"
      - "53:53/udp"
    restart: always
    depends_on:
      db:
        condition: service_healthy
  db:
    image: postgres:16
    volumes:
      - ./data:/var/lib/postgresql/data
    healthcheck:
      test: "pg_isready -U $$POSTGRES_USER"
      interval: 2s
      timeout: 5s
      retries: 5
    restart: always
    environment:
      - POSTGRES_PORT
      - POSTGRES_USER
      - POSTGRES_PASSWORD
      - POSTGRES_DB

All of additional env vars was set by dotenv file on same directory. Thank you.