{"record":{"id":"6583fa78be1427da","repo":"testcontainers/testcontainers-java","slug":"could-not-obtain-ssh-connection-environment-variable-for","errorCode":null,"errorMessage":"Could not obtain SSH_CONNECTION environment variable for docker machine ${defaultMachine}","messagePattern":"Could not obtain SSH_CONNECTION environment variable for docker machine (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/testcontainers/containers/GenericContainer.java","lineNumber":1329,"sourceCode":"\n    /**\n     * {@inheritDoc}\n     */\n    @Override\n    @Deprecated\n    public String getTestHostIpAddress() {\n        if (DockerMachineClient.instance().isInstalled()) {\n            try {\n                Optional<String> defaultMachine = DockerMachineClient.instance().getDefaultMachine();\n                if (!defaultMachine.isPresent()) {\n                    throw new IllegalStateException(\"Could not find a default docker-machine instance\");\n                }\n\n                String sshConnectionString = CommandLine\n                    .runShellCommand(\"docker-machine\", \"ssh\", defaultMachine.get(), \"echo $SSH_CONNECTION\")\n                    .trim();\n                if (Strings.isNullOrEmpty(sshConnectionString)) {\n                    throw new IllegalStateException(\n                        \"Could not obtain SSH_CONNECTION environment variable for docker machine \" +\n                        defaultMachine.get()\n                    );\n                }\n\n                String[] sshConnectionParts = sshConnectionString.split(\"\\\\s\");\n                if (sshConnectionParts.length != 4) {\n                    throw new IllegalStateException(\n                        \"Unexpected pattern for SSH_CONNECTION for docker machine - expected 'IP PORT IP PORT' pattern but found '\" +\n                        sshConnectionString +\n                        \"'\"\n                    );\n                }\n\n                return sshConnectionParts[0];\n            } catch (Exception e) {\n                throw new RuntimeException(e);\n            }","sourceCodeStart":1311,"sourceCodeEnd":1347,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/core/src/main/java/org/testcontainers/containers/GenericContainer.java#L1311-L1347","documentation":"Thrown by GenericContainer.getTestHostIpAddress() when TestContainers is running against a docker-machine VM and the command `docker-machine ssh <machine> echo $SSH_CONNECTION` returns an empty string. The library needs the SSH connection details to derive the VM's IP address, and without SSH_CONNECTION it cannot determine the docker host the test container is reachable on. It wraps the failure in an IllegalStateException because it indicates an unexpectedly broken docker-machine environment.","triggerScenarios":"Calling getTestHostIpAddress() when TestContainers detects the environment variable DOCKER_MACHINE_NAME (a docker-machine default machine) but `docker-machine ssh <machine> echo $SSH_CONNECTION` returns empty output — e.g. docker-machine is broken, the machine is in a bad state, or the SSH session cannot source the SSH_CONNECTION variable.","commonSituations":"Developers on Mac/Windows with legacy docker-machine toolboxes (pre-Docker-for-Desktop) whose machine was stopped, deleted, or recreated; docker-machine installed but not provisioned correctly; running inside CI images where the docker-machine VM is not started; custom shells where $SSH_CONNECTION is not populated.","solutions":["Verify the docker-machine VM is running and healthy: `docker-machine ls` then `docker-machine start <machine>` and `docker-machine env <machine>`.","Run `docker-machine ssh <machine> echo $SSH_CONNECTION` manually; if empty, regenerate SSH keys or recreate the machine with `docker-machine rm <machine> && docker-machine create ...`.","Prefer Docker for Desktop / native Docker over docker-machine, or set DOCKER_HOST directly so TestContainers does not take the docker-machine code path.","Upgrade TestContainers — docker-machine support is legacy and newer versions detect the host IP without SSH_CONNECTION."],"exampleFix":"// before (broken docker-machine env)\ngenericContainer.getTestHostIpAddress();\n\n// after: pre-check the machine is reachable before running tests\nassumingThat(isDockerMachineReachable(), () -> {\n    String hostIp = genericContainer.getTestHostIpAddress();\n});","handlingStrategy":"validation","validationCode":"String machine = System.getenv(\"DOCKER_MACHINE_NAME\");\nif (machine != null) {\n    String out = CommandLine.runShellCommand(\"docker-machine\", \"ssh\", machine, \"echo $SSH_CONNECTION\").trim();\n    if (out.isEmpty()) throw new SkipException(\"docker-machine SSH_CONNECTION unavailable; skipping host-IP test\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    String ip = container.getTestHostIpAddress();\n} catch (IllegalStateException e) {\n    Assume.assumeFalse(\"docker-machine unavailable\", e.getMessage().contains(\"SSH_CONNECTION\"));\n}","preventionTips":["Keep the docker-machine VM running (`docker-machine status <machine>`) before the test suite.","Prefer Docker for Desktop/native Docker; avoid the docker-machine code path entirely.","Cache a successful `docker-machine ssh ... echo $SSH_CONNECTION` check in CI setup steps.","Guard tests with Assume/assumeTrue so a broken machine skips rather than fails."],"tags":["docker","docker-machine","ssh","environment"],"backgroundTag":"missing-env-var","analyzedSha":"8e549514e3f01c57d70546fbb8599d138f3903e5","analyzedAt":"2026-09-12T14:56:41.227Z","contentChangedAt":"2026-09-12T14:56:41.227Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}