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 .

2
  • Why do you have an extra process.env in there? Does this question help? Commented Jun 8, 2018 at 9:35
  • @BMitch Sorry, it was a typo in the question. I used process.env.SALES_DB_HOST in my code. Commented Jun 8, 2018 at 9:53

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Sorry, the typo was in my question not in the code. I was using process.env.SALES_DB_HOST all along.

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.