Home > Net >  Docker compose, accessing a Postgres container from an other container
Docker compose, accessing a Postgres container from an other container

Time:10-22

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

CodePudding user response:

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
  • Related