<p>I'm creating a kind cluster with <code>kind create cluster --name kind</code> and I want to access it from another docker container but when I try to apply a Kubernetes file from a container (<code>kubectl apply -f deployment.yml</code>) I got this error:</p>
<pre><code>The connection to the server 127.0.0.1:6445 was refused - did you specify the right host or port?
</code></pre>
<p>Indeed when I try to curl kind control-plane from a container, it's unreachable.</p>
<pre><code>&gt; docker run --entrypoint curl curlimages/curl:latest 127.0.0.1:6445
curl: (7) Failed to connect to 127.0.0.1 port 6445 after 0 ms: Connection refused
</code></pre>
<p>However kind control-plane is publishing to the right port but only to the localhost.</p>
<pre><code>&gt; docker ps --format &quot;table {{.Image}}\t{{.Ports}}&quot;
IMAGE                  PORTS
kindest/node:v1.23.4   127.0.0.1:6445-&gt;6443/tcp
</code></pre>
<p>Currently the only solution I found is to set the host network mode.</p>
<pre><code>&gt; docker run --network host --entrypoint curl curlimages/curl:latest 127.0.0.1:6445
Client sent an HTTP request to an HTTPS server.
</code></pre>
<p>This solution don't look to be the most secure. Is there another way like connecting the kind network to my container or something like that that I missed ?</p>

Vincent Andrieu
<p>Don't have enough rep to comment on the other answer, but wanted to comment on what ultimately worked for me.</p>
<h2>Takeaways</h2>
<ul>
<li>Kind cluster running in it own bridge network <code>kind</code></li>
<li>Service with kubernetes client running in another container with a mounted kube config volume</li>
<li>As described above the containers need to be in the same network unless you want your service to run in the <code>host</code> network.</li>
<li>The server address for the kubeconfig is the container name + internal port e.g. <code>kind-control-plane:6443</code>. The port is NOT the exposed port in the example below <code>6443</code> <strong>NOT</strong> <code>38669</code>
<pre><code>CONTAINER ID   IMAGE                                PORTS
7f2ee0c1bd9a   kindest/node:v1.25.3                 127.0.0.1:38669-&gt;6443/tcp
</code></pre>
</li>
</ul>
<h2>Kube config for the container</h2>
<pre class="lang-yaml prettyprint-override"><code># path/to/some/kube/config
apiVersion: v1
clusters:
  - cluster:
      insecure-skip-tls-verify: true # Don't use in Prod equivalent of --insecure on cli
      server: https://&lt;kind-control-plane container name&gt;:6443 # NOTE port is internal container port
    name: kind-kind # or whatever
contexts:
  - context:
      cluster: kind-kind
      user: &lt;some-service-account&gt;
    name: kind-kind # or whatever
current-context: kind-kind
kind: Config
preferences: {}
users:
  - name: &lt;some-service-account&gt;
    user:
      token: &lt;TOKEN&gt;
</code></pre>
<h2>Docker container stuff</h2>
<ul>
<li><p>If using docker-compose you can add the kind network to the container such as:</p>
<pre class="lang-yaml prettyprint-override"><code>#docker-compose.yml
services:
  foobar:
    build:
      context: ./.config
    networks:
      - kind # add this container to the kind network
    volumes:
      - path/to/some/kube/config:/somewhere/in/the/container
networks:
  kind: # define the kind network
    external: true # specifies that the network already exists in docker
</code></pre>
</li>
<li><p>If running a new container:</p>
<pre class="lang-bash prettyprint-override"><code>docker run --network kind -v path/to/some/kube/config:/somewhere/in/the/container &lt;image&gt;
</code></pre>
</li>
<li><p>Container already running?</p>
<pre class="lang-bash prettyprint-override"><code>docker network connect kind &lt;container name&gt;
</code></pre>
</li>
</ul>
