Skip to content

Commit

Permalink
#198 : Expose container information as Docker properties.
Browse files Browse the repository at this point in the history
E.g. docker.container.<alias>.ip exposes the internal IP address of a docker container. Fixes #198
  • Loading branch information
rhuss committed Apr 26, 2016
1 parent 511d045 commit f3ef5bd
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
7 changes: 5 additions & 2 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
- Move `dockerFileDir` to topLevel `<build>` and introduced `dockerFile` directive
`build>assembly>dockerFileDir` is now deprecated and will be removed.
- Add new lifecycles "docker" (build + run), "docker-build" (build only) and
"docker-tar" (creating source)
"docker-tar" (creating source) (#433)
- Add `docker:run` as an alias to `docker:start`

- Expose certain container properties also as Maven properties. By default
the format is `docker.container.<alias>.ip` for the internal IP address of container with alias `<alias>`.
(#198)

* **0.14.2**
- Introduce a mode `try` for `<cleanup>` so that an image gets removed if not being still used.
This is the default now, which should be close enough to `true` (except that it won't fail the build
Expand Down
14 changes: 13 additions & 1 deletion doc/manual/docker-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ CTRL-C is pressed. That similar to the option `-i` for `docker run`. This will a
you can see what is happening within the container. Also, after stopping with CTRL-C, the container is stopped (but
not removed so that you can make postmortem analysis).

The `<run>` configuration knows the following sub elements:
By default container specific properties are exposed as Maven properties. These properties have the format
`docker.container.`*<alias>*`.`*<prop>* where *alias* is the name of the container (see below) and *<prop>* is one of
the following container properties:

* **ip** : The internal IP address of the container.

For example the Maven property `docker.container.tomcat.ip` would container the Docker internal IP for a container with
an alias "tomcat". You can set the global configuration **exposeContainerInfo** to an empty string to not expose container
information that way or to a string for an other prefix than `docker.container`.

#### <run> Configuration

The `<run>` configuration section knows the following sub elements:

* **capAdd** a list of `add` elements to specify kernel parameters to add to
the container.
Expand Down
5 changes: 3 additions & 2 deletions samples/data-jolokia-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@
</profile>

<profile>
<id>post-integration-test</id>
<id>properties</id>
<build>
<plugins>
<plugin>
Expand All @@ -489,7 +489,8 @@
</executions>
<configuration>
<echos>
<echo>============= Post Integration Test ========================</echo>
<echo>============= Props =============</echo>
<echo>IP : ${docker.container.server.ip}</echo>
</echos>
</configuration>
</plugin>
Expand Down
4 changes: 2 additions & 2 deletions samples/dockerignore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

<artifactId>dmp-sample-dockerignore</artifactId>
<version>0.15.0-SNAPSHOT</version>
<packaging>docker-tar</packaging>
<packaging>docker-build</packaging>

<build>
<plugins>
<plugin>
Expand Down
44 changes: 42 additions & 2 deletions src/main/java/io/fabric8/maven/docker/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
*/

import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;

import io.fabric8.maven.docker.access.DockerAccess;
Expand All @@ -32,6 +34,7 @@
import org.codehaus.plexus.util.StringUtils;



/**
* Goal for creating and starting a docker container. This goal evaluates the image configuration
*
Expand All @@ -46,9 +49,25 @@ public class StartMojo extends AbstractDockerMojo {
@Parameter(property = "docker.pull.registry")
private String pullRegistry;

// whether to block during to start. Set it via Sysem property docker.follow
// whether to block during to start. Set it via System property docker.follow
private boolean follow;

/**
* Expose container information like the internal IP as Maven properties which
* can be reused in the build information. The value of this property is the prefix
* used for the properties. The default prefix is "docker.container". Only information
* of images having an alias are exposed and have the format <code>&lt;prefix&gt;.&lt;alias&gt;.&lt;property&gt;</code>.
* (e.g. <code>docker.container.mycontainer.ip</code>
* The following properties are currently supported:
* <ul>
* <li><strong>ip</strong> : the container's internal IP address as chosen by Docker</li>
* </ul>
*
* If set to an empty string, no properties are exposed.
*/
@Parameter(property = "docker.exposeContainerInfo")
private String exposeContainerProps = "docker.container";

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -97,6 +116,9 @@ public synchronized void executeInternal(final ServiceHub hub) throws DockerAcce
if (waitConfig != null && waitConfig.getExec() != null && waitConfig.getExec().getPostStart() != null) {
runService.execInContainer(containerId, waitConfig.getExec().getPostStart(), imageConfig);
}

// Expose container info as properties
exposeContainerProps(hub.getQueryService(), containerId,imageConfig.getAlias());
}
if (follow) {
runService.addShutdownHookForStoppingContainers(keepContainer,removeVolumes);
Expand Down Expand Up @@ -271,4 +293,22 @@ protected boolean showLogs(ImageConfiguration imageConfig) {
return false;
}

private void exposeContainerProps(QueryService queryService, String containerId, String alias)
throws DockerAccessException {
if (StringUtils.isNotEmpty(exposeContainerProps) && StringUtils.isNotEmpty(alias)) {
Container container = queryService.getContainer(containerId);
Properties props = project.getProperties();
String ip = container.getIPAddress();
if (StringUtils.isNotEmpty(ip)) {
String key = addDot(exposeContainerProps) + addDot(alias) + "ip";
props.put(key, ip);
}
}
}

private String addDot(String part) {
return part.endsWith(".") ? part : part + ".";
}


}

0 comments on commit f3ef5bd

Please sign in to comment.