Skip to content

Commit

Permalink
Make dockerHostIp detection less flaky, Fixes #479
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Jan 11, 2018
1 parent 95bbecc commit 0aedb31
Showing 1 changed file with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.testcontainers.dockerclient;

import com.github.dockerjava.core.DockerClientConfig;
import com.google.common.net.InetAddresses;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.testcontainers.DockerClientFactory;

import java.io.File;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Slf4j
public class DockerClientConfigUtils {
Expand All @@ -20,13 +23,34 @@ public class DockerClientConfigUtils {
.of(IN_A_CONTAINER)
.filter(it -> it)
.map(file -> DockerClientFactory.instance().runInsideDocker(
cmd -> cmd.withCmd("sh", "-c", "ip route|awk '/default/ { print $3 }'"),
cmd -> cmd.withCmd("ip", "route"),
(client, id) -> {
try {
return client.logContainerCmd(id)
.withStdOut(true)
.exec(new LogToStringContainerCallback())
.toString();
return Unreliables.retryUntilSuccess(3, TimeUnit.SECONDS, () -> {
String output = client.logContainerCmd(id)
.withStdOut(true)
.withSince(0)
.exec(new LogToStringContainerCallback())
.toString();

output = StringUtils.trimToEmpty(output);

for (String line : output.split("\n")) {
if (line.startsWith("default")) {
for (String part : line.split(" ")) {
part = StringUtils.trimToEmpty(part);
if (InetAddresses.isInetAddress(part)) {
return part;
}
}
}
}

String message = "'ip route' did not contain a default route. Output:\n" + output;
log.warn(message);

throw new IllegalStateException(message);
});
} catch (Exception e) {
log.warn("Can't parse the default gateway IP", e);
return null;
Expand Down

0 comments on commit 0aedb31

Please sign in to comment.