TLDR; If in eureka you cant reach docker container instance, use docker run --network host

In last weeks I tried to go back to the cloud-basics (developing microservice with vanilla docker + jenkins). Since I am mostly working with Openshift/Kubernetes, there is a lot of things that are happening in the background, which are pretty well hidden. This time, I wanted to create a simple microservice application (its on github), which can be deployed on my own VMs in Docker (as every self respecting geek, I have my own server).

So far so good. But my brilliant plan to deploy everything automatically on Docker and enjoy all the eureka/feign/ribbon goodies, had couple of tricky issues in it.

The Issue:

Eureka instances (clients) are presenting themselves with hostnames. In theory everything is okay, until you find that your hostname looks like "2eba29169a0d". It is because, every docker container gets its own hostname. This, obviously, doesnt look nice on eureka overview, so we had to change something. You can change it with eureka.instance.hostname=not-docker-host, but sadly it didnt solve the problem (it effected only the instance label on eureka server console). If you click on the instance itself, you still get redirected to rubbish URL..

After some googling, I could solve the problem with eureka.instance.preferIpAddress=true .In this case, the instance (eureka client) will get connections via ip (avoid docker random hostname issues). Sadly, after brief testing I still could not connect to my instances via eureka. The IP that the container had assigned, of course, was from docker bridge ( 172.17.0.0/16) (its default networking plugin in Docker). Initially, I wanted to overwrite the IP of the instance with something meaningful, like docker-host IP, but it would defeat the whole microservice purpose (I want my containers to choose whatever VM they want!).


After some brainstorming, I decided to get the Docker Host IP automatically and overwrite that in applications startup-bean. Unhappily, Docker does pretty good job of masking the host networking (surprise!), and I came to the conclusion that inside the container I wont find any interesting Information in regards of the host networking.

Sadly, google was not helpful either (only some issues). After some googling “expose host interface docker” I got only this, which is way too technical (I just wanted to get god damned host IP!!!). In the edge of almost giving up (nearly transfered everything to Openshift…), I tried one last time to google: aaaaand BINGO!!!

There is super simple command (somehow not well documented), to expose to containers its host network! (in case you have seen it - its “–network host”).

Afterwards adding this magic command, my container was able to fully interact with wild world (including hostname, which is not really super exciting). After this interesting journey of googling, my container was able to fully register itself to eureka server and it was able to receive stuff from other containers via feign (docker & non-docker services). Of course, its not exactly following the docker philosophy (the goal is abstract everything away, not to expose stuff), but if the eureka client needs the information about its routable from outside network (some services in the app are not containerised), I think its the most efficient choice.

And that was my journey to the discovering docker containers in eureka world.

P.S. In openshift/kubernetes there are things that take care of this stuff “out of the box”….