Docker, Kafka and .NET - Error to connect .NET application container in Kafka container

32 views Asked by At

I'm trying to make a connection between my .NET container (consumer) and Kafka container, but I'm getting an error when the .NET consumer tries to connect to Kafka. I've read a lot about listeners and I believe I have configured it correctly, but it still doesn't work. The most curious thing is that the same docker-compose file has a "Kafka-ui" container and this can connect to the broker correctly using the same address that I pass to my .NET application.

The message of error:

[thrd:kafka:19092/bootstrap]: kafka:19092/bootstrap: Connect to ipv4#192.168.32.4:19092 failed: Connection refused (after 1ms in state CONNECT)

My docker-compose.yml

version: "3.7"

services:
  zoo:
    container_name: zoo
    hostname: zoo
    image: zookeeper:3.4.9
    ports:
      - 2181:2181
    networks:
      - prod_context_net

  kafka:
    container_name: kafka
    hostname: kafka
    image: confluentinc/cp-kafka:5.3.0
    ports:
      - 9092:9092
    environment:
      KAFKA_LISTENERS: INTERNAL://0.0.0.0:19092,EXTERNAL://0.0.0.0:9092
      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:19092,EXTERNAL://localhost:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zoo:2181
      KAFKA_LOG4J_LOGGERS: kafka.controller=INFO,kafka.producer.async.DefaultEventHandler=INFO,state.change.logger=INFO
      KAFKA_DEFAULT_REPLICATION_FACTOR: 1
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    depends_on:
      - zoo
    networks:
      - prod_context_net

  kafka_ui:
    container_name: kafka_ui
    hostname: kafka_ui
    image: provectuslabs/kafka-ui
    depends_on:
      - kafka
      - zoo
    ports:
      - 8080:8080
    restart: always
    environment:
      KAFKA_CLUSTERS_0_NAME: kafka_cluster
      KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:19092
      KAFKA_CLUSTERS_0_ZOOKEEPER: zoo:2181
    networks:
      - prod_context_net

  application:
    container_name: application
    hostname: application
    image: prod_context/application
    build:
      context: ./
      dockerfile: ./application/Dockerfile
    environment:
      - KafkaEndpoint=kafka:19092
      - KafkaGroupId=GroupId
    ports:
      - 7772:8080
    depends_on:
      - kafka
      - zoo
    networks:
      - prod_context_net

networks:
  prod_context_net:
    name: prod_context_net
    driver: bridge

I ran the application from the host and accessing it via localhost:9092 was successful, but I want to be able to run my application through the container.

0

There are 0 answers