Error: getaddrinfo EAI_AGAIN when running in container

134 views Asked by At

I'm having this error in my API when running my docker compose up. For some reason, the api cannot connect to the bank. Probably DNS error.

My docker-compose.yml:

networks:
  full-application:
    driver: bridge

services:
  db: 
    image: postgres
    container_name: postgres-container
    networks:
      - full-application
    restart: always
    env_file: ./.env
    environment:
      - POSTGRES_PASSWORD=$POSTGRES_PASSWORD
    ports:
      - "5432:5432"
  api:
    build: 
      context: ./backend
      dockerfile: Dockerfile
    working_dir: /home/node/app
    networks:
      - full-application
    container_name: node-container
    restart: always
    env_file: ./backend/.env
    depends_on:
      - db
    ports:
      - "3003:3000"

API connection with postgres:

const client = new pg.Pool({
    host: 'postgres://db:5432', 
    user: 'postgres',
    password: 'mypassword', 
    database: 'docker'
});

Error (it is possible to see it through docker logs (container)):

[nodemon] starting ts-node ./src/index.ts Error: getaddrinfo EAI_AGAIN postgres://db:5432 at /home/node/app/node_modules/pg-pool/index.js:45:11 at processTicksAndRejections (node:internal/process/task_queues:95:5) { errno: -3001, code: 'EAI_AGAIN', syscall: 'getaddrinfo', hostname: 'postgres://db:5432' } [nodemon] app crashed - waiting for file changes before starting...

0

There are 0 answers