Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

containerIpAddress returns the gateway #15

Closed
miketwo opened this issue Mar 14, 2018 · 3 comments
Closed

containerIpAddress returns the gateway #15

miketwo opened this issue Mar 14, 2018 · 3 comments

Comments

@miketwo
Copy link
Contributor

miketwo commented Mar 14, 2018

The call to container.containerIpAddress seems to return the gateway instead of the container's ip address.

Working example (this is modified from the docs):

class nginxtest extends FlatSpec
  with ForAllTestContainer
  with Logging {
  override val container = GenericContainer("nginx:latest",
    exposedPorts = Seq(80),
    waitStrategy = Wait.forHttp("/")
  )

  "GenericContainer" should "start nginx and expose 80 port" in {
    logger.error(s"Returned IP is ${container.containerIpAddress}")
    logger.error(s"Actual IP is ${container.containerInfo.getNetworkSettings.getIpAddress}")
    assert(Source.fromInputStream(
      new URL(s"http://${container.containerIpAddress}:${container.mappedPort(80)}/").openConnection().getInputStream
    ).mkString.contains("If you see this page, the nginx web server is successfully installed"))
    Thread.sleep(60000) // time for running docker commands on the CLI
  }
}

It should return something like:

18:33:53.208 [nginxtest] ERROR - Returned IP is 172.17.0.1
18:33:53.212 [nginxtest] ERROR - Actual IP is 172.17.0.5

To see the actual network settings, I ran docker network inspect <MAIN BRIDGE ID> while the test was running. The outputs looked like this:

> docker network inspect <MAIN BRIDGE ID>
...
"Containers": {
    "8e294d6e7588ae3e23bfbde871abed309ec12a82a47362721e5312e27f6e5d29": {
        "Name": "friendly_dubinsky",
        ...
        "IPv4Address": "172.17.0.5/16",    <----- note IP address that I want
        "IPv6Address": ""
    },
    "ca69c03c979b981da2f6b18aa275de074041b4a6f5e7b41ca2b703ac730db536": {
        "Name": "testcontainers-ryuk-a657e7c9-3cbc-4548-8745-4da652ef7df7",
        ...
        "IPv4Address": "172.17.0.4/16",
        "IPv6Address": ""
    },
    ...
},
...

The workaround seems to be using container.containerInfo.getNetworkSettings.getIpAddress, but this method is marked as deprecated, so I'm worried about relying on it. I think containerIpAddress should return the v6IP if available, and the v4IP if not.

It's worth noting that I ran this test from within another docker container with -v /var/run/docker.sock:/var/run/docker.sock mounted so that I can create sibling containers.

@miketwo miketwo changed the title containerIpAddress returns the host bridge containerIpAddress returns the gateway Mar 14, 2018
@dimafeng
Copy link
Collaborator

Hi @miketwo,
This logic is implemented in the parent project (https://github.com/testcontainers/testcontainers-java). containerIpAddress method is proxied to org.testcontainers.containers.GenericContainer#getContainerIpAddress I'll take a look at your problem when I have free time. Meanwhile, you can ask help in the parent project or slack - https://testcontainers.slack.com

@dimafeng
Copy link
Collaborator

@miketwo it seems (per testcontainers/testcontainers-java#654) this was fixed in testcontainers-java 1.7.1 so it will be available in testcontainers-scala 0.17.0

@miketwo
Copy link
Contributor Author

miketwo commented Apr 29, 2018

Hmmm... maybe. That fix looks like it's for getDockerHostIpAddress, which may not(?) be the same as getContainerIpAddress. To me, it looks like the relevant issue upstream is this one. I'll do some more testing when I get a chance and see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants