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",
}
You can get value in app
const db_host = process.env.db_url || "10.155.30.13"
instead of reading it from json file.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.