testcontainers/testcontainers-java · warning

Can't parse the default gateway IP

Error message

Can't parse the default gateway IP

What it means

DockerClientConfigUtils (in its docker-host gateway detection code) tries to determine the default gateway IP by exec'ing a command in a helper container and parsing the output. If that execution or parse throws, it logs 'Can't parse the default gateway IP' and returns null/empty, meaning Testcontainers cannot compute the docker host address used to reach exposed ports from the container.

Solutions

  1. Ensure the Docker daemon host has a normal default gateway and the container network can reach it.
  2. Set the docker host explicitly (DOCKER_HOST env var or withDockerHost) so Testcontainers doesn't have to guess the gateway.
  3. Upgrade Testcontainers — newer versions use better gateway detection (docker-java network inspect).
  4. If on legacy docker-machine/Toolbox, migrate to Docker Desktop or a supported Linux daemon.

Example fix

// before
DefaultDockerClientConfig.createDefaultConfigBuilder().build(); // relies on gateway detection on odd VM
// after
DefaultDockerClientConfig.createDefaultConfigBuilder()
    .withDockerHost("tcp://192.168.99.100:2376")
    .build();
Defensive patterns

Strategy: fallback

Validate before calling

String host = System.getenv("DOCKER_HOST");
if (host == null || host.isEmpty()) { /* set DOCKER_HOST so gateway detection is skipped */ }

Try / catch

try {
    container.start();
} catch (IllegalStateException e) {
    log.warn("Gateway IP detection failed; check DOCKER_HOST/network setup", e);
}

Prevention

When it happens

Trigger: Running the gateway-IP detection (Optional.rawInspect etc. in DockerClientConfigUtils, triggered when building a config for a remote/docker-machine style host) where the exec on the logging callback fails or throws any Exception.

Common situations: Docker environments without a resolvable default gateway (custom networks, rootless/VM setups, Docker Toolbox/legacy docker-machine), restricted environments where the helper container cannot run, or DNS/network misconfiguration on the daemon host.

Related errors


AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12). Data as JSON: /api/errors/66ad6ee304bc58ce. Report an issue: GitHub.

Appendix: source

Thrown at core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java:38

    private static final Optional<String> defaultGateway = Optional
        .ofNullable(
            DockerClientFactory
                .instance()
                .runInsideDocker(
                    cmd -> cmd.withCmd("sh", "-c", "ip route|awk '/default/ { print $3 }'"),
                    (client, id) -> {
                        try {
                            LogToStringContainerCallback loggingCallback = new LogToStringContainerCallback();
                            client
                                .logContainerCmd(id)
                                .withStdOut(true)
                                .withFollowStream(true)
                                .exec(loggingCallback)
                                .awaitStarted();
                            loggingCallback.awaitCompletion(3, TimeUnit.SECONDS);
                            return loggingCallback.toString();
                        } catch (Exception e) {
                            log.warn("Can't parse the default gateway IP", e);
                            return null;
                        }
                    }
                )
        )
        .map(StringUtils::trimToEmpty)
        .filter(StringUtils::isNotBlank);

    /**
     * @deprecated use {@link DockerClientProviderStrategy#getDockerHostIpAddress()}
     */
    @Deprecated
    public static String getDockerHostIpAddress(URI dockerHost) {
        switch (dockerHost.getScheme()) {
            case "http":
            case "https":
            case "tcp":
                return dockerHost.getHost();

View on GitHub (pinned to 8e549514e3)