4

Docker is using the variables from my .env file and i keep getting the error:

Unhandled rejection SequelizeConnectionError: role "eli" does not exist

I would like for Postgres to get the variables from the environment set in docker-compose.yml

.env

POSTGRES_PORT=5432
POSTGRES_DB=elitest4
POSTGRES_USER=eli
POSTGRES_PASSWORD=

docker-compose.yml

# docker-compose.yml
version: "3"
services:
  app:
    build: .
    depends_on:
      - database
    ports:
      - 8000:8000
    environment:
      - POSTGRES_HOST=database
    env_file:
      - .env
  database:
    image: postgres:9.6.8-alpine
    environment: # postgress should be getting these variables, not the variables set in the env file thats for localhost
      POSTGRES_PASSWORD: password
      POSTGRES_USER: user
      POSTGRES_DB: db
    volumes:
      - pgdata:/var/lib/postgresql/pgdata
    ports:
      - 8002:5432
    env_file:
      - .env
  react_client:
      build:
        context: ./client
        dockerfile: Dockerfile
      image: react_client
      working_dir: /home/node/app/client
      volumes:
        - ./:/home/node/app
      ports:
        - 8001:8001
      env_file:
        - ./client/.env
volumes:
  pgdata:
William Ross
  • 3,568
  • 7
  • 42
  • 73
randal
  • 1,272
  • 3
  • 23
  • 48
  • 1
    Seems to me that this is not a Docker issue but PostgreSQL one. Look at the error you've posted `Unhandled rejection SequelizeConnectionError: role "eli" does not exist` if Docker weren't picking up your ENV variable why or how it would complain about an inexistent role? – ReynierPM Feb 17 '19 at 22:23
  • Environment variables defined in the docker-compose file take precedence over those in the `env_file`. Remove them from the compose file. – teppic Feb 17 '19 at 23:19
  • I see how would i be able to use the variables defined in the environement section opposed to the .env file. I removed `env_file` and i still get the error `Unhandled rejection SequelizeConnectionError: role "eli" does not exist` This is a docker issue, because it works on my local host. – randal Feb 17 '19 at 23:26
  • @randal don't remove the `env_file`, remove the `environment:` section from your compose file. – teppic Feb 18 '19 at 00:35
  • ok let me try it – randal Feb 18 '19 at 00:40
  • i still get the sequelise connection error. u got a repo that uses react, express, and sequelize u can redirect me to ? – randal Feb 18 '19 at 00:44
  • Did you create a database user with that name? – Laurenz Albe Feb 18 '19 at 04:25
  • no, thats the database user of the localhost production, not docker. so when i npm start, and have postgress running on my localhost, it works. But i have not setup a postgress database user role on docker because im unsure how, and how to have docker know what variables to use. – randal Feb 18 '19 at 04:27
  • wait im referencing this https://github.com/anthub-services/docker-for-node-api-and-client-boilerplates and i think im missing the docker sequelize commands. – randal Feb 18 '19 at 04:34
  • Hey @randal, am haing a question, how did you connect the Postgres container? did you use an already uploaded container, or you were referencing the database locally ? – Idris Stack May 01 '20 at 08:31

1 Answers1

9

TL/DR

Try updating the docker-compose service database environment section as follows:

environment:
  POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password}
  POSTGRES_USER: ${POSTGRES_USER:-user}
  POSTGRES_DB: ${POSTGRES_DB:-db}

Also notice that if you would like to see how each bound variable ultimately evaluates in Compose you can run the following command to see the "effective" compose file:

$ docker-compose config

This command will print out what your compose file looks like with all variable substitution replaced with its evaluation.


See the Environment variables in Compose and the Variable substitution sections in the documentation.

Pay close attention to this section:

When you set the same environment variable in multiple files, here’s the priority used by Compose to choose which value to use:

1. Compose file 
2. Shell environment variables 
3. Environment file Dockerfile
4. Variable is not defined

In the example below, we set the same environment variable on an Environment file, and the Compose file:

$ cat ./Docker/api/api.env
NODE_ENV=test

$ cat docker-compose.yml 
version: '3' 
services:
api:
  image: 'node:6-alpine'
  env_file:
    - ./Docker/api/api.env
  environment:
    - NODE_ENV=production 

When you run the container, the environment variable defined in the Compose file takes precedence.

$ docker-compose exec api node
process.env.NODE_ENV 'production'

Having any ARG or ENV setting in a Dockerfile evaluates only if there is no Docker Compose entry for environment or env_file.

Specifics for NodeJS containers

If you have a package.json entry for script:start like NODE_ENV=test node server.js, then this overrules any setting in your docker-compose.yml file.

axiopisty
  • 4,972
  • 8
  • 44
  • 73