<p>I am trying to pass an environment variable into my devcontainer that is the output of a command run on my dev machine. I have tried the following in my <code>devcontainer.json</code> with no luck:</p>
<pre class="lang-json prettyprint-override"><code>&quot;initializeCommand&quot;: &quot;export DOCKER_HOST_IP=\&quot;$(ifconfig | grep -E '([0-9]{1,3}.){3}[0-9]{1,3}' | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1)\&quot;&quot;,
  &quot;containerEnv&quot;: {
    &quot;DOCKER_HOST_IP1&quot;: &quot;${localEnv:DOCKER_HOST_IP}&quot;,
    &quot;DOCKER_HOST_IP2&quot;: &quot;${containerEnv:DOCKER_HOST_IP}&quot;
  },
</code></pre>
<p>and</p>
<pre class="lang-json prettyprint-override"><code>&quot;runArgs&quot;: [
  &quot;-e DOCKER_HOST_IP=\&quot;$(ifconfig | grep -E '([0-9]{1,3}.){3}[0-9]{1,3}' | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1)\&quot;
],
</code></pre>
<p>(the point of the <code>ifconfig</code>/<code>grep</code> piped command is to provide me with the IP of my docker host which is running via Docker for Desktop (Mac))</p>
<p><strong>Some more context</strong></p>
<p>Within my devcontainer I am running some <code>kubectl</code> deployments (to a cluster running on Docker for Desktop) where I would like to configure a <code>hostAlias</code> for a pod (<a href="https://kubernetes.io/docs/tasks/network/customize-hosts-file-for-pods/" rel="nofollow noreferrer">docs</a>) such that that pod will direct requests to <code>https://api.cancourier.local</code> to the ip of the docker host (which would then hit an ingress I have configured for that CNAME).</p>
<p>I <em>could</em> just pass in the output of the <code>ifconfig</code> command to my <code>kubectl</code> command when running from within the devcontainer. The problem is that I get two different results from this depending on whether I am running it on my host (<code>10.0.0.89</code>) or from within the devcontainer (<code>10.1.0.1</code>). <code>10.0.0.89</code> in this case is the &quot;correct&quot; IP as if I <code>curl</code> this from within my devcontainer, or my deployed pod, I get the response I'd expect from my ingress.</p>
<p>I'm also aware that I could just use the name of my k8s service (in this case <code>api</code>) to communicate between pods, but this isn't ideal. As for why, I'm running a Next.js application in a pod. The Next.js app on this pod has two &quot;contexts&quot;:</p>
<ol>
<li>my browser - the app serves up static HTML/JS to my browser where communicating with <code>https://api.cancourier.local</code> works fine</li>
<li>on the pod itself - running some things (ie. <code>_middleware</code>) on the pod itself, where the pod does not currently know what <code>https://api.cancourier.local</code></li>
</ol>
<p>What I was doing to temporarily get around this was to have a separate config on the pod, one for the &quot;browser context&quot; and the other for things running on the pod itself. This is less than ideal as when I go to deploy this Next.js app (to Vercel) it won't be an issue (as my API will be deployed on some publicly accessible CNAME). If I can accomplish what I was trying to do above, I'd be able to avoid this.</p>

Joey Orlando
<p>So I didn't end up finding a way to pass the output of a command run on the host machine as an env var into my <code>devcontainer</code>. However I did find a way to get the &quot;correct&quot; docker host IP and pass this along to my pod.</p>
<p>In my <code>devcontainer.json</code> I have this:</p>
<pre><code>&quot;runArgs&quot;: [
    // https://stackoverflow.com/a/43541732/3902555
    &quot;--add-host=api.cancourier.local:host-gateway&quot;,
    &quot;--add-host=cancourier.local:host-gateway&quot;
  ],
</code></pre>
<p>which augments the devcontainer's <code>/etc/hosts</code> with:</p>
<pre><code>192.168.65.2    api.cancourier.local
192.168.65.2    cancourier.local
</code></pre>
<p>then in my <code>Makefile</code> where I store my <code>kubectl</code> commands I am simply running:</p>
<pre><code>deploy-the-things:
    DOCKER_HOST_IP = $(shell cat /etc/hosts | grep 'api.cancourier.local' | awk '{print $$1}')
    helm upgrade $(helm_release_name) $(charts_location) \
        --install \
        --namespace=$(local_namespace) \
        --create-namespace \
        -f $(charts_location)/values.yaml \
        -f $(charts_location)/local.yaml \
        --set cwd=$(HOST_PROJECT_PATH) \
        --set dockerHostIp=$(DOCKER_HOST_IP) \
        --debug \
        --wait
</code></pre>
<p>then within my helm chart I can use the following for the pod running my Next.js app:</p>
<pre class="lang-yaml prettyprint-override"><code>      hostAliases:
        - ip: {{ .Values.dockerHostIp }}
          hostnames:
            - &quot;api.cancourier.local&quot;
</code></pre>
