简体   繁体   中英

How to get access to docker ENV variables in nodeJS application

I'm using this Dockerfile:

FROM node:8.4.0
COPY . /
ENV MONGO_URL=mongodb://mongo-container/data
ENV PORT=80
EXPOSE 80
CMD node /index.js

In the index.js file I want to check for the ENV variables. As the docker image is used in productive, I would run the app with development environment if the ENV variable is not set. Something similar to this:

index.js

const mongoUrl = ENV.MONGO_URL || 'mongodb://localhost:3001'

Running the Docker image should use the productive mongoDB, running locally should use localhost DB

process.env object contains all the user environment variables. Check link for more info https://nodejs.org/api/process.html#process_process_env

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM