Can anyone suggest how can I setup NODE_ENV variable in dockerfile when running a node app.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With