testcontainers/testcontainers-java · error · UnsupportedOperationException

getTestHostIpAddress() is only implemented for…

Error message

getTestHostIpAddress() is only implemented for docker-machine right now

What it means

Thrown by GenericContainer.getTestHostIpAddress() as an UnsupportedOperationException when the container is NOT running against a docker-machine VM. The only implemented strategy for resolving the test host IP at this point in the code is via docker-machine's SSH_CONNECTION; all other environments fall through to this branch. It signals an unimplemented feature for the current Docker environment, not a broken configuration per se.

Solutions

  1. Do not call getTestHostIpAddress(); use container.getHost() (and getMappedPort) to obtain the address TestContainers already resolves for the environment.
  2. Upgrade TestContainers — modern versions implement host IP detection for Docker for Desktop and other environments.
  3. If you truly need the docker-machine flow, run against a docker-machine VM with DOCKER_MACHINE_NAME set.
  4. Compute the host IP manually from the docker bridge network (e.g. `ip route` / gateway IP) only as a last resort.

Example fix

// before
String host = container.getTestHostIpAddress(); // UnsupportedOperationException

// after
String host = container.getHost();
Integer port = container.getMappedPort(8080);
Defensive patterns

Strategy: validation

Validate before calling

// Only call getTestHostIpAddress() when actually on docker-machine
if (System.getenv("DOCKER_MACHINE_NAME") == null) {
    // use the portable API instead
    String host = container.getHost();
}

Type guard

boolean isDockerMachineEnv() {
    return System.getenv("DOCKER_MACHINE_NAME") != null;
}

Try / catch

try {
    String ip = container.getTestHostIpAddress();
} catch (UnsupportedOperationException e) {
    String host = container.getHost(); // portable fallback
}

Prevention

When it happens

Trigger: Calling getTestHostIpAddress() when no DOCKER_MACHINE_NAME/default machine is detected — e.g. plain Linux Docker, Docker for Desktop (Mac/Windows), or remote DOCKER_HOST environments where a different host-IP detection strategy (like TestContainers' normal internal per-container resolution) should be used instead.

Common situations: Developers calling getTestHostIpAddress() directly to connect test clients to a container on Docker for Desktop or Linux; copying docker-machine-era test code into a modern Docker setup; older TestContainers versions before richer host-IP detection was implemented.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at core/src/main/java/org/testcontainers/containers/GenericContainer.java:1349

                        defaultMachine.get()
                    );
                }

                String[] sshConnectionParts = sshConnectionString.split("\\s");
                if (sshConnectionParts.length != 4) {
                    throw new IllegalStateException(
                        "Unexpected pattern for SSH_CONNECTION for docker machine - expected 'IP PORT IP PORT' pattern but found '" +
                        sshConnectionString +
                        "'"
                    );
                }

                return sshConnectionParts[0];
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            throw new UnsupportedOperationException(
                "getTestHostIpAddress() is only implemented for docker-machine right now"
            );
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SELF withLogConsumer(Consumer<OutputFrame> consumer) {
        this.logConsumers.add(consumer);

        return self();
    }

    /**
     * {@inheritDoc}
     */

View on GitHub (pinned to 8e549514e3)