6

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?

1
  • What is the error? Commented Mar 20, 2019 at 18:58

2 Answers 2

16

Remove the equal sign

ENV NODE_ENV production
Sign up to request clarification or add additional context in comments.

2 Comments

But i have other variables like ENV UV_THREADPOOL_SIZE=5 which does not have any issue.
Syntax is not the problem in this case, ENV supports both styles as shown in here: docs.docker.com/engine/reference/builder/#env
4

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

8 Comments

npm 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
So 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
If i use prod instead of production, it works fine.
So it seems your application uses prod not production, am i correct ?
Yeah. So i had to change my code also top prod. If i use production in dockerfile, it does not work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.