I'm running Spring Cloud Eureka inside my Docker VM. I have services registering to it, but they use their IP adress from inside the Docker VM, but to be able to use them properly i need them to use the IP adress i can access from outside the VM.
For example inside my VM the register using 172.x.x.x and i can access the REST interface from my browser using 192.168.x.x.x. I need them to register as 192.168.x.x.x.
How can i tell my service to register with a specific IP adress?
You can configure it in your application.yml
:
eureka:
instance:
ipAddress: 192.168.x.x
Both previous answers are correct, but I'll make copy-pasting easier.
What you should do is add an environment variable with the host ip when starting your container and in your Spring Boot application.yml file include it.
application.yml
eureka:
instance:
# Necessary for Docker as it doesn't have DNS entries
prefer-ip-address: true
# Necessary for Docker otherwise you will get 127.0.0.x ip
ip-address: "${HOST}"
client:
serviceUrl:
# Location of your eureka server
defaultZone: http://192.168.0.107:8761/eureka/
Running with Docker
docker run -p <port>:<port> -e HOST='192.168.0.106' <image name>
Running with docker-compose
my_service:
image: image_name
environment:
- HOST=192.168.0.106
ports:
- your_port:container_port
You can use environment variables in the eureka configuration in application.yml
In the following example I am using the $HOST
and $PORT
environment variables to tell the eureka client what values to use. In my case, these variables are set by Mesos/Marathon. You may find other useful variables set by Docker.
The following works for me:
eureka:
client:
serviceUrl:
defaultZone: http://discovery.marathon.mesos:31444/eureka/
registerWithEureka: true
fetchRegistry: true
instance:
appname: configserver
health-check-url: /health
prefer-ip-address: true
ip-address: "${HOST}" # mesos/marathon populates this in the environment
non-secure-port: "${PORT}" # mesos/marathon populates this in the environment
Register to Eureka with a custom IP and a custom PORT:
server:
port: 18090
eureka:
instance:
prefer-ip-address: true
ip-address: 10.150.160.21
non-secure-port: 8080
I run into a situation where 10.150.160.21:8080 is mapped to 192.168.1.124:18090 over a firewall. The application is running on 192.168.1.124:18090 but have to register to Eureka with 10.150.160.21:8080.
It works for me.