2

I am in reference to Benjamin Muschko's Gradle-Docker plugin.

I use Mac OS X but some of my colleagues use Linux.

I would like to find a way to use the above plugin in order to retrieve the docker server IP and set that as an environment variable to use by my Spring Boot application.

I could do it manually by issuing a docker-machine ip <machineName> but I need to do this programmatically through gradle so that I can just run my app from gradle which will:

  • Retrieve the docker server IP
  • Set it as an env variable (e.g. $DOCKER_IP)
  • My Spring Boot app will then use that variable in order to connect to Mysql and Elasticsearch on the docker host.

Is this possible to do that in a generic way so that it will work under Mac Os X and Linux?

Opal
  • 81,889
  • 28
  • 189
  • 210
balteo
  • 23,602
  • 63
  • 219
  • 412

1 Answers1

1

Basically, yes. It can be done programatically. You need to execute the command in gradle to get the docker IP. However environment variables can not be set in JVM languages, you can use e.g. system properties. The following snippet might be helpful:

task setDockerIP << {
   def dockerIP = ['boot2docker', 'ip'].execute().text
   System.setProperty('dockerIP', dockerIP)
}

task printProp(dependsOn: 'setDockerIP') << {
   System.properties.each {
      println "$it.key -> $it.value"
   }
}
Opal
  • 81,889
  • 28
  • 189
  • 210
  • Thanks a lot Opal. Will this be portable to linux? – balteo Aug 14 '15 at 10:15
  • It should as far as the command is execute in exactly the same way. – Opal Aug 14 '15 at 10:17
  • Ummm... I believe linux doesn't use `docker-machine ip ` or the like. How then will someone using linux retrieve the IP for the docker host? – balteo Aug 14 '15 at 10:23
  • On linux env IP of the docker host is an IP of the machine that's running docker. So, in such case, you just need get the IP of the workstation. See: http://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java Is that correct? – Opal Aug 14 '15 at 10:26
  • You mean `localhost` then? – balteo Aug 14 '15 at 10:26
  • Not sure about it, but it may work with localhost as well. – Opal Aug 14 '15 at 10:27