3

I have a .env file like below:

# DEV
SALES_DB_HOST=xxx

Then I have a docker-compose.yml file that looks like:

version: "3.1"
services:
  web:
    image: xxx
    build: .
    env_file: .env

However, the values for the environment variables when accessed in nodejs like process.env.SALES_DB_HOST it prints undefined.

Output of docker-compose config is:

services:
  web:
    build:
      context: xxxxxxxx
    environment:
      SALES_DB_HOST: xxx
    image: xxxxx
version: '3.1'

So, it looks like docker-compose.yml is formed correctly. But why is process.env not getting this value correctly?

EDIT:

I build the docker image with: docker build -t my_image .

kovac
  • 4,945
  • 9
  • 47
  • 90
  • Why do you have an extra `process.env` in there? Does [this question](https://stackoverflow.com/q/4870328/596285) help? – BMitch Jun 08 '18 at 09:35
  • @BMitch Sorry, it was a typo in the question. I used `process.env.SALES_DB_HOST` in my code. – kovac Jun 08 '18 at 09:53

2 Answers2

2

Can you change command on you container configuration in yml file. You should try to test your environment to understand - where is a problem. In docker or in your code.

Try something like this:

maxantonov : ~/passbolt  .$ cat dc.yml
version: '3.4'
services:
  db:
    image: alpine:latest
    container_name: db
    hostname: db
    env_file:
      - env/mysql.env
    command: ["printenv"]

maxantonov : ~/passbolt  .$ docker-compose -f dc.yml up
Starting db ... done
Attaching to db
db    | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
db    | HOSTNAME=db
db    | MYSQL_ROOT_PASSWORD=test
db    | MYSQL_DATABASE=passbolt
db    | MYSQL_USER=passbolt
db    | MYSQL_PASSWORD=P4ssb0lt
db    | HOME=/root
db exited with code 0
Maxim Antonov
  • 295
  • 3
  • 9
0

It's not a docker problem. Look to your code:

process.env.process.env.SALES_DB_HOST

It's typo. process.env.process.env

You shold use

process.env.SALES_DB_HOST
Maxim Antonov
  • 295
  • 3
  • 9
  • Sorry, the typo was in my question not in the code. I was using `process.env.SALES_DB_HOST` all along. – kovac Jun 08 '18 at 09:52