diff --git a/core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java b/core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java index 7b0aaafc169..73ea830b8d9 100644 --- a/core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java +++ b/core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java @@ -391,11 +391,21 @@ public static DockerClient getClientForConfig(TransportConfig transportConfig) { String transportType = TestcontainersConfiguration.getInstance().getTransportType(); switch (transportType) { case "httpclient5": - dockerHttpClient = - new ZerodepDockerHttpClient.Builder() - .dockerHost(transportConfig.getDockerHost()) - .sslConfig(transportConfig.getSslConfig()) - .build(); + ZerodepDockerHttpClient.Builder builder = new ZerodepDockerHttpClient.Builder() + .dockerHost(transportConfig.getDockerHost()) + .sslConfig(transportConfig.getSslConfig()); + + Duration connectionTimeout = TestcontainersConfiguration.getInstance().getClientConnectionTimeout(); + if (connectionTimeout != null) { + builder.connectionTimeout(connectionTimeout); + } + + Duration responseTimeout = TestcontainersConfiguration.getInstance().getClientResponseTimeout(); + if (responseTimeout != null) { + builder.responseTimeout(responseTimeout); + } + + dockerHttpClient = builder.build(); break; default: throw new IllegalArgumentException("Unknown transport type '" + transportType + "'"); diff --git a/core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java b/core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java index 5c80b82ba9c..f53c25e326d 100644 --- a/core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java +++ b/core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java @@ -22,6 +22,7 @@ import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; +import java.time.Duration; import java.util.Map; import java.util.Optional; import java.util.Properties; @@ -224,6 +225,18 @@ public Integer getClientPingTimeout() { return Integer.parseInt(getEnvVarOrProperty("client.ping.timeout", "10")); } + @Nullable + public Duration getClientConnectionTimeout() { + String timeout = getEnvVarOrProperty("client.connection.timeout", null); + return timeout != null ? Duration.ofSeconds(Integer.parseInt(timeout)) : null; + } + + @Nullable + public Duration getClientResponseTimeout() { + String timeout = getEnvVarOrProperty("client.response.timeout", null); + return timeout != null ? Duration.ofSeconds(Integer.parseInt(timeout)) : null; + } + @Nullable @Contract("_, !null, _ -> !null") private String getConfigurable( diff --git a/core/src/test/java/org/testcontainers/utility/TestcontainersConfigurationTest.java b/core/src/test/java/org/testcontainers/utility/TestcontainersConfigurationTest.java index 4e0520af5ba..97ea472c8c6 100644 --- a/core/src/test/java/org/testcontainers/utility/TestcontainersConfigurationTest.java +++ b/core/src/test/java/org/testcontainers/utility/TestcontainersConfigurationTest.java @@ -225,6 +225,22 @@ void shouldTrimImageNames() { .isEqualTo("testcontainers/ryuk:0.3.2"); } + @Test + void shouldReadClientConnectionTimeout() { + assertThat(newConfig().getClientConnectionTimeout()).isNull(); + + userProperties.setProperty("client.connection.timeout", "45"); + assertThat(newConfig().getClientConnectionTimeout()).isEqualTo(java.time.Duration.ofSeconds(45)); + } + + @Test + void shouldReadClientResponseTimeout() { + assertThat(newConfig().getClientResponseTimeout()).isNull(); + + userProperties.setProperty("client.response.timeout", "120"); + assertThat(newConfig().getClientResponseTimeout()).isEqualTo(java.time.Duration.ofSeconds(120)); + } + private TestcontainersConfiguration newConfig() { return new TestcontainersConfiguration(userProperties, classpathProperties, environment); } diff --git a/docs/features/configuration.md b/docs/features/configuration.md index 389936cb89f..707ef2fcab0 100644 --- a/docs/features/configuration.md +++ b/docs/features/configuration.md @@ -101,6 +101,14 @@ but does not allow starting privileged containers, you can turn off the Ryuk con > **client.ping.timeout = 10** > Specifies for how long Testcontainers will try to connect to the Docker client to obtain valid info about the client before giving up and trying next strategy, if applicable (in seconds). +## Customizing client connection and response timeouts + +> **client.connection.timeout = (not set)** +> Specifies the maximum time allowed to establish a connection to the Docker daemon (in seconds). If not configured, the default connection timeout of the underlying transport client (3 minutes) is used. + +> **client.response.timeout = (not set)** +> Specifies the maximum time allowed to wait for a response from the Docker daemon after a connection is established (in seconds). If not configured, the default response timeout of the underlying transport client is used. + ## Customizing Docker host detection Testcontainers will attempt to detect the Docker environment and configure everything to work automatically.