Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup Node environment variable in Dockerfile for running node.js application?

Can anyone suggest how can I setup NODE_ENV variable in dockerfile when running a node app.

like image 256
djangogirl Avatar asked Mar 24 '17 05:03

djangogirl


People also ask

How do I specify node in Dockerfile?

Outside of a container, this is simple. You install the Node version that you want (use nvm to easily swap between them) and then specify npm version. Doing this inside a container, while possible, is a little bit trickier. You declare the version of Node you want to use with the “FROM” command.

Can I use environment variables in Dockerfile?

Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.

How do I set an environment variable in Docker?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

What Dockerfile instruction is used to set environment variables?

ENV lets you set environment variables that you can use later in your Dockerfile, and they'll also be set within the environment of your running container.


1 Answers

There a two ways, while building the image or when running the container.

For builds:

Add to your Dockerfile

ENV NODE_ENV=whatEver

Or use build arguments if you don't want them to stick during runtime Docker build --build-args NODE_ENV whatEver

When running:

Run your container with "-e"

docker run -e NODE_ENV=whatever mycontainer
like image 168
opHASnoNAME Avatar answered Oct 11 '22 12:10

opHASnoNAME