为什么我的节点容器会忽略我在docker-compose.yml中设置的环境变量? [英] Why does my node container ignore the environment variable I set in docker-compose.yml?
问题描述
在我的docker-compose.yml文件中,我设置了环境NODE_ENV
In my docker-compose.yml, I set environment NODE_ENV
node2:
image: ...
environment:
- "NODE_ENV=production"
我的Dockerfile,
My Dockerfile,
FROM node:latest
... //all the ususal stuff
CMD ["npm", "start"]
我的npm,
"scripts": {
"start": "NODE_ENV=development node --inspect ./bin/www"
},
但是当我运行docker-compose up时,我发现nodejs代码仍在开发中而不是在生产中运行。为什么?
But when I run docker-compose up, I found the nodejs code still runs in development, not in production. Why is that?
我的第二个问题是在没有docker的情况下运行我的nodejs(即使用 npm start
,我希望它在开发模式下运行,但在生产模式下运行docker?
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 -----
现在我的第一个问题是我的 npm start
重写了docker-中的NODE_ENV = production-
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.
推荐答案
本文 在Node.js中使用环境变量给了我
所以我首先使用了如建议的if-env 和我的 npm脚本
看起来像这样,
So I first used if-env as it suggested and my npm script
looks liked this,
"scripts": {
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:dev": "DEBUG=dummy-app:* node --inspect ./bin/www",
"start:prod": "node ./bin/www"
}
但作为 https://github.com/ericclemmons/if-env/issues/6 说,更重要的是,我需要简单设置环境变量并在必要时将其覆盖的方法现在我使用 per-env 和我的 npm脚本
看起来像这样,
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,
"per-env": {
"production": {
"DBPASS":"superman", //this can be overwrote again
},
"development":{
"DBPASS":"batman",
"DEBUG":"dummy-app:*",
}
},
"scripts": {
"start":"per-env",
"start:development": "node --inspect ./bin/www",
"start:production": "node ./bin/www"
},
这篇关于为什么我的节点容器会忽略我在docker-compose.yml中设置的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!