Merc
Merc

Reputation: 17097

Environment variable in the Docker file for a specific RUN command

In my Dockerfile, I have:

ENV ENVIRONMENT=$ENVIRONMENT
CMD NODE_ENV=$ENVIRONMENT npm run serve

However, I need to run another command BEFORE serve, and make sure NODE_ENV Is set for that one too. I tried this:

ENV ENVIRONMENT=$ENVIRONMENT
RUN NODE_ENV=$ENVIRONMENT npm run upgrade
CMD NODE_ENV=$ENVIRONMENT npm run serve

However, NODE_ENV doesn't seem to be set for RUN.

What am I missing?

(Note: edited tag removed)

Upvotes: 0

Views: 53

Answers (1)

ckaserer
ckaserer

Reputation: 5742

Environment variables set in a RUN statement do not persist.

It's like you open a shell, set the environment variable and close the shell session again. The next shell will not have the environment variable you set in the previous shell session.

How to fix this? Add the NODE_ENV Variable as an ENV in your dockerfile

ENV ENVIRONMENT=$ENVIRONMENT \
    NODE_ENV=$ENVIRONMENT
RUN npm run upgrade
CMD npm run serve

Upvotes: 2

Related Questions