Skip to content

Commit 9104105

Browse files
authored
[Bug]: Error starting container when using DIND in Kubernetes pod #11139
I updated container-environment detection so we correctly recognize Kubernetes/DIND pods and compute a reachable Docker host IP for unix-socket setups, avoiding “localhost” when inappropriate. This makes Ryuk and published container ports reachable from your test container in K8s+DIND without requiring manual TESTCONTAINERS_HOST_OVERRIDE. Key change: In core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java, IN_A_CONTAINER now detects containers using multiple heuristics: /.dockerenv, /run/.containerenv, KUBERNETES_SERVICE_HOST, and cgroup hints (“docker”, “kubepods”, “containerd”, “podman”). When true and DOCKER_HOST is a unix socket, Testcontainers will use the Docker bridge gateway instead of localhost, which fits DIND-in-pod networking.
1 parent 73726f4 commit 9104105

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,51 @@
66
import org.testcontainers.DockerClientFactory;
77

88
import java.io.File;
9+
import java.io.IOException;
910
import java.net.URI;
11+
import java.nio.charset.StandardCharsets;
12+
import java.nio.file.Files;
13+
import java.nio.file.Paths;
1014
import java.util.Optional;
1115
import java.util.concurrent.TimeUnit;
1216

1317
@Slf4j
1418
public class DockerClientConfigUtils {
1519

1620
// See https://github.com/docker/docker/blob/a9fa38b1edf30b23cae3eade0be48b3d4b1de14b/daemon/initlayer/setup_unix.go#L25
17-
public static final boolean IN_A_CONTAINER = new File("/.dockerenv").exists();
21+
public static final boolean IN_A_CONTAINER = detectInContainer();
22+
23+
private static boolean detectInContainer() {
24+
// Common indicators for being inside a containerized environment:
25+
// - Docker: /.dockerenv
26+
// - Podman / container tools: /run/.containerenv
27+
// - Kubernetes: presence of KUBERNETES_SERVICE_HOST
28+
// - cgroup hints: entries containing docker/kubepods/containerd/podman
29+
if (new File("/.dockerenv").exists()) {
30+
return true;
31+
}
32+
if (new File("/run/.containerenv").exists()) {
33+
return true;
34+
}
35+
if (System.getenv("KUBERNETES_SERVICE_HOST") != null) {
36+
return true;
37+
}
38+
try {
39+
byte[] content = Files.readAllBytes(Paths.get("/proc/1/cgroup"));
40+
String cgroup = new String(content, StandardCharsets.UTF_8).toLowerCase();
41+
if (
42+
cgroup.contains("docker") ||
43+
cgroup.contains("kubepods") ||
44+
cgroup.contains("containerd") ||
45+
cgroup.contains("podman")
46+
) {
47+
return true;
48+
}
49+
} catch (IOException ignored) {
50+
// ignore and fall back to false
51+
}
52+
return false;
53+
}
1854

1955
@Getter(lazy = true)
2056
private static final Optional<String> defaultGateway = Optional

0 commit comments

Comments
 (0)