0

I have created a docker compose file where my application wants to use kafka.

docker-compose.yaml is:


version: '3.7'

services:
  api:
    depends_on:
      - kafka
    restart: on-failure
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8080:8080

  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"

  kafka:
    image: wurstmeister/kafka
    ports:
      - "9092:9092"
    depends_on:
      - zookeeper
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 192.168.1.7
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_CREATE_TOPICS: "mytopic:1:1"

192.168.1.7 is my ip that i got from ifconfig.

enter image description here

In my service i am giving broker as 192.168.1.7:9092.

When i do docker ps and exec to my kafka container. I am not able to access the 192.168.1.0

enter image description here

What am i doing wrong here though the strange thing is in my application logs i see that the topic is created.

When i try to create the topic:

enter image description here

Avenger
  • 793
  • 11
  • 31

2 Answers2

0

You don't need IP addresses other than 127.0.0.1

192.168.1.7 seems like your host IP, not the docker IP, and yet you are not using network_mode: host, and so the network is not allowing you to connect to the broker.

I recommend finding existing, functional Docker Compose files such as ones in this answer

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

As posted above by @oneCricketeer you don't have to hardcode any of your host ip addresses.

You can connect to broker using "broker" name inside your api itself. And same can be set to advertise host name as well.

user2039152
  • 146
  • 8