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.