<p>I have a Kubernetes v1.18.3 cluster and the workers have a Docker v19.03.6 dameon.  </p>

<p>I'm looking for a way to automatically inject the <code>HTTP_PROXY</code> and <code>HTTPS_PROXY</code> to every container that Kubernetes creates.  </p>

<p>I tried creating a <code>~/.docker/config.json</code> file, but it didn't work.  </p>

<p>What would be the proper way to accomplish it?  </p>

areller
<p>Was interested in your case, even reproduced with the same docker and k8s versions...
Used official <a href="https://docs.docker.com/network/proxy/" rel="nofollow noreferrer">Configure Docker to use a proxy server</a> documentation to set proxy for docker in <code>~/.docker/config.json</code> </p>

<blockquote>
  <p>Configure the Docker client On the Docker client, create or edit the
  file ~/.docker/config.json in the home directory of the user which
  starts containers. Add JSON such as the following, substituting the
  type of proxy with httpsProxy or ftpProxy if necessary, and
  substituting the address and port of the proxy server. You can
  configure multiple proxy servers at the same time.</p>
</blockquote>

<pre><code>{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://127.0.0.1:3001",
     "httpsProxy": "http://127.0.0.1:3001",
     "noProxy": "*.test.example.com,.example2.com"
   }
 }
}
</code></pre>

<blockquote>
  <p>Save the file.
       When you create or start new containers, the environment variables are set automatically within the container.</p>
</blockquote>

<p>My config was:</p>

<pre><code>{
  "proxies": {
    "default": {
      "httpProxy": "http://user:pass@my.proxy.domain.com",
      "httpsProxy": "http://user:pass@my.proxy.domain.com"
    }
  }
}
</code></pre>

<p>So basically after setting above in <code>~/.docker/config.json</code>, the proxy-server will be automatically used when starting a brand new containers.</p>

<p>In my case that worked, I can verify that by using cli and creating e.g busybox container.</p>

<pre><code>$ docker container run --rm busybox env
HTTP_PROXY=http://user:pass@my.proxy.domain.com
http_proxy=http://user:pass@my.proxy.domain.com
HTTPS_PROXY=http://user:pass@my.proxy.domain.com
https_proxy=http://user:pass@my.proxy.domain.com
HOME=/root
</code></pre>

<p>Please keep in mind that there should be issues with next part:</p>

<blockquote>
  <p>On the Docker client, create or edit the file ~/.docker/config.json <strong>in
  the home directory of the user which starts containers</strong>.</p>
</blockquote>

<p>Be careful with <code>USER</code> you use and make sure your <code>HOME</code> env is set to correct one.</p>

<p>Links to github almost similar issue and ways to resolve:</p>

<p>1) <a href="https://github.com/kubernetes/kubernetes/issues/45487#issuecomment-312042754" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/45487#issuecomment-312042754</a></p>

<blockquote>
  <p>I dug into this a bit and the issue for me was that the HOME
  environment variable was empty when kubelet was launched through a
  systemd unit. While it's not documented this way, loading the
  configuration from /root/docker/config.json or /root/.dockercfg
  requires that HOME=/root</p>
  
  <p>Setting User=root in the [Service] declaration fixed it up for me.</p>
</blockquote>

<p>2) <a href="https://github.com/kubernetes/kubernetes/issues/45487#issuecomment-378116386" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/45487#issuecomment-378116386</a></p>

<p>3) <a href="https://github.com/kubernetes/kubernetes/issues/45487#issuecomment-464516064" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/45487#issuecomment-464516064</a> (partial info)</p>

<pre><code>(3) vim /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

Add User=root
File looks kind of like this
[Service]
User=root
Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
KUBELET_KUBEADM_ARGS variable dynamically
EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
KUBELET_EXTRA_ARGS should be sourced from this file.
EnvironmentFile=-/etc/default/kubelet
ExecStart=
ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS
(4) Reload and restart kubelet

systemctl daemon-reload
systemctl restart kubelet
</code></pre>

<p>Exactly in my case everything worked fine from scratch. So read carefully and check points I highlighted. Most probably you have very tiny problem/typo, cause it works as expected.</p>

<p>I hope my investigation will help you</p>
