Skip to content

Commit a51806e

Browse files
committed
fix:test
1 parent f187a91 commit a51806e

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.github.dockerjava.transport.SSLConfig;
1212
import com.github.dockerjava.transport.UnixSocket;
1313
import com.github.dockerjava.zerodep.ZerodepDockerHttpClient;
14+
1415
import com.google.common.annotations.VisibleForTesting;
1516
import com.google.common.base.Throwables;
1617
import lombok.Getter;
@@ -27,6 +28,11 @@
2728
import org.testcontainers.UnstableAPI;
2829
import org.testcontainers.utility.TestcontainersConfiguration;
2930

31+
import java.nio.charset.StandardCharsets;
32+
import java.util.regex.Matcher;
33+
import java.util.regex.Pattern;
34+
35+
3036
import java.io.BufferedReader;
3137
import java.io.File;
3238
import java.io.IOException;
@@ -406,11 +412,9 @@ public static DockerClient getClientForConfig(TransportConfig transportConfig) {
406412

407413
DefaultDockerClientConfig.Builder configBuilder = DefaultDockerClientConfig.createDefaultConfigBuilder();
408414

409-
410-
String dockerVersion = getDockerEngineVersionFromCli();
411-
System.out.println("Detected Docker Engine Version: " + dockerVersion);
412-
413-
if ("0.0.0".equals(dockerVersion) ||isDockerVersionAtLeast(dockerVersion, 25)) {
415+
String apiVersion = getDockerApiVersionFromClient(dockerHttpClient);
416+
System.out.println("Detected Docker API Version via Client: " + apiVersion);
417+
if (isApiVersionAtLeast(apiVersion, 1, 44)) {
414418
configBuilder.withApiVersion(RemoteApiVersion.VERSION_1_44);
415419
} else {
416420
configBuilder.withApiVersion(RemoteApiVersion.VERSION_1_32);
@@ -481,34 +485,41 @@ static String resolveDockerHostIpAddress(DockerClient client, URI dockerHost, bo
481485
}
482486
}
483487

484-
485-
private static String getDockerEngineVersionFromCli() {
488+
private static String getDockerApiVersionFromClient(DockerHttpClient client) {
486489
try {
487-
ProcessBuilder pb = new ProcessBuilder("docker", "version", "--format", "{{.Server.Version}}");
488-
Process process = pb.start();
489-
490-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
491-
String version = reader.readLine();
492-
if (version != null) {
493-
return version.trim();
490+
DockerHttpClient.Request request = DockerHttpClient.Request.builder()
491+
.method(DockerHttpClient.Request.Method.GET)
492+
.path("/version")
493+
.build();
494+
495+
try (DockerHttpClient.Response response = client.execute(request)) {
496+
if (response.getStatusCode() == 200) {
497+
String body = IOUtils.toString(response.getBody(), java.nio.charset.StandardCharsets.UTF_8);
498+
499+
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("\"ApiVersion\"\\s*:\\s*\"([0-9.]+)\"");
500+
java.util.regex.Matcher matcher = pattern.matcher(body);
501+
if (matcher.find()) {
502+
return matcher.group(1);
503+
}
494504
}
495505
}
496-
} catch (IOException e) {
497-
System.out.println("Failed to get docker version via CLI");
506+
} catch (Exception e) {
507+
log.warn("Failed to check docker version via HTTP client", e);
498508
}
499-
return "0.0.0";
509+
return "1.32";
500510
}
501511

502-
private static boolean isDockerVersionAtLeast(String versionString, int targetMajorVersion) {
503-
if (versionString == null || versionString.isEmpty()) return false;
504-
512+
// 【新規追加】APIバージョンの数値比較用
513+
private static boolean isApiVersionAtLeast(String currentVersion, int targetMajor, int targetMinor) {
505514
try {
506-
String majorPart = versionString.split("\\.")[0];
507-
int majorVersion = Integer.parseInt(majorPart);
515+
String[] parts = currentVersion.split("\\.");
516+
int major = Integer.parseInt(parts[0]);
517+
int minor = Integer.parseInt(parts[1]);
508518

509-
return majorVersion >= targetMajorVersion;
510-
} catch (NumberFormatException e) {
511-
System.out.println("Failed to parse docker version string: {}"+ versionString);
519+
if (major > targetMajor) return true;
520+
if (major == targetMajor && minor >= targetMinor) return true;
521+
return false;
522+
} catch (Exception e) {
512523
return false;
513524
}
514525
}

0 commit comments

Comments
 (0)