In my docker-compose.yml, I set environment NODE_ENV
node2:
image: ...
environment:
- "NODE_ENV=production"
My Dockerfile,
FROM node:latest
... //all the ususal stuff
CMD ["npm", "start"]
My npm,
"scripts": {
"start": "NODE_ENV=development node --inspect ./bin/www"
},
But when I run docker-compose up, I found the nodejs code still runs in development, not in production. Why is that?
My second question is what is the proper way to achieve what I want to do here, when running my nodejs without docker, i.e. using npm start
, I want it to run in development mode, but running docker in production mode?
---- update -----
For my first question now I understand it is my npm start
overwrote NODE_ENV=production in docker-composer.yml not the other way around.
But for my second question, I am still looking for an easy solution.
Thanks for the answers I got so far.
My first guess would be to have different
npm script
entries in yourpackage.json
for the different environments likestart
for production andinspect
for developping/debugging - with accordingENV_VARS
.But if you would like to stick with one
script
for all, then you might consider using abash
default value like${parameter:-default}
forNODE_ENV
if it has not been set (by Docker):References:
http://www.tldp.org/LDP/abs/html/parameter-substitution.html
https://glebbahmutov.com/blog/shell-variables-in-npm-scripts/
This article "Working with Environment Variables in Node.js" gave me some thought.
So I first used if-env as it suggested and my
npm script
looks liked this,But as https://github.com/ericclemmons/if-env/issues/6 said and more importantly I need to an easy way to set environment variables and overwrite them if necessay now I am using per-env and my
npm script
looks like this,