You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bad KAFKA_ADVERTISED_LISTENERS when exposing host ports to the container #3187
The error seen from the Kafka container logs is the following:
java.lang.IllegalArgumentException: requirement failed: Each listener must have a different port, listeners: PLAINTEXT://localhost:32889,BROKER://192.168.0.2:9092,BROKER://172.17.0.4:9092
We change the affected part to use different ports when multiple networks are present, but I'm not sure of the implications. Any thoughts on the subject?
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.
I've got the same problem.
Kafka fails to start with the same exception posted by @goncalonsp
I was able to create a small test that reproduces the issue.
import org.junit.jupiter.api.Test;
import org.testcontainers.Testcontainers;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.Network;
public class KafkaContainerBug {
@Test
public void reproduceBug() {
Testcontainers.exposeHostPorts(3454);
try (
Network bridgeNetwork = Network.builder().driver("bridge").build();
KafkaContainer kafka = new KafkaContainer().withNetwork(bridgeNetwork).withNetworkAliases("kafka")
) {
kafka.start();
}
}
}
without Testcontainers.exposeHostPorts(3454); it works. When this line is added 2nd network keyed "bridge" is added to containerInfo.getNetworkSettings().getNetworks() map.
I am also unable to use the KafkaContainer with a custom network while exposing some other port. My use-case is:
I need to expose the local port 3306 (MySQL) to toxiproxy testcontainer;
I use the same network both for toxiproxy container and for kafka container.
The error I get is:
ERROR Fatal error during SupportedServerStartable startup. Prepare to shutdown (io.confluent.support.metrics.SupportedKafka)
java.lang.IllegalArgumentException: requirement failed: Each listener must have a different port, listeners: PLAINTEXT://localhost:53510,BROKER://192.168.128.3:9092,BROKER://172.17.0.6:9092
If I omit this line from my setup, it works: Testcontainers.exposeHostPorts(3306).
There is a Kafka ticket that will make this bug go away when it is resolved. But I assume it doesn't have high enough priority, so unlikely it will be picked up soon.
@NersesAM thanks for sharing this link! We will try to reach out to the Kafka folks and also workaround it on our side 👍
Thanks Sergei,
I think workaround in testcontainers will be needed for some time anyway, because even if Kafka fixes that issue, not everyone will be able to use latest version from the get go.
This looks fixed since 1.16.0 by #4213. The old org.testcontainers.containers.KafkaContainer now uses only the network set with withNetwork (or bridge) for the BROKER listener. Tests testWithHostExposedPort and testWithHostExposedPortAndExternalNetwork in KafkaContainerTest cover this case.
The new org.testcontainers.kafka.KafkaContainer and ConfluentKafkaContainer use the container hostname for the BROKER listener, so they do not have this problem. I ran the test from this issue (exposeHostPorts + custom bridge network, so the container has 2 networks) on main with both classes, and Kafka starts and works.
Activity
stale commentedon Dec 19, 2020
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.
NersesAM commentedon Apr 19, 2021
I've got the same problem.
Kafka fails to start with the same exception posted by @goncalonsp
I was able to create a small test that reproduces the issue.
without
Testcontainers.exposeHostPorts(3454);it works. When this line is added 2nd network keyed "bridge" is added to containerInfo.getNetworkSettings().getNetworks() map.But in general https://github.com/testcontainers/testcontainers-java/blob/master/modules/kafka/src/main/java/org/testcontainers/containers/KafkaContainer.java#L128-L129
will never work for more than one networks.
Any suggestions how to fix it?
worldtiki commentedon Apr 20, 2021
I'm trying to understand the need for the bridge network.
If you want to access the Kafka container from a non containerized app you can:
And in your app:
If you want to access from other containers you can:
And then for the configs of the other containerized app, add it to the same network and use the alias like this:
NersesAM commentedon Apr 23, 2021
@worldtiki my use-case is I need to connect from inside docker container to my test app running on the host.
botismarius commentedon Apr 26, 2021
Hi,
I am also unable to use the KafkaContainer with a custom network while exposing some other port. My use-case is:
The error I get is:
If I omit this line from my setup, it works:
Testcontainers.exposeHostPorts(3306).Please advise.
botismarius commentedon Apr 26, 2021
As a workaround I am extending the
KafkaContainerclass and removing thebridgenetwork:NersesAM commentedon Apr 29, 2021
There is a Kafka ticket that will make this bug go away when it is resolved. But I assume it doesn't have high enough priority, so unlikely it will be picked up soon.
https://issues.apache.org/jira/browse/KAFKA-9257
bsideup commentedon Apr 29, 2021
@NersesAM thanks for sharing this link! We will try to reach out to the Kafka folks and also workaround it on our side 👍
NersesAM commentedon Apr 29, 2021
Thanks Sergei,
I think workaround in testcontainers will be needed for some time anyway, because even if Kafka fixes that issue, not everyone will be able to use latest version from the get go.
aymenjamaaoui commentedon Oct 4, 2026
This looks fixed since 1.16.0 by #4213. The old
org.testcontainers.containers.KafkaContainernow uses only the network set withwithNetwork(orbridge) for theBROKERlistener. TeststestWithHostExposedPortandtestWithHostExposedPortAndExternalNetworkinKafkaContainerTestcover this case.The new
org.testcontainers.kafka.KafkaContainerandConfluentKafkaContaineruse the container hostname for theBROKERlistener, so they do not have this problem. I ran the test from this issue (exposeHostPorts+ custom bridge network, so the container has 2 networks) onmainwith both classes, and Kafka starts and works.I think this issue can be closed.