Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springboot client unable register with Eureka using Docker container id

I have several microservices running in Docker Data Center. I have the same Eureka configuration across the services/applications. But some of the applications are registering with their eth0 IP address instead of the container ID.

I have tried setting the preferIpAddress as false but it is not enforcing all the time.

There is no pattern. The same service which registers with container ID during the previous deployment gets registered with IP the other time. I want my services to register always with its container id. Is there a way to enforce it or am I missing something?

Note: I have also cleared all the old docker images from the registry, deployment nodes and tried from the scratch as well.

Eureka Server Config:

eureka:
  instance:
    hostname: discovery
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/

Microservices client config (It is same across all the microservices)

eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/
  instance:
    preferIpAddress: false
    metadataMap:
      instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${random.value}}}

Eureka Dashboard Snapshot: enter image description here

like image 818
zeagord Avatar asked Jun 22 '17 02:06

zeagord


3 Answers

In docker the container-id will be set as the hostname of the container by default. Containers can talk with each other using container-id (or here hostname)

So this issue can be solved by preferring hostname instead of ip.

But only way of making sure that registration happens through hostname is by setting eureka.instance.hostname reference

In docker you can set the container-id at run-time from the hostname by using the entry point as shell script (for example start.sh) and the script should be something similar to

#!/bin/sh
export HOST_NAME=`hostname`
java -Djava.security.egd=file:/dev/./urandom -Xmx1024m -jar /app.jar

and make sure you add eureka.instance.hostname=${HOST_NAME} in your application.yml

or you can reuse the HOSTNAME variable which is set by default in Docker and configuration becomes eureka.instance.hostname=${HOSTNAME}

I have added the same info in documentation

Update: looks like this can also be fixed by using endpoint_mode: dnsrr in the compose file (have to confirm). Refer this

like image 164
Tummala Dhanvi Avatar answered Oct 14 '22 00:10

Tummala Dhanvi


Try setting eureka.instance.hostname in your client config.

like image 22
raiyan Avatar answered Oct 13 '22 22:10

raiyan


I know this is an old question but this answer might be helpful to someone. After trying out a number of solutions, I noticed that a hostname environment variable(HOSTNAME) already exists in Ubuntu. My docker containers use the openjdk:8-jdk-alpine base. If you're using an Ubuntu base you can simply solve this issue by adding the following parameters to the properties files of all your eureka clients:

eureka.instance.hostname=${HOSTNAME}
spring.cloud.client.hostname=${HOSTNAME}

The app will pick up the container's hostname which is the container id and pass that to the eureka server.

like image 26
euniceadu Avatar answered Oct 13 '22 22:10

euniceadu