Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set NODE_ENV variable in production via docker file

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?

like image 937
Hacker Avatar asked Aug 26 '26 10:08

Hacker


2 Answers

Remove the equal sign

ENV NODE_ENV production
like image 119
Cody Swann Avatar answered Aug 28 '26 03:08

Cody Swann


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
like image 29
Mostafa Hussein Avatar answered Aug 28 '26 03:08

Mostafa Hussein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!