1

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
3
  • 1
    Pretty sure this was already answered somewhere else, but you don't need the " around db Commented Oct 21, 2021 at 14:10
  • 1
    if you want to use quotes you should quote the full string and not just a part of it, but in YAML you don't need quotes most of the time. Commented Oct 21, 2021 at 14:22
  • Isschh .. 🤦‍♂️ Of course. Thanks. Commented Oct 22, 2021 at 6:22

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

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, thanks edited the answer.
its better to design the app to work without depends_on with retry logic and things like that.
depends_on: doesn't actually ensure the database is up and running; see Docker Compose wait for container X before starting Y. (It does avoid a DNS-lookup error and is generally good practice, though.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.