7

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:
10
  • 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? Commented Feb 17, 2019 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. Commented Feb 17, 2019 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. Commented Feb 17, 2019 at 23:26
  • @randal don't remove the env_file, remove the environment: section from your compose file. Commented Feb 18, 2019 at 0:35
  • ok let me try it Commented Feb 18, 2019 at 0:40

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Can you kindly explain this ${POSTGRES_PASSWORD:-password} more, is this the env variable from the .env file, the syntax is a bit weird
See the answer to this SO question pertaining to the syntax: stackoverflow.com/a/2013589/328275
See also bash parameter substitution: tldp.org/LDP/abs/html/parameter-substitution.html
I'm having a similar issue, any help is apreciated - stackoverflow.com/questions/78249196/…
This doesn't work, but docker-compose config is a good tip, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.