testcontainers-java: getContainerIpAddress always returns localhost

I’m trying to use testcontainers (v1.4.2) for integration testing between a Spring Boot service packed into a GenericContainer and a MysqlContainer.

Unfortunately, getContainerIpAddress will always return “localhost”.

Hence, ((MySQLContainer) mysql).getJdbcUrl() will just return garbage: jdbc:mysql://localhost:32821/test

My system: Archlinux x64, Docker version 17.07.0-ce But same error on colleague’s Mac.

The following part of GenericContainer.java doesn’t make sense to me

    @Override
    public String getContainerIpAddress() {
        return DockerClientFactory.instance().dockerHostIpAddress();
    }

-> isn’t that just returning the mere ip address of the host running docker service?

What I expected here to see is a call returning the IP address of the virtual network interface related to the desired container… please correct me if I’m wrong…

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 12
  • Comments: 33 (17 by maintainers)

Commits related to this issue

Most upvoted comments

What about adding a API method returning the real host IP address? I think it’s needed in some test configurations.

The point of this issue is that localhost is NOT and IP address. It is a hostname.

I do have to agree with @fred777. I was confused as well when I saw getting localhost when querying a method that was supposed to return an IP address. If returning localhost is expected behavior, then might be best to change then name of that method. Btw, great work with TestContainers!!! 😉

@burmecia apologies - I don’t have context on this issue. Do you mind creating an issue in https://github.com/airbytehq/airbyte to address the need? Would be happy to discuss there

In Groovy it would be possible to access the container’s IP address like this:

genericContainer.containerInfo.networkSettings.networks.entrySet().first().value.ipAddress

So it’s possible in Java as well, using a bit more verbose code, since the information is available in the object.

@fred777 The general idea would be use the mapped port of the MySQLContainer, which you can already see in the JdbcUrl using the port 32821.

@rnorth I wonder what’s the reason in not exposing the container’s ip in the public API?

Not good that this has been closed. I’d rather have container.getContainerAddress() return the actual address on the docker network and have a getMappedAddress() (as a counterpart to getMappedPort()) for the forwarded network mappings.

Thank you for your suggestions @bsideup, I also think that’s an ideal solution. If Airbyte team @sherifnada could implement that will be great. The current situation is that we have no control of the underlying container’s network setup, that’s why I have to work out some dirty hacks to get ip address.

The above code returns gateway ip address, not the container’s ip address. To get container’s ip address:

String ipAddress = container.getContainerInfo().getNetworkSettings().getIpAddress();

Make sure your container network is using Docker’s default bridge network.

A recommended read which might be helpful to others googling this issue when trying to connect to their containers: https://www.testcontainers.org/features/networking/

@tristantarrant what would be you use case for this?

If you need this value on your host, you may have a problem because Testcontainers supports running the tests with various Docker setups, including the remote ones (even in some Cloud)

If you need it for a communication between the containers, there is a support for networks and network aliases that should be used instead.

The issue was kept closed because it was mostly about the confusion about “getIp returns host” as stated here: https://github.com/testcontainers/testcontainers-java/issues/452#issuecomment-481223442

Also, it got automatically closed due to inactivity 😅

@fred777 getContainerIpAddress returns an IP address you can use to access your container from your tests, not from other containers. For inter-containers communication I recommend you to use Networks (since TestContainers version 1.4+ )