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 9 years ago
  • Reactions: 12
  • Comments: 34 (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.

Thank you all for your replies. I can understand if you think it is not beneficial to add this function. I just comment here for someone else who has same ip address requirement as me.

To get container’s ip address:

  1. use container.getContainerInfo().getNetworkSettings().getIpAddress()
  2. if above is not work, search through values of container.getContainerInfo().getNetworkSettings().getNetworks() to find one

To answer @kiview’s question, I will describe my scenario below.

I am working on a JDBC-based destination connector for https://github.com/airbytehq/airbyte. They built an automated integration test pipeline using Testcontainers. For each test case, it will create 2 containers:

  1. Container for the destination database such like ClickHouse, Postgres, MySQL etc.

    This one uses Testcontainers and provides JDBC url like jdbc:clickhouse://localhost:8123/default. It uses default bridge network option and expose port 8123.

  2. Container for the connector we developed using JDBC interfaces

    It uses --network host option so it can access the 1st container’s db through JDBC url jdbc:clickhouse://localhost:8123/default

This pipeline is already built for many connectors and it runs on both local laptop and CI/CD environment. Thus we, as a connector developer, cannot change its config. It works fine on Linux platform, but on Mac the 2nd container cannot access the 1st. The reason is because --network host is not supported on Mac which means host name localhost cannot be used. I tried use host.docker.internal instead of localhost, but the JDBC driver will take DNS lookup first for that which still fails.

So my solution is do a dirty hack to disable 2nd container use --network host, then both containers are now in same default bridge network. But localhost still doesn’t work because default bridge network can only use ip address. Then I have to use ip address in JDBC url like jdbc:clickhouse://172.17.0.2:8123/default to access 1st container.

Since this hack is so dirty I cannot publish it and only used for my local development and brings lots of inconvenience. So my final solution is totally run the integration test pipeline on Linux VM, thus all the problems solved and I don’t need ip address anymore.

I am writing here just in case if I need it in the future or for someone else has similar situation like me.

Thanks again.

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+ )