9951 explained code solutions for 126 technologies


reactjsHow can I set environment variables for a React.js application running in a Kubernetes cluster?


Environment variables can be set for a React.js application running in a Kubernetes cluster by either setting them as arguments when starting the container, or by setting them as ConfigMaps.

Setting them as arguments when starting the container:

kubectl run my-react-app --image=<image_name> --env="MY_VARIABLE_NAME=my_variable_value"

Setting them as ConfigMaps:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-react-app-config
data:
  MY_VARIABLE_NAME: my_variable_value

Then, you can mount the ConfigMap to the container using the --volume-mounts argument when starting the container:

kubectl run my-react-app --image=<image_name> --volume-mounts="name=my-react-app-config,mountPath=/etc/config"

The environment variables will then be available to the React.js application.

Helpful links

Edit this code on GitHub