We are building containers using spring boot and eureka. Inside Rancher we can’t resolve the hostname of the container (the eureka default) so we register using the rancher ip. The following script does this and is designated the ENTRYPOINT. We then build our application image and pass CMD as java -jar myapp.jar. This should append --eureka.instance.hostname= to the command.
Sadly the curl doesn’t work the first time, and only the first time so it doesn’t look like a timing issue. Can anyone explain why?
#!/bin/bash
# this script is designed as a wrapper for a spring boot application which may or may
# not run under Rancher. In Rancher we need to explicitly set the Eureka instance name
# to our Rancher IP which resolves within the Rancher world. On the outside we don't.
# BODGE - rancher-metadata doesn't work the first time.
curl -s http://rancher-metadata/latest/self/container/primary_ip > /dev/null
EUREKA_INSTANCE_HOSTNAME=$(curl -s http://rancher-metadata/latest/self/container/primary_ip)
if [ $? -eq 0 ]; then
OPTS="--eureka.instance.hostname=$EUREKA_INSTANCE_HOSTNAME"
fi
# now hand over to the CMD[] string. Use exec so that the running process is pid 1 and
# will get signals correctly from docker
exec $* $OPTS