Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@
<version>3.0.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.16.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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);
}
}
}