Search code examples
pythondockerapache-kafkakafka-consumer-apikafka-producer-api

Python, Kafka and Docker - KafkaConsumer keeps hanging


I have the following docker-compose file:

  version: '3.1'

  services:
    postgres_db:
      image: postgres
      restart: always
      environment:
        POSTGRES_USER: admin
        POSTGRES_PASSWORD: admin
        POSTGRES_DB: default_db
      ports:
        - 54320:5432
    zookeeper:
      image: wurstmeister/zookeeper
      ports:
        - "2181:2181"
    kafka:
      image: wurstmeister/kafka
      ports:
        - "9092:9092"
      environment:
        KAFKA_ADVERTISED_HOST_NAME: kafka
        KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
        KAFKA_CREATE_TOPICS: "test:1:1"
      volumes:
        - /var/run/docker.sock:/var/run/docker.sock

After running it using docker-compose up - everything looks fine from the terminal output. I start a python console and run the following lines:

kp = KafkaProducer(bootstrap_servers=['localhost:9092'],api_version=(0,10),
                         value_serializer=lambda x: 
                         dumps(x).encode('utf-8'))
kc = KafkaConsumer('test', bootstrap_servers=['localhost:9092'],api_version=(0,10),group_id=None,auto_offset_reset='earliest', 
                   value_deserializer=lambda json_data: json.loads(json_data.decode('utf-8')))
data = {"test":"test"}
kp.send(topic="test",value=data)
for message in kc:
    print(message.value)

However after running this, the console simply hangs and it dosent look like the message was consumed/produced. Any ideas what went wrong here? Thanks!


Solution

  • Either you need to run your Python code in a container and set

    bootstrap_servers=['kafka:9092']
    

    Or you need to advertise Kafka back to the clients on your host machine

    KAFKA_ADVERTISED_HOST_NAME: localhost 
    

    You can read the wurstmeister README on the usage of HOSTNAME_COMMAND as well

    I'd also recommend running the producer and consumer separately as you test them