diff --git a/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java b/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java index bfeabc63f8a..d88dc76331f 100644 --- a/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java +++ b/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java @@ -43,8 +43,10 @@ public class DockerClientConfigUtils { .map(StringUtils::trimToEmpty) .filter(StringUtils::isNotBlank); - - + /** + * Use {@link DockerClientFactory#dockerHostIpAddress()} + */ + @Deprecated public static String getDockerHostIpAddress(DockerClientConfig config) { switch (config.getDockerHost().getScheme()) { case "http": diff --git a/core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java b/core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java index 7101b23b21a..aa607964ae5 100644 --- a/core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java +++ b/core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java @@ -1,8 +1,10 @@ package org.testcontainers.dockerclient; import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.model.Network; import com.github.dockerjava.core.DockerClientBuilder; import com.github.dockerjava.core.DockerClientConfig; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Throwables; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; @@ -17,6 +19,7 @@ import org.testcontainers.dockerclient.transport.okhttp.OkHttpDockerCmdExecFactory; import org.testcontainers.utility.TestcontainersConfiguration; +import java.net.URI; import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -33,6 +36,8 @@ public abstract class DockerClientProviderStrategy { protected DockerClient client; protected DockerClientConfig config; + private String dockerHostIpAddress; + private static final RateLimiter PING_RATE_LIMITER = RateLimiterBuilder.newBuilder() .withRate(2, TimeUnit.SECONDS) .withConstantThroughput() @@ -188,8 +193,38 @@ protected void ping(DockerClient client, int timeoutInSeconds) { } } - public String getDockerHostIpAddress() { - return DockerClientConfigUtils.getDockerHostIpAddress(this.config); + public synchronized String getDockerHostIpAddress() { + if (dockerHostIpAddress == null) { + dockerHostIpAddress = resolveDockerHostIpAddress(client, config.getDockerHost()); + } + return dockerHostIpAddress; + } + + @VisibleForTesting + static String resolveDockerHostIpAddress(DockerClient client, URI dockerHost) { + switch (dockerHost.getScheme()) { + case "http": + case "https": + case "tcp": + return dockerHost.getHost(); + case "unix": + case "npipe": + if (DockerClientConfigUtils.IN_A_CONTAINER) { + return client.inspectNetworkCmd() + .withNetworkId("bridge") + .exec() + .getIpam() + .getConfig() + .stream() + .filter(it -> it.getGateway() != null) + .findAny() + .map(Network.Ipam.Config::getGateway) + .orElse("localhost"); + } + return "localhost"; + default: + return null; + } } protected void checkOSType() { diff --git a/core/src/test/java/org/testcontainers/dockerclient/DockerClientConfigUtilsTest.java b/core/src/test/java/org/testcontainers/dockerclient/DockerClientConfigUtilsTest.java index 2e88982a7a8..353d6ea94e7 100644 --- a/core/src/test/java/org/testcontainers/dockerclient/DockerClientConfigUtilsTest.java +++ b/core/src/test/java/org/testcontainers/dockerclient/DockerClientConfigUtilsTest.java @@ -1,9 +1,10 @@ package org.testcontainers.dockerclient; -import com.github.dockerjava.core.DefaultDockerClientConfig; -import com.github.dockerjava.core.DockerClientConfig; -import org.junit.Ignore; +import com.github.dockerjava.api.DockerClient; import org.junit.Test; +import org.testcontainers.DockerClientFactory; + +import java.net.URI; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -11,44 +12,29 @@ public class DockerClientConfigUtilsTest { + DockerClient client = DockerClientFactory.lazyClient(); + @Test public void getDockerHostIpAddressShouldReturnLocalhostWhenUnixSocket() { - DockerClientConfig configuration = DefaultDockerClientConfig.createDefaultConfigBuilder() - .withDockerHost("unix:///var/run/docker.sock") - .withDockerTlsVerify(false) // TODO - check wrt. https://github.com/docker-java/docker-java/issues/588 - .build(); - String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration); + String actual = DockerClientProviderStrategy.resolveDockerHostIpAddress(client, URI.create("unix:///var/run/docker.sock")); assertEquals("localhost", actual); } - @Test @Ignore - public void getDockerHostIpAddressShouldReturnDockerHostIpWhenHttpUri() { - DockerClientConfig configuration = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost("http://12.23.34.45").build(); - String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration); - assertEquals("12.23.34.45", actual); - } - - @Test @Ignore + @Test public void getDockerHostIpAddressShouldReturnDockerHostIpWhenHttpsUri() { - DockerClientConfig configuration = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost("https://12.23.34.45").build(); - String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration); + String actual = DockerClientProviderStrategy.resolveDockerHostIpAddress(client, URI.create("http://12.23.34.45")); assertEquals("12.23.34.45", actual); } @Test public void getDockerHostIpAddressShouldReturnDockerHostIpWhenTcpUri() { - DockerClientConfig configuration = DefaultDockerClientConfig.createDefaultConfigBuilder() - .withDockerHost("tcp://12.23.34.45") - .withDockerTlsVerify(false) // TODO - check wrt. https://github.com/docker-java/docker-java/issues/588 - .build(); - String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration); + String actual = DockerClientProviderStrategy.resolveDockerHostIpAddress(client, URI.create("tcp://12.23.34.45")); assertEquals("12.23.34.45", actual); } - @Test @Ignore + @Test public void getDockerHostIpAddressShouldReturnNullWhenUnsupportedUriScheme() { - DockerClientConfig configuration = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost("gopher://12.23.34.45").build(); - String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration); + String actual = DockerClientProviderStrategy.resolveDockerHostIpAddress(client, URI.create("gopher://12.23.34.45")); assertNull(actual); }