set NODE_ENV variable in production via docker file
Solution 1
Remove the equal sign
ENV NODE_ENV production
Solution 2
Your Dockerfile should work, are you sure there isn't anything else inside the application, maybe package.json could be override it ?
I have tested it and it works correctly
$ echo $NODE_ENV
production
Also I would suggest to set the NODE_ENV
variable while running the container as it would be easier to modify, I am aware that you can override it anyway if needed but setting it on the run-time will make your image with less layers. you can use the command below
-e can be used to pass an environment variable and can be passed multiple times if you have more than one variable
docker run -e NODE_ENV=production $IMAGENAME
Hacker
Hi, This is Pradeep here. Bacially a web developer. Work experience completely on LAMP Stack, Drupal. "I'm convinced that the only thing that kept me going was that I loved what I did." - Steve Jobs 1955-2011
Updated on June 15, 2022Comments
-
Hacker almost 2 years
I am trying to use below code below code in nodejs
if (process.env.NODE_ENV !== 'production')
I tried to set NODE_ENV variable from docker file like below.
FROM collinestes/docker-node-oracle:10-slim ENV NODE_ENV=production EXPOSE 8085 CMD ["npm","start"]
If i to run my docker image it does not start and throws error. If i remove NODE_ENV all runs fine. Is it the right way to setting NODE_ENV from dockerfile?
-
djheru about 5 yearsWhat is the error?
-
-
Hacker about 5 yearsBut i have other variables like ENV UV_THREADPOOL_SIZE=5 which does not have any issue.
-
Hacker about 5 yearsnpm start has babel-node --presets es2015 index.js and when i have that variable in docker file it throws up error like babel-node not found
-
Mostafa Hussein about 5 yearsSo maybe you need to configure
PATH
to make it work. This is just my guessing as I don't know more information about your actual Dockerfile -
Mostafa Hussein about 5 yearsSyntax is not the problem in this case,
ENV
supports both styles as shown in here: docs.docker.com/engine/reference/builder/#env -
Hacker about 5 yearsIf i use prod instead of production, it works fine.
-
Mostafa Hussein about 5 yearsSo it seems your application uses
prod
notproduction
, am i correct ? -
Hacker about 5 yearsYeah. So i had to change my code also top prod. If i use production in dockerfile, it does not work.
-
Hacker about 5 yearsI changed in docker file and code also to prod and all worked fine.
-
Mostafa Hussein about 5 yearsI cannot tell without knowing the framework. have you reviewed the framework's documentation or github issues ? maybe they have something special regarding the node env variable ?
-
Nick N over 3 yearsUsing AWS ECS Docker instances, I ended up adding an Environment Variable in the Task Definition key NODE_ENV and value production. Things worked fine. Somehow I stumbled onto this page with my issue, so documenting it for others.