How to get docker ip and port in spring boot bootstrap.yml file
The problem that you might face when running inside a Docker container is that when trying to recover the hostname
or IP Address
, you will get the internal Docker hostname
and internal IP Address
.
A solution that I have used in similar problems, is to:
- Inject the hostname of the Docker Host when creating the container.
- Expose the container port in the Docker Host
For instance:
Set up the property to take the address from outside:
discovery: health-check-path: /actuator/health healthCheckInterval: 20s scheme: http enabled: true address:${HOSTNAME:server.address}
When running your container, provide the value of the
HOSTNAME
property:docker run -e HOSTNAME=myhostname.mydomain.com -p 8500:8500 my_docker_image
With this, the endpoint you are defining in your service will be accessible for anyone making health checks from outside.
Hope it helps.