简体   繁体   中英

Kafka broker not accessible in docker compose

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.

在此处输入图像描述

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

在此处输入图像描述

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:

在此处输入图像描述

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM