java.net.UnknownHostException: postgres

6/4/2019

I am trying to deploy spring boot application on kubernetes and connect to postgres which is on my localhost.

Spring boot deployment is fine. for postgres i did as

kind: Service
apiVersion: v1
metadata:
  name: postgres
  namespace: default
spec:
  type: ExternalName
  # https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds
  externalName: host.docker.internal
  ports:
    - name: port
      port: 5432

kubectl get svc gives

postgres           ExternalName   <none>           host.docker.internal   5432/TCP         9m13s

I am database url as

jdbc:postgresql://postgres:5432/postgres

So spring boot deployed logs give exception as

 java.net.UnknownHostException: postgres
-- Dhanraj
kubernetes

1 Answer

6/4/2019

This feature works only for Mac and Win Docker, so if you are running it on Linux -it's not gonna work.

Linux support for this hostname has been implemented and is likely coming in the next days/weeks: docker/libnetwork#2348

There are still some workarounds, while it's not released for Linux

I. you can create a file named .ddev/docker-compose.xdebug.yaml in your project with these contents:

# For Linux users; Docker has not yet supported the "host.docker.internal"
# convention that is used in ddev v0.18.0. But if you add this file as
# .ddev/docker-compose.xdebug.yaml it should sort that out.
# Note that the IP address in your environment might not be 172.17.0.1,
# Find out what address to use with "ifconfig docker0" or "ip addr show docker0 | grep inet"
version: "3"
services:
  web:
    extra_hosts:
      # Find out what address to use with "ifconfig docker0" or "ip addr show docker0 | grep inet"
      - "host.docker.internal:172.17.0.1"

II. Use this image to forward TCP and UDP traffic to the docker host

-- A_Suh
Source: StackOverflow