testcontainers/testcontainers-java · error · IllegalStateException
Unexpected pattern for SSH_CONNECTION for docker machine -…
Error message
Unexpected pattern for SSH_CONNECTION for docker machine - expected 'IP PORT IP PORT' pattern but found '${sshConnectionString}' What it means
Thrown when the SSH_CONNECTION string obtained from `docker-machine ssh <machine> echo $SSH_CONNECTION` does not split into exactly 4 whitespace-separated parts ('IP PORT IP PORT' — client IP/port then server IP/port). TestContainers expects the standard OpenSSH format and uses the first field as the VM IP. Any other shape means the output was unexpected (extra text, warnings, localized messages) and the IP cannot be trusted.
Solutions
- Run `docker-machine ssh <machine> echo $SSH_CONNECTION` manually and inspect the raw output; remove any banners/MOTD that add extra tokens.
- Recreate the docker-machine VM with a default setup so SSH_CONNECTION is the standard 'IP PORT IP PORT' format.
- Avoid the docker-machine path entirely by using Docker for Desktop or setting DOCKER_HOST so the host IP is resolved natively.
- Upgrade TestContainers to a version that parses the docker host IP more robustly.
Defensive patterns
Strategy: validation
Validate before calling
String machine = System.getenv("DOCKER_MACHINE_NAME");
String out = CommandLine.runShellCommand("docker-machine", "ssh", machine, "echo $SSH_CONNECTION").trim();
if (out.split("\\s+").length != 4) throw new SkipException("Unexpected SSH_CONNECTION format: " + out); Try / catch
try {
String ip = container.getTestHostIpAddress();
} catch (IllegalStateException e) {
if (e.getMessage().startsWith("Unexpected pattern for SSH_CONNECTION")) {
log.warn("Malformed docker-machine SSH output; falling back", e);
} else throw e;
} Prevention
- Disable SSH banners/MOTD in the docker-machine VM so echo output stays clean.
- Use stock docker-toolbox images; avoid custom SSH wrappers.
- Verify output format once in CI provisioning and fail fast there.
- Migrate off docker-machine to avoid the SSH parsing path.
When it happens
Trigger: getTestHostIpAddress() is called in docker-machine mode and the SSH_CONNECTION value parsed via split("\\s") has a length != 4 — e.g. docker-machine ssh prints login banners, MOTD text, or a truncated/missing field.
Common situations: docker-machine VMs with custom SSH banners or MOTD polluting echo output; custom SSH wrappers or non-standard shells inside the VM; older/patched docker-toolbox images where SSH_CONNECTION is not set to the standard 4-token format; Docker-machine's `ssh` command configured to a different ssh binary that appends warnings.
Related errors
- Could not obtain SSH_CONNECTION environment variable for…
- You should never close the global DockerClient!
- Check failed:
- Requested port ( ) is not mapped
- execInContainer can only be used while the Container is…
AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12).
Data as JSON: /api/errors/9ae61fb8c55f0631.
Report an issue: GitHub.
Appendix: source
Thrown at core/src/main/java/org/testcontainers/containers/GenericContainer.java:1337
try {
Optional<String> defaultMachine = DockerMachineClient.instance().getDefaultMachine();
if (!defaultMachine.isPresent()) {
throw new IllegalStateException("Could not find a default docker-machine instance");
}
String sshConnectionString = CommandLine
.runShellCommand("docker-machine", "ssh", defaultMachine.get(), "echo $SSH_CONNECTION")
.trim();
if (Strings.isNullOrEmpty(sshConnectionString)) {
throw new IllegalStateException(
"Could not obtain SSH_CONNECTION environment variable for docker machine " +
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"
);
}
}
/**View on GitHub (pinned to 8e549514e3)