Search code examples
mavendockerfabric8maven-docker-plugin

maven-docker-plugin: how to get container ip address


I am new to Docker and fabric8's maven-docker-plugin. I want to do some integration tests using two containers, one with my webapp deployed and one with database.

UPDATE: I can run both containers using these commands:

docker run --name db -p 27017:27017 mongo
docker run --name as -p 8080:8080 --link db mycustom/wildfly /opt/jboss/wildfly/standalone/bin/standalone.sh -b 0.0.0.0 -Ddbhost=172.17.0.2 -Ddbport=27017

This the dockerfile I use:

FROM fpezzati/wildflyogm
ARG webapp
COPY $webapp /opt/jboss/wildfly/standalone/deployments/
EXPOSE 8080
CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0"]

Now I want to run both containers using the fabric8 maven-docker-plugin. At this point I have to figure out about how to express a run command that is able to take the two arguments -Ddbhost=${dbhost} -Ddbport=${dbport} using the maven-docker-plugin, where ${dbhost} and ${dbport} should be two maven properties having database container ip address and exposed port. How can I express a run command as I do in the by-hand example and get my webapp aware about database ip address using the fabric8 maven-docker-plugin?

UPDATE: I clean up some mess I did in my pom.xml and in dockerfile. Now I can pass arguments by using environment variables. The only issue left is I can't get database container's ip address even if it is linked into webapp container's <run>...</run> configuration.

Here is my project's pom. I am using maven 3.3.9, docker 1.12.5 and fabric8's maven-docker-plugin 0.19.0.


Solution

  • Ok I got it. As you link container A to container B, Docker add A as hostname to B's etc/hosts file. So you don't have to figure out container's ip address you can just use its name. The same happens on fabric8 maven-docker-plugin, which is a great plugin. You use

    <links>
      <link>A</link>
    </links>
    

    to link A to B and B can use A:27017 to reach service on A's exposed port, so in my pom I pass the string A by enviroment variable instead of container A's ip address. I push solution to my github repo.