skip to Main Content

I’m trying to configure my docker-compose file so that it automatically connects to the right database depending on the credentials provided in the .env file. Connecting to the local database doesn’t cause any problems, but connecting to the remote one doesn’t work. Returns error port 5432 failed: FATAL: password authentication failed for user

docker-compose.yml

version: "3.9"
services:
  panel:
    env_file: .env
    restart: always
    build: .
    ports:
      - "8000:8000"
    command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/app
    depends_on:
      - db
    environment:
      - DB_HOST=db
  db:
    image: postgres:13
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_NAME}
      - DB_HOST=${DB_HOST}

volumes:
  postgres_data:

.env file

DB_NAME=panel_db
DB_USER=db_root
DB_PASSWORD=i8sfsfdhisdu9sd8sjdis
DB_PORT=5432
DB_HOST=localhost

# remote
# DB_NAME=remote_panel_db
# DB_USER=gpanel
# DB_PASSWORD=yG8W5rxeggAasdffsdTolG
# DB_HOST=....rds.amazonaws.com
# DB_PORT=5432

In this form, I connect to the local base without any problems.

Now I am trying to uncomment the remote database access credentials

#DB_NAME=panel_db
#DB_USER=db_root
#DB_PASSWORD=i8sfsfdhisdu9sd8sjdis
#DB_PORT=5432
#DB_HOST=localhost

# remote
DB_NAME=remote_panel_db
DB_USER=gpanel
DB_PASSWORD=yG8W5rxeggAasdffsdTolG
DB_HOST=....rds.amazonaws.com
DB_PORT=5432

And it’s not working anymore.

I’m assuming it’s the line

environment:
  - DB_HOST=db

But I can’t figure out what to change

2

Answers


  1. Could you confirm you are able to connect to remote DB? You can attach to running container by docker exec -it <container_name> /bin/bash or run new container by docker run <container_name> -it /bin/bash

    then use psql to connect to your DB to verify you are able to connect from there
    psql -h <hostname or ip address> -p <port number of remote machine> -d <database name which you want to connect> -U <username of the database server>

    Login or Signup to reply.
  2. Since you have both env_file: and environment:, the documentation for env_file: notes:

    Environment variables declared in the environment section override these values – this holds true even if those values are empty or undefined.

    So when your Compose file has

    env_file: .env
    environment:
      - DB_HOST=db
    

    the value of $DB_HOST is always db; the value from the .env file is ignored. In the remote case you’re trying to connect to the Compose database but with your RDS credentials.

    Since you declare DB_HOST in both .env files, you don’t need it in the docker-compose.yml. Just delete that entire environment: block.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search