node.jsdockerdocker-cmd

Docker CMD with environment variable while running node application


I need to pass an environment variable to node like below.

RAZZLE_ENV=production node build/server.js

How I can achieve this with docker CMD command. My current config is like this:

CMD [ 'node', 'build/server.js' ]

I did change it to this:

CMD [ 'RAZZLE_ENV=production node', 'build/server.js' ]

But it does not work as expected and the container is not going to be created even.

UPDATE: the error is:

Cannot find module /app/RAZZLE_ENV=production node

Solution

  • Dockerfile

    # Use ARG so that it can be overridden at build time 
    ARG ARG_RAZZLE_ENV=development
    
    # Set environment variable based on ARG
    ENV RAZZLE_ENV=$ARG_RAZZLE_ENV
    
    CMD [ 'node', 'build/server.js' ]
    

    Pass ARG during build:

    docker build --build-arg ARG_RAZZLE_ENV=production . -t name:tag