We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
If fails to start, since tc_start assumes that the container is using host networking
tc_start
from docker.types import EndpointConfig from testcontainers.core.container import DockerContainer from testcontainers.core.network import Network from testcontainers.kafka import KafkaContainer def _connect_to_network(ctr: DockerContainer, network: Network, alias: str): # Needed until https://github.com/testcontainers/testcontainers-python/issues/645 is fixed ctr.with_kwargs( network=network.name, networking_config={network.name: EndpointConfig("1.33", aliases=[alias])} ) with Network() as network: ctr = KafkaContainer(image=f"confluentinc/cp-kafka:{KAFKA_VERSION}") _connect_to_network(ctr, network, "kafka") with ctr: assert ctr.get_bootstrap_server(). # This should start
A possible fix:
def tc_start(self): # We replace these three lines, which assume host networking # host = self.get_container_host_ip() # port = self.get_exposed_port(self.port) # listeners = f"PLAINTEXT://{host}:{port},BROKER://$(hostname -i | cut -d' ' -f1):9092" listeners = f"PLAINTEXT://{self._get_network_alias()}:{self.port},BROKER://$(hostname -i | cut -d' ' -f1):9092" data = ( dedent( f""" #!/bin/bash {self.boot_command} export KAFKA_ADVERTISED_LISTENERS={listeners} . /etc/confluent/docker/bash-config /etc/confluent/docker/configure /etc/confluent/docker/launch """ ) .strip() .encode("utf-8") ) self.create_file(data, KafkaContainer.TC_START_SCRIPT)
The text was updated successfully, but these errors were encountered:
I expanded this solution so I can connect from the docker network as well as the host:
class CustomKafkaContainer(KafkaContainer): def __init__(self): super().__init__() self.security_protocol_map += ",EXTERNAL:PLAINTEXT" self.with_env( "KAFKA_LISTENER_SECURITY_PROTOCOL_MAP", self.security_protocol_map ) self.listeners = f"PLAINTEXT://0.0.0.0:29092,BROKER://0.0.0.0:9092,EXTERNAL://0.0.0.0:{self.port}" self.with_env("KAFKA_LISTENERS", self.listeners) def tc_start(self): listeners = ",".join( [ f"EXTERNAL://{self.get_bootstrap_server()}", f"PLAINTEXT://{self._get_network_alias()}:29092", "BROKER://$(hostname -i | cut -d' ' -f1):9092", ] ) data = ( dedent( f""" #!/bin/bash {self.boot_command} export KAFKA_ADVERTISED_LISTENERS={listeners} . /etc/confluent/docker/bash-config /etc/confluent/docker/configure /etc/confluent/docker/launch """ ) .strip() .encode("utf-8") ) self.create_file(data, KafkaContainer.TC_START_SCRIPT)
Sorry, something went wrong.
No branches or pull requests
If fails to start, since
tc_start
assumes that the container is using host networkingA possible fix:
The text was updated successfully, but these errors were encountered: