set NODE_ENV variable in production via docker file

12,607

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
Share:
12,607
Hacker
Author by

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, 2022

Comments

  • Hacker
    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
      djheru about 5 years
      What is the error?
  • Hacker
    Hacker about 5 years
    But i have other variables like ENV UV_THREADPOOL_SIZE=5 which does not have any issue.
  • Hacker
    Hacker about 5 years
    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
  • Mostafa Hussein
    Mostafa Hussein about 5 years
    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
  • Mostafa Hussein
    Mostafa Hussein about 5 years
    Syntax is not the problem in this case, ENV supports both styles as shown in here: docs.docker.com/engine/reference/builder/#env
  • Hacker
    Hacker about 5 years
    If i use prod instead of production, it works fine.
  • Mostafa Hussein
    Mostafa Hussein about 5 years
    So it seems your application uses prod not production, am i correct ?
  • Hacker
    Hacker about 5 years
    Yeah. So i had to change my code also top prod. If i use production in dockerfile, it does not work.
  • Hacker
    Hacker about 5 years
    I changed in docker file and code also to prod and all worked fine.
  • Mostafa Hussein
    Mostafa Hussein about 5 years
    I 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
    Nick N over 3 years
    Using 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.