Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register to Eureka from Docker with a custom IP

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?

like image 681
Blue Avatar asked May 20 '15 12:05

Blue


People also ask

How do I assign an IP address to a Docker container?

When you connect an existing container to a different network using docker network connect , you can use the --ip or --ip6 flags on that command to specify the container's IP address on the additional network. In the same way, a container's hostname defaults to be the container's ID in Docker.

Do Docker containers get their own IP?

By default, the container is assigned an IP address for every Docker network it connects to. And each network is created with a default subnet mask, using it as a pool later on to give away the IP addresses. Usually Docker uses the default 172.17.

How do I get my Docker host IP?

AFAIK, in the case of Docker for Linux (standard distribution), the IP address of the host will always be 172.17. 0.1 (on the main network of docker, see comments to learn more). This is true of containers attached to the docker0 default bridge interface.


2 Answers

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 includes 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 172.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
like image 92
Yoshua Nahar Avatar answered Nov 07 '22 05:11

Yoshua Nahar


You can configure it in your application.yml:

eureka:
    instance:
        ipAddress: 192.168.x.x
like image 36
Tyutyutyu Avatar answered Nov 07 '22 05:11

Tyutyutyu