password authentication failed for user postgres using docker-compose

Viewed 816

im trying to make requests from a node bash to a node backend and this to a database, so first i up this docker-compose:

version: '3.9'

services:
  backend:
    build: .
    ports:
       - '2000:2000'
  db:
    image: 'postgres'
    environment:
      POSTGRES_PASSWORD: '12345'
      POSTGRES_DB: 'attendancebd'

And it works good, i just create a bash in node and when i make a curl like:

curl backend:2000/info/teachers

it give me the correct json that i have in the db.

But when i use this docker compose:

version: '3.9'

services:
  server:
    build: .
    ports:
      - '5000:5000'
    networks:
      serveranddb:
        aliases:
          - server_host
      serverandbash:
        aliases:
          - server_host          
  db:
    image: 'postgres'
    environment:
      POSTGRES_PASSWORD: '12345'
      POSTGRES_DB: 'attendancebd'
    networks:
      serveranddb:
        aliases:
          - db_host          
  mybash:
    build: ./bash
    stdin_open: true
    tty: true
    networks:
      serverandbash:
        aliases:
          - bash_host    
networks:
  serveranddb:
    name: server_db
    driver: bridge
    ipam:
      driver: default
  serverandbash:
    name: server_bash
    driver: bridge
    ipam:
      driver: default     

and i do the same curl since the bash:

curl server_host:2000/info/teachers

it sends me the message:

{"message":"password authentication failed for user \"postgres\""}

And the environment is the same, with the same password and same database, why this is happening?

1 Answers

I find the solution, i have to put a depends_on into the server service so it will wait the db for raise up:

  server:
    build: .
    ports:
      - '5000:5000'
    networks:
      serveranddb:
        aliases:
          - server_host
      serverandbash:
        aliases:
          - server_host
    depends_on:
      - db 
Related