0

I have a kafka consumer in VSCode IDE (MS Windows) that works properly. I take the same python script in docker container, but the kafka broker is not recognized. I put them inside the same network; so networking is OK. What could be the problem?


from kafka import KafkaConsumer
import time
while True:
    try:
        consumer = KafkaConsumer(
            'topic01',
            bootstrap_servers = ['localhost:9092'], #this works
            auto_offset_reset='latest',
            enable_auto_commit=True,
            auto_commit_interval_ms = 5000,
            fetch_max_bytes = 128,
            max_poll_records = 100,
            value_deserializer = lambda x: x.decode('utf-8')
        )
        break
    except Exception as e:
        time.sleep(2)
        print('exception happenned\n')
        print(f"an error occurred: {e}")

for message in consumer:
    print(message.value)

Networking issue checked. I expect that the python script consumes the messages as it consumes in windows environment. But inside docker container the kafka broker is not recognized.

FSH
  • 1
  • 1
    `localhost` in a container means the container itself. You need to put in the name assigned to the Kafka container on the Docker network instead. – Hans Kilian Apr 20 '23 at 13:59
  • Thanks, so if the name of the Kafka broker is kafka, I should make the bootstrap_servers as 'kafka:9092'? I tried this scenario, but it shows the same behavior. Look forward. – FSH Apr 20 '23 at 16:47

0 Answers0