<p>I have a ReactJS application and I'm deploying it using Kubernetes.</p>

<p>I'm trying to wrap my head around how to inject environment variables into my <code>config.js</code> file from within the Kubernetes deployment file.</p>

<p>I currently have these:
<code>config.js</code> file:</p>

<pre><code>export const CLIENT_API_ENDPOINT = { 
    default:process.env.URL_TO_SERVICE,
};
</code></pre>

<p>and here's my Kubernetes deployment variables:</p>

<pre><code>"spec": {
  "containers": [
    {
      "name": "container_name",
      "image": "image_name",
      "env": [
        {
          "name": "URL_TO_SERVICE",
          "value": "https://www.myurl.com"
        }
      ]
</code></pre>

<p>Kinda clueless of why I can't see the environment variable in my <code>config.js</code> file. Any help would be highly appreciated.</p>

<p>Here's my dockerfile:</p>

<pre><code># Dockerfile (tag: v3)
FROM node:9.3.0
RUN npm install webpack -g
WORKDIR /tmp
COPY package.json /tmp/
RUN npm config set registry http://registry.npmjs.org/ &amp;&amp; npm install
WORKDIR /usr/src/app
COPY . /usr/src/app/
RUN cp -a /tmp/node_modules /usr/src/app/
#RUN webpack
ENV NODE_ENV=production
ENV PORT=4000
#CMD [ "/usr/local/bin/node", "./index.js" ]
ENTRYPOINT npm start
EXPOSE 4000
</code></pre>

<p>The kubernetes environment variables are available in your container.  So you would think the task here  is a version of getting server side configuration variables shipped to your client side code.</p>

<p>But, If your react application is running in a container, you are most likely running your javascript build pipeline when you build the docker image. Something like this:</p>

<pre><code>RUN npm run build

# Run app using nodemon
CMD [ "npm", "start" ]
</code></pre>

<p>When docker is building your container, the environment variables injected by kubernetes aren't yet yet available. They won't exist until you run the built container on a cluster.</p>

<p>One solution, and this is maybe your shortest path, is to stop building your client side code in the docker file and combine the build and run steps in npm start command . Something like this if you are using webpack:</p>

<pre><code>"start": "webpack -p --progress --config webpack.production.config.js &amp;&amp; node index.js"
</code></pre>

<p>If you go this route, then you can use any of the well documented techniques for shipping server side environment variables to your client during the build step : <a href="https://stackoverflow.com/questions/30030031/passing-environment-dependent-variables-in-webpack">Passing environment-dependent variables in webpack</a>.  There are similar techniques and tools for all other javascript build tools.</p>

<p>Two: If you are running node, you can continue building your client app in the container, but have the node app write a config.js to the file system on the startup of the node application.</p>

<p>You could do even more complicated things like exposing your config via an api (a variation on the second approach), but this seems like throwing good money after bad.  </p>

<p>I wonder if there isn't an easier way.  If you have a purely client side app, why not just deploy it as a static site to, say, an amazon or gcloud bucket, firebase, or netlify?  This way you just run the build process and deploy to the correct environment. no container needed.</p>
