diff --git a/core/pom.xml b/core/pom.xml
index 5a120c3c145..6a958088ced 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -54,7 +54,12 @@
3.0.2
test
-
+
+ com.github.stefanbirkner
+ system-rules
+ 1.16.0
+ test
+
diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java
index 50106476a63..2a5723a85ad 100644
--- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java
+++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java
@@ -514,6 +514,9 @@ public GenericContainer withClasspathResourceMapping(String resourcePath, String
/**
* Get the IP address that this container may be reached on (may not be the local machine).
*
+ * If the system property 'testcontainers.container.host' or environment variable 'TESTCONTAINERS_CONTAINER_HOST'
+ * are set, that value will be returned instead.
+ *
* @return an IP address
*/
public String getContainerIpAddress() {
diff --git a/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java b/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java
index b93070780a7..71e0ac79f29 100644
--- a/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java
+++ b/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java
@@ -4,6 +4,12 @@
public class DockerClientConfigUtils {
public static String getDockerHostIpAddress(DockerClientConfig config) {
+
+ String overrideContainerHost = System.getProperty("testcontainers.container.host", System.getenv("TESTCONTAINERS_CONTAINER_HOST"));
+ if (overrideContainerHost != null) {
+ return overrideContainerHost;
+ }
+
switch (config.getUri().getScheme()) {
case "http":
case "https":
diff --git a/core/src/test/java/org/testcontainers/dockerclient/DockerClientConfigUtilsTest.java b/core/src/test/java/org/testcontainers/dockerclient/DockerClientConfigUtilsTest.java
index 1cb406cece8..238382a67d9 100644
--- a/core/src/test/java/org/testcontainers/dockerclient/DockerClientConfigUtilsTest.java
+++ b/core/src/test/java/org/testcontainers/dockerclient/DockerClientConfigUtilsTest.java
@@ -1,13 +1,20 @@
package org.testcontainers.dockerclient;
+import com.github.dockerjava.core.DockerClientConfig;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.contrib.java.lang.system.EnvironmentVariables;
+
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
-import org.junit.Test;
+public class DockerClientConfigUtilsTest {
-import com.github.dockerjava.core.DockerClientConfig;
+ @Rule
+ public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
-public class DockerClientConfigUtilsTest {
+ public static final String CONTAINER_HOST_SYSPROP = "testcontainers.container.host";
+ public static final String CONTAINER_HOST_ENVVAR = "TESTCONTAINERS_CONTAINER_HOST";
@Test
public void getDockerHostIpAddressShouldReturnLocalhostWhenUnixSocket() {
@@ -43,4 +50,30 @@ public void getDockerHostIpAddressShouldReturnNullWhenUnsupportedUriScheme() {
String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration);
assertNull(actual);
}
+
+ @Test
+ public void getDockerHostIpAddressShouldReturnOverrideWhenSystemPropertyIsSet() {
+ DockerClientConfig configuration = DockerClientConfig.createDefaultConfigBuilder().withUri("unix:///var/run/docker.sock").build();
+ try {
+ System.setProperty(CONTAINER_HOST_SYSPROP, "12.23.34.45");
+ String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration);
+ assertEquals("12.23.34.45", actual);
+
+ } finally {
+ System.clearProperty(CONTAINER_HOST_SYSPROP);
+ }
+ }
+
+ @Test
+ public void getDockerHostIpAddressShouldReturnOverrideWhenEnvVarIsSet() {
+ DockerClientConfig configuration = DockerClientConfig.createDefaultConfigBuilder().withUri("unix:///var/run/docker.sock").build();
+ try {
+ environmentVariables.set(CONTAINER_HOST_ENVVAR, "12.23.34.45");
+ String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration);
+ assertEquals("12.23.34.45", actual);
+
+ } finally {
+ environmentVariables.set(CONTAINER_HOST_ENVVAR, null);
+ }
+ }
}