testcontainers / testcontainers/testcontainers-python
Bug: KafkaContainer.tc_start not starting when using a Network
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.3k
- Forks
- 386
- Avg merge
- 4h 40m
- Merged PRs (30d)
- 1
Description
If fails to start, since tc_start assumes that the container is using host networking
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)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at KafkaContainer.tc_start and compare its host-networking assumptions with the Network setup shown in the issue. Trace how _get_network_alias, get_bootstrap_server, and the container startup script interact. Done means KafkaContainer starts successfully on a Network and get_bootstrap_server returns a usable bootstrap server.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, kafka, python
- Domain
- distributed-systems, networking
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100