0

I'm executing below docker run command to run my nodejs based docker container

docker run -p 8080:7000 --env db_url=10.155.30.13 automation:v1.0.3

And i'm trying to access this env variable by using separate config file from my container. config file is in json format as below.

{
"db_host": "${process.env.db_url}",
}

And in my nodejs code, i'm accessing this db_host value to add the host IP to the listener. But when the above code executed, the docker container is brings down as soon as it brought up. But if i replace the json file value as below, it is working fine and my container is listening as below. Could someone please help me to pass the value and to access it within my json file?

{
"db_host": "10.155.30.13",
}

2 Answers 2

1

You can get value in app

const db_host = process.env.db_url || "10.155.30.13"

instead of reading it from json file.

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

1 Comment

This has worked perfectly my friend. But now there is another problem. If i tried to send the DB IP from the env variables like the below in my Docker compose file and config.js file, my container is exiting with 0 command. But if i do the above method, its working fine. Could you please tell why is the reason?
0

You can not substitute environment variable in JSON file, you can use dotenv or config that will help to have some default value in the config file and override these value from environment variables.

create default config vi config/default.json

{
"db_host": "10.155.30.13"
}

now read from ENV first, else pick the default value from config app.js

const config = require('config');
const dbConfig = process.env.DB_HOST || config.get('db_host');
console.log(dbConfig)

Now run the docker container

build the container

docker build -t app .

run the container

docker run -it app

console output

10.155.30.13

Now we want to override this default values

docker run -e DB_HOST=192.168.0.1 -it app

console output

192.168.0.1

enter image description here

Comments

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.