Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

container ip #198

Closed
lappodeveloper opened this issue Jun 19, 2015 · 22 comments
Closed

container ip #198

lappodeveloper opened this issue Jun 19, 2015 · 22 comments
Labels
Milestone

Comments

@lappodeveloper
Copy link

Inside pom.xml:
with the property "docker.host.address" I can get the IP of the host running docker daemon.
with the property "host.port" I can get the published port assigned to the container started with the plugin.

Is there a property to get the IP assigned by docker to the container started by the plugin ?

@jgangemi
Copy link
Collaborator

at the moment, no. but is something that could be added.

you would need to define what property you want to use for the container's address (similar to how the random port mapping works)

@rhuss
Copy link
Collaborator

rhuss commented Jun 19, 2015

At the moment ${docker.host.property} is the only variable which is implicitly set. Please note, if you use a Unix socket for accessing the docker host, this variable is set to localhost (which should work for most of the time).

Adding another fixed variable shouldn't be a problem (however I guess we have to issue an extrac REST call to get to this information). However, I wonder what your use case is ? Isn't it, that from the outside the docker host IP should be sufficient for accessing your container ? Why and where do you need the internal IP ? (just curious)

@lappodeveloper
Copy link
Author

Thank you for the answer.
I configured the plugin to execute system test, similar to the "cargo+tomcat" pattern.
The plugin starts and stop the container and the failsafe plugin execute my tests.
If I build the project locally, there is no problem. All tests are ok.
The problem is with the jenkins instance, which is a running docker container too. The tests fails because there is no docker running on localhost.
From the jenkins instance I could reach all other docker containers (if the ip is known), but I have to find out how to reach the main host where docker started the container for the tests.
Maybe I could give the host IP as parameter for my tests, as optional parameter. I'll try it.

@rhuss
Copy link
Collaborator

rhuss commented Jun 22, 2015

I'm confused ;-)

You start a Jenkins Job which runs mvn docker:start. Jenkins itself runs within the container as the maven command does. The docker-maven-plugin needs to access the Docker host somehow to start you appserver container, right ? Therefore the plugin already needs to know the Docker hosts' address, or I'm a wrong ? This host (stored then in ${docker.host}) is also the one how you Tomcat etc. should be reachable.

@lappodeveloper
Copy link
Author

Good question : -)
To manage the docker functions (start/stop containers) from a running container (and from the docker-maven-plugin used in the jenkins job) I wrote a new container (nginx as proxy to the docker Unix socket /var/run/docker, passed as "-v" by starting the container and listening on specific port e.g. 4232) to expose the REST Api of Docker to all other containers.
Let's say "container N".

The jenkins and the job using the docker-maven-plugin run in another container.
Let's say "Container J".

The docker-maven-plugin ("container J") start and stop the docker containers contacting the IP of "Container N" on port 4232 (property "dockerHost" in the docker-maven-plugin, which I can override with a maven environment variable), which correspond to the standard Docker Remote API, as configured in nginx.
In this way the "container J" doesn't need to know the Host IP, where docker is running and I don't need to configure (and expose) the docker Remote REST API directly on the Host (the container N doesn't publish any ports, as the connection to Container J -> N ist assured by the "--link" property by starting Container J).

@rhuss
Copy link
Collaborator

rhuss commented Jun 22, 2015

Ah, ok. You are doing some proxying in between (so you access container N as a proxy for the Docker host which is only accessible via Unix socket. BTW, the docker maven plugin supports now also access to Unix sockets directly since 0.12.0)

For me it looks a little bit like a quite complex setup ;-) (which is probably not so common in general) so I wonder whether its worth to introduce this extra step for querying the docker daemon about more information of the container. Maybe we could do this an option like 'exposeContainerInfo'. However then there is still the problem how this properties should be exposed since there can be multiple containers configured. So probably with the container name as prefix (like ${tomcat.ip}).

Maybe you could share the docker host's IP via an environment variable ?

I think this could work:

  • Add a property e.g. server.host wit value ${docker.host} in the pom.xml's <properties> section.
  • When you test locally, this should work out of the box
  • When you test on Jenkins, use for the maven command the option mvn -Dserver.host=${SERVER_HOST} where ${SERVER_HOST} is a environment variable pointing to your Docker server and which is set when starting the Jenkins container.

The only thing which could possibly could go wrong here is that ${docker.host} is initialized to late by Maven so that it's empty when used in <properties>

@lappodeveloper
Copy link
Author

Yes I know, my currently Jenkins installation it's a little bit exotic, unfortunately I can't change it... :-(
With a normal installation (not as "docker" container, directly on the host) it would be no problem.
I'll try the solution sharing the host ip.
Thank you !

jgangemi referenced this issue in jgangemi/docker-maven-plugin Aug 10, 2015
- update dynamic ports in the run service
- cleaned up compiler warnings
- allow 'HostIp' to be bound as maven property and exported
- introduced 'PortMapping.Writer' to handle property file writing

Signed-off-by: Jae Gangemi <jgangemi@gmail.com>
@CyborTronik
Copy link

Any updates on that?

@jgangemi
Copy link
Collaborator

jgangemi commented Nov 3, 2015

actually...

it looks like the day job need some form of this functionality b/c we are running docker in docker and starting a mysql container w/ in the docker container and have no way to route to it.

@rhuss - can't we just have the user specify a distinct property name they want to read the ip into on a per container basis? we could add a exposedContainerInfo element (or whatever else we want to call it) and then underneath define various other elements that you would supply a property name to and have it populated w/ that result, eg:

<exposedContainerInfo>
  <ipAddress>container1.ip.address</ipAddress>
  ...
</exposedContainerInfo>

that would give an easy way to expose additional container details back to the build in the future.

@llofberg
Copy link

+1

I have HBase and Zookeeper running in docker and integration test running outside docker.
To connect to HBase I use Zookeeper which returns the HBase address (docker internal name e.g. hbase1).
While I can get the random ports for Zookeeper and HBase it seems I'll need Consul/Registrator to resolve HBase docker internal address (e.g. 172.17.0.4).

I could get the random port for consul DNS but it looks like defining sun.net.spi.nameservice.nameservers can only contain an ip address, not a custom port (standard DNS port is 53 which is typically not free).

Things work as expected if I give (hard code)

-Dsun.net.spi.nameservice.nameservers=172.17.0.2

on maven-failsafe-plugin argLine. I just can't find a way to get that docker internal ip address from anywhere.

  • lenny

@rhuss
Copy link
Collaborator

rhuss commented Dec 15, 2015

I see that there are several use cases for exposing container information into Maven properties.

My suggestion is to use a <exposeContainerInfo>prefix</exposeContainerInfo> which exposes a predefined set of container information (like the internal IP address). The default could be the <alias> or image name.

Since we already have the inspect REST call implemented this shouldn't be a big deal. I would start with the internal IP address and extend on this according to requirements coming in. (So I wouldn't expose all information available right now)

@jfgosselin
Copy link

+1

We are also running docker in docker (Bamboo) and retrieving the container ip is a must for the integration tests.

Another option might be to start the container (integration-test) with the option --net=container:$HOSTNAME where $HOSTNAME is the hostname of our Bamboo agent and this way we could access the container using localhost . But the plugin doesn't seems to expose the --net option.

@vitobasso
Copy link

The changelog says "Allow the container's host ip to be bound to a maven property and exported" on version 0.13.3, but I couldn't figure out how to do it, couldn't find it in the docs or by searching the repo.

@jgangemi
Copy link
Collaborator

see the port mapping section here: http://ro14nd.de/docker-maven-plugin/docker-start.html

the +host.ip:host.port:80 syntax will achieve this. please note that this binds the the ip of the container's host machine, not the internal ip of the container itself.

@rhauch
Copy link

rhauch commented Apr 25, 2016

I have a very similar use case to @lofbele1, except I'm running three Docker containers with MongoDB to form a replica set. I'm starting a fourth container that links to the first three and connects to the first MongoDB instance (the primary) to initiate the replica set and add the addresses of the other 2 MongoDB containers, and this container then exits. I can get everything set up, but when my integration tests, my tests need the address of each MongoDB container in order to connect; it'd be awesome if my build could set system properties using generated Maven properties.

I really just need the IP addresses of each container, and the Maven property could be named based upon my assigned alias for each container. For example, given containers with names mongo1, mongo2, and mongo3, perhaps the Maven properties would be something similar to container.ip.mongo1, container.ip.mongo2, and container.ip.mongo3. (The ports and everything else are easily accessed or set via other Maven properties in the POM file. It's just the container's IP addresses that are needed in my use case.)

@rhauch
Copy link

rhauch commented Apr 26, 2016

@rhuss, your proposal looks great, and is exactly what I need to get around my current issue with trying to start up a MongoDB replica set for use by integration tests.

@rhuss rhuss closed this as completed in f3ef5bd Apr 27, 2016
@rhauch
Copy link

rhauch commented Apr 27, 2016

@rhuss, I'm using this the latest commit in my build, and I'm trying to use the docker.container.<alias>.ip property within an image definition to set environment variables that point to the IP address of containers defined with other image definitions (which do have aliases):

 <env>
     <MONGODB_NODE1_HOST>${docker.container.mongo1.ip}</MONGODB_NODE1_HOST>
    ...
</env>

where mongo1 is the alias for one of my containers. When I build, I get this error:

[ERROR] Failed to execute goal io.fabric8:docker-maven-plugin:0.15.0:start (start) on project debezium-connector-mongodb: Execution start of goal io.fabric8:docker-maven-plugin:0.15.0:start failed: Failed to create contained configuration for [debezium/mongodb-repl-init]: Env variable 'MONGODB_NODE1_HOST' must not be null or empty -> [Help 1]

Here's the stack trace:

[ERROR] Failed to execute goal io.fabric8:docker-maven-plugin:0.15.0:start (start) on project debezium-connector-mongodb: Execution start of goal io.fabric8:docker-maven-plugin:0.15.0:start failed: Failed to create contained configuration for [debezium/mongodb-repl-init]: Env variable 'MONGODB_NODE1_HOST' must not be null or empty -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal io.fabric8:docker-maven-plugin:0.15.0:start (start) on project debezium-connector-mongodb: Execution start of goal io.fabric8:docker-maven-plugin:0.15.0:start failed: Failed to create contained configuration for [debezium/mongodb-repl-init]
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:224)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution start of goal io.fabric8:docker-maven-plugin:0.15.0:start failed: Failed to create contained configuration for [debezium/mongodb-repl-init]
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:145)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    ... 20 more
Caused by: java.lang.IllegalArgumentException: Failed to create contained configuration for [debezium/mongodb-repl-init]
    at io.fabric8.maven.docker.service.RunService.createContainerConfig(RunService.java:246)
    at io.fabric8.maven.docker.service.RunService.createAndStartContainer(RunService.java:96)
    at io.fabric8.maven.docker.StartMojo.executeInternal(StartMojo.java:104)
    at io.fabric8.maven.docker.AbstractDockerMojo.execute(AbstractDockerMojo.java:164)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    ... 21 more
Caused by: java.lang.IllegalArgumentException: Env variable 'MONGODB_NODE1_HOST' must not be null or empty
    at io.fabric8.maven.docker.access.ContainerCreateConfig.environment(ContainerCreateConfig.java:58)
    at io.fabric8.maven.docker.service.RunService.createContainerConfig(RunService.java:236)
    ... 25 more
[DEBUG] Connection manager is shutting down

Are the containers not being created and started in the same order they are defined in the file? I even tried adding links to the first 3 containers to try to force the fourth container to be created and started after the others.

Any clue why the container IP address properties are not being resolved in the image configuration creation?

@rhuss
Copy link
Collaborator

rhuss commented Apr 27, 2016

I'm in the road at the moment, but containers are created in parallel when
no is used. More on this later.

Randall Hauch notifications@github.com schrieb am Mi., 27. Apr. 2016
17:07:

@rhuss https://github.com/rhuss, I'm using this the latest commit
debezium/debezium@b799006#diff-66d581613c8c9a1bbbf20e76a52f3ac2R226
in my build debezium/debezium#35, and I'm
trying to use the docker.container..ip property within an image
definition
to set environment variables that point to the IP address of
containers defined with other image definitions (which do have aliases):

${docker.container.mongo1.ip} ...

where mongo1 is the alias for one of my containers. When I build, I get
this error:

[ERROR] Failed to execute goal io.fabric8:docker-maven-plugin:0.15.0:start (start) on project debezium-connector-mongodb: Execution start of goal io.fabric8:docker-maven-plugin:0.15.0:start failed: Failed to create contained configuration for [debezium/mongodb-repl-init]: Env variable 'MONGODB_NODE1_HOST' must not be null or empty -> [Help 1]

Here's the stack trace:

[ERROR] Failed to execute goal io.fabric8:docker-maven-plugin:0.15.0:start (start) on project debezium-connector-mongodb: Execution start of goal io.fabric8:docker-maven-plugin:0.15.0:start failed: Failed to create contained configuration for [debezium/mongodb-repl-init]: Env variable 'MONGODB_NODE1_HOST' must not be null or empty -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal io.fabric8:docker-maven-plugin:0.15.0:start (start) on project debezium-connector-mongodb: Execution start of goal io.fabric8:docker-maven-plugin:0.15.0:start failed: Failed to create contained configuration for [debezium/mongodb-repl-init]
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:224)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution start of goal io.fabric8:docker-maven-plugin:0.15.0:start failed: Failed to create contained configuration for [debezium/mongodb-repl-init]
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:145)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
... 20 more
Caused by: java.lang.IllegalArgumentException: Failed to create contained configuration for [debezium/mongodb-repl-init]
at io.fabric8.maven.docker.service.RunService.createContainerConfig(RunService.java:246)
at io.fabric8.maven.docker.service.RunService.createAndStartContainer(RunService.java:96)
at io.fabric8.maven.docker.StartMojo.executeInternal(StartMojo.java:104)
at io.fabric8.maven.docker.AbstractDockerMojo.execute(AbstractDockerMojo.java:164)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
... 21 more
Caused by: java.lang.IllegalArgumentException: Env variable 'MONGODB_NODE1_HOST' must not be null or empty
at io.fabric8.maven.docker.access.ContainerCreateConfig.environment(ContainerCreateConfig.java:58)
at io.fabric8.maven.docker.service.RunService.createContainerConfig(RunService.java:236)
... 25 more
[DEBUG] Connection manager is shutting down

Are the containers not being created and started in the same order they
are defined in the file? I even tried adding links to the first 3
containers to try to force the fourth container to be created and started
after the others.

Any clue why the container IP address properties are not being resolved in
the image configuration creation?


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#198 (comment)

@andredasilvapinto
Copy link

This also doesn't seem to work for me. I will try to come up with a simple example in order to debug it.

@andredasilvapinto
Copy link

andredasilvapinto commented Nov 21, 2016

https://gist.github.com/andredasilvapinto/e09d52a0940f2ecf29c7dbc9db401dd4

$ mvn clean verify
...
[INFO] DOCKER> [postgres:9.4.10] "db": Start container addfe544e7f1
[INFO] DOCKER> [postgres:9.4.10] "db": Waited on log out 'database system is ready to accept connections' 2521 ms
[INFO] DOCKER> [ubuntu]: Start container f3d113a122d6
server> 
...

@andredasilvapinto
Copy link

it works well when using it in another plugin like how it's done here:

https://github.com/fabric8io/docker-maven-plugin/blob/master/samples/data-jolokia-demo/pom.xml

the problem is with using it inside other containers in the same plugin/execution

@andredasilvapinto
Copy link

andredasilvapinto commented Nov 22, 2016

OK, so after a few hours of debugging, things need to happen:

  • injection of properties depends on the containers having a precedence relationship. you can enforce the precedence by using dependsOn, but it only works if you are using a custom network. alternatively you can use links.
  • property names need to be escaped, otherwise the first tentative to resolve the property will remove it's value, i.e. use $${docker.container.db.ip}, not ${docker.container.db.ip}

leusonmario pushed a commit to leusonmario/docker-maven-plugin that referenced this issue Aug 18, 2018
Support for docker command line option --build-arg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants