<p>I am running Docker Desktop on Windows 10</p>
<ul>
<li>I have Docket Kubernetes enabled</li>
<li>In my Kubernetes cluster I have a single pod with a single container running Postgres, built with kubectl apply  (call this <code>inside</code>)</li>
<li>Outside of Kubernetes, I have another container running postgres built with docker desktop (call this <code>outside</code>).</li>
</ul>
<p>I want to be able to connect to the <code>outside</code> postgres server by name from the <code>inside</code> to the <code>outside</code></p>
<p>My kubernetes yaml configuration that builds <code>inside</code>:</p>
<pre><code>apiVersion: v1
kind: PersistentVolume # Create a PV
metadata:
  name: postgresql-data # Sets PV's name
  labels:
    type: local # Sets PV's type to local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi # Sets PV Volume
  accessModes:
    - ReadWriteOnce
  hostPath: #/run/desktop/mnt/host/c points to c:\ on the windows host
    path: &quot;/run/desktop/mnt/host/c/mk/data/volume&quot; # Sets the volume's path
---
apiVersion: v1
kind: PersistentVolumeClaim # Create PVC
metadata:
  name: postgresql-data-claim # Sets name of PV
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce # Sets read and write access
  resources:
    requests:
      storage: 10Gi # Sets volume size    
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-secret
  labels:
    app: postgres
data:
  POSTGRES_DB: appdb
  POSTGRES_USER: user
  POSTGRES_PASSWORD: password
---
apiVersion: apps/v1
kind: Deployment # Create a deployment
metadata:
  name: postgres # Set the name of the deployment
spec:
  replicas: 1 
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:12.10 # Docker image
          imagePullPolicy: &quot;IfNotPresent&quot;
          ports:
            - containerPort: 5432 # Exposing the container port 5432 for PostgreSQL client connections.
          envFrom:
            - configMapRef:
                name: postgres-secret # Using the ConfigMap postgres-secret
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgresdata
      volumes:
        - name: postgresdata
          persistentVolumeClaim:
            claimName: postgresql-data-claim
---
apiVersion: v1
kind: Service # Create service
metadata:
  name: postgres # Sets the service name
  labels:
    app: postgres # Defines app to create service for
spec:
  type: NodePort # Sets the service type
  ports:
    - protocol: TCP # Sets the port to run the postgres application
      port: 5432
      targetPort: 5432
      nodePort: 31225
  selector:
    app: postgres            

</code></pre>
<p>My docker-compose that builds <code>outside</code></p>
<pre><code>version: '3.6'
services:
  client_portal:
    image: postgres:13.3
    ports: 
      - 5435:5432
    environment:
      POSTGRES_PASSWORD: appdb
      POSTGRES_USER: user
      POSTGRES_DB: password
</code></pre>
<p>From the windows host, I am able to access <code>inside</code> at <code>localhost:5432</code>.  <code>Outside</code> is accessed at  <code>localhost:31225</code></p>
<p>From <code>inside</code> I can access <code>outside by it's ip address.  I got the ip address by running hostname -i on </code>outside`'s console (172.18.0.3) .</p>
<p>This works (on <code>inside</code>):</p>
<pre><code>psql postgresql://user:password@172.18.0.3:5432/appdb
</code></pre>
<p>I want to address it by name (not ip) so that when I share this with someone else or the ip address of outside changes, my config does not change.</p>

JoeLeBaron
<p>You can check the node object in your cluster to see what is the <code>hostname</code> for this node. It should probably be <code>docker-desktop</code>. You can check this as:</p>
<pre><code>kubectl get node docker-desktop -oyaml
</code></pre>
<p>This should provide an output similar to the following:</p>
<pre><code>...
status:
    addresses:
    - address: 192.168.65.4
      type: InternalIP
    - address: docker-desktop
      type: Hostname
...
</code></pre>
<p>I believe in your case the <code>InternalIP</code> address is <code>172.18.0.3</code>. It is the one you can use to address the <code>node</code> from inside the cluster.</p>
<p>Assuming that when you provide this setup to someone else, and they will also use docker desktop, the hostname should be <code>docker-desktop</code>.</p>
<p>You can simply use the following command from <code>inside</code>, safely:</p>
<pre><code>psql postgresql://user:password@docker-desktop:5432/appdb
</code></pre>
<p>Just make sure <code>docker-desktop</code> is the <code>hostname</code> for the node, as checked earlier.</p>
