<p>How can I access environment variables in Vue, that are passed to the container at runtime and not during the build?</p>
<p>Stack is as follows:</p>
<ul>
<li>VueCLI 3.0.5</li>
<li>Docker</li>
<li>Kubernetes</li>
</ul>
<p>There are suggested solutions on stackoverflow and elsewhere to use .env file to pass variables (and using mode) but that's at build-time and gets baked into the docker image.</p>
<p>I would like to pass the variable into Vue at run-time as follows:</p>
<ul>
<li>Create Kubernetes ConfigMap (I get this right)</li>
<li>Pass ConfigMap value into K8s pod env variable when running deployment yaml file (I get this right)</li>
<li>Read from env variable created above eg. VUE_APP_MyURL and do something with that value in my Vue App (I DO NOT get this right)</li>
</ul>
<p>I've tried the following in helloworld.vue:</p>
<pre><code>&lt;template&gt;
&lt;div&gt;{{displayURL}}
  &lt;p&gt;Hello World&lt;/p&gt;
&lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
export default {  
    data() {
        return {
            displayURL: &quot;&quot;
        }
    },
    mounted() {
        console.log(&quot;check 1&quot;)
        this.displayURL=process.env.VUE_APP_ENV_MyURL
        console.log(process.env.VUE_APP_ENV_MyURL)
        console.log(&quot;check 3&quot;)
    }
}
&lt;/script&gt;
</code></pre>
<p>I get back &quot;undefined&quot; in the console log and nothing showing on the helloworld page.</p>
<p>I've also tried passing the value into a vue.config file and reading it from there. Same &quot;undefined&quot; result in console.log</p>
<pre><code>&lt;template&gt;
&lt;div&gt;{{displayURL}}
  &lt;p&gt;Hello World&lt;/p&gt;
&lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
const vueconfig = require('../../vue.config');
export default {  
    data() {
        return {
            displayURL: &quot;&quot;
        }
    },
    mounted() {
        console.log(&quot;check 1&quot;)
        this.displayURL=vueconfig.VUE_APP_MyURL
        console.log(vueconfig.VUE_APP_MyURL)
        console.log(&quot;check 3&quot;)
    }
}
&lt;/script&gt;
</code></pre>
<p>With vue.config looking like this:</p>
<pre><code>module.exports = {
    VUE_APP_MyURL: process.env.VUE_APP_ENV_MyURL
}
</code></pre>
<p>If I hardcode a value into VUE_APP_MyURL in the vue.config file it shows successfully on the helloworld page.</p>
<p>VUE_APP_ENV_MyURL is successfully populated with the correct value when I interrogate it: kubectl describe pod </p>
<p>process.env.VUE_APP_MyURL doesn't seem to successfully retrieve the value.</p>
<p>For what it is worth... I am able to use process.env.VUE_APP_3rdURL successfully to pass values into a Node.js app at runtime.</p>

Johann
<p>Create a file <code>config.js</code> with your desired configuration. We will use that later to create a config map that we deploy to Kubernetes. Put it into your your Vue.js project where your other JavaScript files are. Although we will exclude it later from minification, it is useful to have it there so that IDE tooling works with it.</p>
<pre class="lang-js prettyprint-override"><code>const config = (() =&gt; {
  return {
    &quot;VUE_APP_ENV_MyURL&quot;: &quot;...&quot;,
  };
})();
</code></pre>
<p>Now make sure that your script is excluded from minification. To do that, create a file vue.config.js with the following content that preserves our config file.</p>
<pre class="lang-js prettyprint-override"><code>const path = require(&quot;path&quot;);
module.exports = {
  publicPath: '/',
  configureWebpack: {
    module: {
      rules: [
        {
          test: /config.*config\.js$/,
          use: [
            {
              loader: 'file-loader',
              options: {
                name: 'config.js'
              },
            }
          ]
        }
      ]
    }
  }
}
</code></pre>
<p>In your index.html, add a script block to load the config file manually. Note that the config file won't be there as we just excluded it. Later, we will mount it from a <code>ConfigMap</code> into our container. In this example, we assume that we will mount it into the same directory as our HTML document.</p>
<pre class="lang-html prettyprint-override"><code>&lt;script src=&quot;&lt;%= BASE_URL %&gt;config.js&quot;&gt;&lt;/script&gt;
</code></pre>
<p>Change your code to use our runtime config:</p>
<pre class="lang-js prettyprint-override"><code>this.displayURL = config.VUE_APP_ENV_MyURL || process.env.VUE_APP_ENV_MyURL 
</code></pre>
<p>In Kubernetes, create a config map that uses the content your config file. Of course, you wanna read the content from your config file.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  ...
data:
  config.js: |
    var config = (() =&gt; {
      return {
        &quot;VUE_APP_ENV_MyURL&quot;: &quot;...&quot;,
      };
    })();
</code></pre>
<p>Reference the config map in your deployment. This mounts the config map as a file into your container. The <code>mountPath</code> Already contains our minified index.html. We mount the config file that we referenced before.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  ...
spec:
  ...
  template:
    ...
    spec:
      volumes:
        - name: config-volume
          configMap:
            name: ...
      containers:
        - ...
          volumeMounts:
                - name: config-volume
                  mountPath: /usr/share/nginx/html/config.js
                  subPath: config.js
</code></pre>
<p>Now you can access the config file at <code>&lt;Base URL&gt;/config.js</code> and you should see the exact content that you put into the ConfigMap entry. Your HTML document loads that config map as it loads the rest of your minified Vue.js code. Voila!</p>
