Ask Your Question
4

What is the method for defining and utilizing the host name for Kafka within a Docker Compose setup?

asked 2022-09-20 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-12-27 01:00:00 +0000

david gravatar image

To define and utilize the host name for Kafka within a Docker Compose setup, follow these steps:

  1. Open the docker-compose.yml file and define the environment variable for the Kafka broker host name:
environment:
      KAFKA_ADVERTISED_HOST_NAME: "kafka"
  1. Define the Kafka service in the services section of the docker-compose.yml file:
kafka:
    image: wurstmeister/kafka:latest
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: "kafka"
  1. Define a network for the Kafka service:
networks:
  my_network:
    driver: bridge
  1. Set the KAFKA_ADVERTISED_LISTENERS environment variable in the Kafka service:
environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
  1. Reference the network in all other services that need to connect to the Kafka service:
networks:
      my_network:
        aliases:
          - myapp
      depends_on:
        - kafka
  1. Use the host name kafka in the configuration of the Kafka clients to connect to the Kafka broker on the Docker network:
bootstrap.servers=kafka:9092

Note: Make sure to replace myapp with your application name, and zookeeper:2181 with the actual Zookeeper connection string.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-09-20 11:00:00 +0000

Seen: 1 times

Last updated: Dec 27 '22