0

I have this really simple setup with a web app in one container and a Postgres service running in another container.

I need to connect to the Postgres container and thought PGHOST="db" would point to that container ..?

But I keep getting Error: getaddrinfo ENOTFOUND "db" at GetAddrInfoReqWrap.onlookup that I read as; can't find the "db" host ...

What am I missing here?

version: "3.9"
services:
  web:
    build: .
    ports:
      - "8081:3011"
    links:
      - db
    environment:
      - PGHOST="db"
      - PGDATABASE="testdb"
      - PGUSER="postgres"
      - PGPASSWORD="postgres"
  db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - /usr/local/var/postgresql@13
Riri
  • 11,501
  • 14
  • 63
  • 88

1 Answers1

1

Try this config. You don't need quotes when passing env variables. And it is better to use depends_on here to make sure DB is up and running before your app starts.

version: "3.9"
services:
  web:
    build: .
    ports:
      - "8081:3011"
    depends_on:
      - db
    environment:
      - PGHOST=db
      - PGDATABASE=testdb
      - PGUSER=postgres
      - PGPASSWORD=postgres
  db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - /usr/local/var/postgresql@13
Ayzrian
  • 2,279
  • 1
  • 7
  • 14
  • You may want to add a `depends_on` clause for the `web` service. I'm not sure if the `links` clause is needed here – Hollyol Oct 21 '21 at 14:21
  • @Hollyol, thanks edited the answer. – Ayzrian Oct 21 '21 at 14:23
  • its better to design the app to work without `depends_on` with retry logic and things like that. – The Fool Oct 21 '21 at 14:23
  • 1
    `depends_on:` doesn't actually ensure the database is up and running; see [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y). (It does avoid a DNS-lookup error and is generally good practice, though.) – David Maze Oct 21 '21 at 14:44