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
- Put league service testcontainers in same network and use alias According to https://github.com/testcontainers/testcontainers-java/issues/452 the .getContainerIpAddress() method returns an ip to acce... — committed to FAForever/faf-java-api by Brutus5000 5 years ago
- Put league service testcontainers in same network and use alias According to https://github.com/testcontainers/testcontainers-java/issues/452 the .getContainerIpAddress() method returns an ip to acce... — committed to FAForever/faf-java-api by Brutus5000 5 years ago
- Put league service testcontainers in same network and use alias According to https://github.com/testcontainers/testcontainers-java/issues/452 the .getContainerIpAddress() method returns an ip to acce... — committed to FAForever/faf-java-api by Brutus5000 5 years ago
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
localhostis NOT and IP address. It is a hostname.I do have to agree with @fred777. I was confused as well when I saw getting
localhostwhen querying a method that was supposed to return an IP address. If returninglocalhostis 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:
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:
container.getContainerInfo().getNetworkSettings().getIpAddress()container.getContainerInfo().getNetworkSettings().getNetworks()to find oneTo 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:
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 defaultbridgenetwork option and expose port 8123.Container for the connector we developed using JDBC interfaces
It uses
--network hostoption so it can access the 1st container’s db through JDBC urljdbc:clickhouse://localhost:8123/defaultThis 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 hostis not supported on Mac which means host namelocalhostcannot be used. I tried usehost.docker.internalinstead oflocalhost, 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 defaultbridgenetwork. Butlocalhoststill doesn’t work because default bridge network can only use ip address. Then I have to use ip address in JDBC url likejdbc:clickhouse://172.17.0.2:8123/defaultto 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:
Make sure your container network is using Docker’s default
bridgenetwork.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
getContainerIpAddressreturns 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+ )