|
11 | 11 | import com.github.dockerjava.transport.SSLConfig; |
12 | 12 | import com.github.dockerjava.transport.UnixSocket; |
13 | 13 | import com.github.dockerjava.zerodep.ZerodepDockerHttpClient; |
| 14 | + |
14 | 15 | import com.google.common.annotations.VisibleForTesting; |
15 | 16 | import com.google.common.base.Throwables; |
16 | 17 | import lombok.Getter; |
|
27 | 28 | import org.testcontainers.UnstableAPI; |
28 | 29 | import org.testcontainers.utility.TestcontainersConfiguration; |
29 | 30 |
|
| 31 | + import java.nio.charset.StandardCharsets; |
| 32 | + import java.util.regex.Matcher; |
| 33 | + import java.util.regex.Pattern; |
| 34 | + |
| 35 | + |
30 | 36 | import java.io.BufferedReader; |
31 | 37 | import java.io.File; |
32 | 38 | import java.io.IOException; |
@@ -406,11 +412,9 @@ public static DockerClient getClientForConfig(TransportConfig transportConfig) { |
406 | 412 |
|
407 | 413 | DefaultDockerClientConfig.Builder configBuilder = DefaultDockerClientConfig.createDefaultConfigBuilder(); |
408 | 414 |
|
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)) { |
414 | 418 | configBuilder.withApiVersion(RemoteApiVersion.VERSION_1_44); |
415 | 419 | } else { |
416 | 420 | configBuilder.withApiVersion(RemoteApiVersion.VERSION_1_32); |
@@ -481,34 +485,41 @@ static String resolveDockerHostIpAddress(DockerClient client, URI dockerHost, bo |
481 | 485 | } |
482 | 486 | } |
483 | 487 |
|
484 | | - |
485 | | - private static String getDockerEngineVersionFromCli() { |
| 488 | + private static String getDockerApiVersionFromClient(DockerHttpClient client) { |
486 | 489 | 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 | + } |
494 | 504 | } |
495 | 505 | } |
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); |
498 | 508 | } |
499 | | - return "0.0.0"; |
| 509 | + return "1.32"; |
500 | 510 | } |
501 | 511 |
|
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) { |
505 | 514 | 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]); |
508 | 518 |
|
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) { |
512 | 523 | return false; |
513 | 524 | } |
514 | 525 | } |
|
0 commit comments