Testcontainers-java: getTestHostIpAddress not working

Created on 12 Feb 2019  Â·  9Comments  Â·  Source: testcontainers/testcontainers-java

Hi again,

unfortunatelly this code fails with an exception:

package testcontainers;

import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testcontainers.containers.BrowserWebDriverContainer;

public class TestContainerWebdriveHostIpAddress {

    @Rule
    public BrowserWebDriverContainer<?> firefox = new BrowserWebDriverContainer<>()
          .withCapabilities(new FirefoxOptions());

    @Test
    public void test() {
      String testHostIpAddress = firefox.getTestHostIpAddress();
    }

}

Exception:

java.lang.RuntimeException: org.testcontainers.utility.CommandLine$ShellCommandException: Exception when executing docker-machine ssh  echo $SSH_CONNECTION
    at org.testcontainers.containers.GenericContainer.getTestHostIpAddress(GenericContainer.java:1001)
    at testcontainers.TestContainerWebdriveHostIpAddress.test(TestContainerWebdriveHostIpAddress.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.testcontainers.containers.FailureDetectingExternalResource$1.evaluate(FailureDetectingExternalResource.java:30)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.testcontainers.utility.CommandLine$ShellCommandException: Exception when executing docker-machine ssh  echo $SSH_CONNECTION
    at org.testcontainers.utility.CommandLine.runShellCommand(CommandLine.java:47)
    at org.testcontainers.containers.GenericContainer.getTestHostIpAddress(GenericContainer.java:989)
    ... 26 more
Caused by: org.testcontainers.shaded.org.zeroturnaround.exec.InvalidExitValueException: Unexpected exit value: 1, allowed exit values: [0], executed command [docker-machine, ssh, "", echo $SSH_CONNECTION], output was 122 bytes:
Docker machine "" does not exist. Use "docker-machine ls" to list machines. Use "docker-machine create" to add a new one.
    at org.testcontainers.shaded.org.zeroturnaround.exec.InvalidExitUtil.checkExit(InvalidExitUtil.java:27)
    at org.testcontainers.shaded.org.zeroturnaround.exec.WaitForProcess.call(WaitForProcess.java:114)
    at org.testcontainers.shaded.org.zeroturnaround.exec.ProcessExecutor.waitFor(ProcessExecutor.java:1097)
    at org.testcontainers.shaded.org.zeroturnaround.exec.ProcessExecutor.execute(ProcessExecutor.java:925)
    at org.testcontainers.utility.CommandLine.runShellCommand(CommandLine.java:43)
    ... 27 more

I'am on Windows and use Hyper-V. I found a Post saying that docker-machine ls is not working with Hyper-V.
I'am guessing thats the reason why getTestHostIpAddress is not working.

Any thoughts on this (except using virtual box instead :-))

Thanky you

cliendocker-machine owindows stale typbug

All 9 comments

Oh dear - looks like another issue on Windows. I’m not sure quite how to fix that, but I can suggest an approach that should work better!

getTestHostIpAddress is generally a less portable thing anyway, as there are various restrictions in what containers can talk to. You could try instead using the approach set out in the ‘Exposing host ports to the container’ of https://www.testcontainers.org/features/networking/

Does that help?

Sent with GitHawk

@bopi- Why are you using docker-machine in the first place?

The recommended (and only supported) way of using Testcontainers on Windows is Windows 10 with Docker for Windows, which will use Docker without docker-machine.

The logged error could also indicate an old installation of Docker Toolbox, which will mess with Testcontainers' environment discovery.

Hello there. I met this issue today. So, to start BrowserWebDriverContainer I need to install and run Docker-machine. After install I have those messages in the issue. To fix this I done this:

aivinog1@W-NSK01-OF01399:~$ docker-machine env dev
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/home/aivinog1/.docker/machine/machines/dev"
export DOCKER_MACHINE_NAME="dev"
# Run this command to configure your shell: 
# eval $(docker-machine env dev)
aivinog1@W-NSK01-OF01399:~$ echo $DOCKER_MACHINE_NAME

aivinog1@W-NSK01-OF01399:~$ eval $(docker-machine env dev)
aivinog1@W-NSK01-OF01399:~$ echo $DOCKER_MACHINE_NAME
dev
aivinog1@W-NSK01-OF01399:~$

After that all work just fine. My OS is:

aivinog1@W-NSK01-OF01399:~$ cat /etc/issue
Ubuntu 18.04.2 LTS \n \l

aivinog1@W-NSK01-OF01399:~$ 

@aivinog1 Thanks for your remark, but this issue is for Windows, you are running Linux.
Testcontainers does not require docker-machine, but works with it if detected as the environment.

Wow. You guys are fast. Thank you for that.

@rnorth Your suggestion works like charme. Thank you.
What do you think about a little bit more of configuration possibilities?

  1. It would be great if it would be possible to change the hostname (host.testcontainers.internal) in the container.
  2. It also would be great if it would be possible to set the hostport in the container to a different one then the actual hostport. E.g. that the actual hostport is a random one but in the container its always 80.

@kiview
I'am not using docker-machine. Testcontainers is using docker-machine in getTestHostIpAddress:
https://github.com/testcontainers/testcontainers-java/blob/c2e2a0fa96ce8c1687120ffa45c347a9f5a46e27/core/src/main/java/org/testcontainers/containers/GenericContainer.java#L977-L1007

I found a Post saying that docker-machine ls is not working with Hyper-V.
I'am guessing thats the reason why getTestHostIpAddress is not working.

I did a first time installation of Docker for Windows on this machine. Afterwards also docker-machine was available. So I guess it comes with the Docker for Windows installation.

Docker for Windows on Windows 10 does not use docker-machine by default, why it does in your case is strange and maybe worth investigating.

If it's configured, Testcontainers will use it though.

You are probably right regarding missing docker-machine features on Windows 10.

What do you think about a little bit more of configuration possibilities?

1. It would be great if it would be possible to change the hostname (host.testcontainers.internal) in the container.

2. It also would be great if it would be possible to set the hostport in the container to a different one then the actual hostport. E.g. that the actual hostport is a random one but in the container its always 80

@rnorth : Any thoughts on this?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this is a mistake, please reply to this comment to keep it open. If there isn't one already, a PR to fix or at least reproduce the problem in a test case will always help us get back on track to tackle this.

This issue has been automatically closed due to inactivity. We apologise if this is still an active problem for you, and would ask you to re-open the issue if this is the case.

Was this page helpful?
0 / 5 - 0 ratings