1

With Maven and docker-maven-plugin I have configured Apache Kafka + ZooKeeper containers:

<image>
    <name>wurstmeister/zookeeper:${zookeeper.version}</name>
    <alias>zookeeper</alias>
    <run>
        <ports>
            <port>${zookeeper.port}:2181</port>
        </ports>
    </run>
</image>

<image>
    <name>wurstmeister/kafka:${kafka.version}</name>
    <alias>kafka</alias>
    <run>
        <ports>
            <port>9093:9092</port>
        </ports>
        <links>
            <link>zookeeper:zookeeper</link>
        </links>
        <env>
            <KAFKA_ADVERTISED_HOST_NAME>192.168.1.202</KAFKA_ADVERTISED_HOST_NAME>
            <KAFKA_ADVERTISED_PORT>9093</KAFKA_ADVERTISED_PORT>
            <KAFKA_ZOOKEEPER_CONNECT>zookeeper:${zookeeper.port}</KAFKA_ZOOKEEPER_CONNECT>
        </env>
    </run>
</image>

As you can see, in order to get it working I have to provide the actual IP address of my host system:

<KAFKA_ADVERTISED_HOST_NAME>192.168.1.202</KAFKA_ADVERTISED_HOST_NAME>

Is there any way in Maven or docker-maven-plugin to get this IP address automatically with no needs to hardcode it?

UPDATED

I found plugin that allows me to retrieve the host IP address:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>get-local-ip</id>
                    <goals>
                        <goal>local-ip</goal>
                    </goals>
                    <configuration>
                        <localIpProperty>local.ip</localIpProperty>
                    </configuration>
                </execution>
            </executions>
        </plugin>

but in order to use it I need to execute the build-helper:local-ip goal before rest of the Maven commands:

mvn build-helper:local-ip docker:start

How to bind this plugin with some phase/goal in order to invoke it automatically on some early initialization phase with no needs to invoke build-helper:local-ip manually each time?

alexanoid
  • 24,051
  • 54
  • 210
  • 410
  • For a docker-compose managed container the default network address of the host is 172.17.0.1 – Thorbjørn Ravn Andersen Nov 23 '17 at 10:23
  • Thanks for your comment. Unfortunately, my Kafka setup doesn't work properly with this IP address and only works with the actual host IP. Please see the following question for more details https://stackoverflow.com/questions/47439477/kafka-docker-and-port-forwarding-from-9092-to-9093 – alexanoid Nov 23 '17 at 12:26

2 Answers2

2

From the docs for build-helper-maven-plugin:

Binds by default to the lifecycle phase: process-test-classes

You can change it to bind to whatever phase you want, e.g.

            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>local-ip</goal>
                    </goals>
                    <phase>initialize</phase>
                </execution>
            </executions>
        </plugin>
skaffman
  • 398,947
  • 96
  • 818
  • 769
1

The local-ip from the build-helper-maven-plugin only returned 127.0.0.1.

In my use case the network address was necessary - something like 192.168.17.112.

<plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <source>
                    java.net.DatagramSocket socket = new java.net.DatagramSocket()
                    socket.connect(java.net.InetAddress.getByName('google.com'), 80)
                    project.properties['host.address'] = socket.getLocalAddress().getHostAddress()
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

The property host.address then contains the wanted network IP.

(Don't upvote me instead upvote the answer that helped me: Getting the IP address of the current machine using Java)

Sven Döring
  • 3,927
  • 2
  • 14
  • 17