user3122771
user3122771

Reputation: 65

Unable to connect kafka to zookeeper with different port using docker compose

I am trying to connect my kafka setup with zookeeper contain exposed to a different port other than the default port, however when I change the port to 2181 the container is running fine but if I change the port in my yml file I am not able to run it, any guidance would be helpful.

version: '3'

services:
 zookeeper:
  image: wurstmeister/zookeeper
  container_name: zookeeper
  ports:
   - "2182:2181"
 
 kafka:
  image: wurstmeister/kafka
  container_name: kafka
  ports:
   - "9092:9092"
  environment:
   KAFKA_ADVERTISED_HOST_NAME: localhost
   KAFKA_ZOOKEEPER_CONNECT: zookeeper:2182
 

Upvotes: 2

Views: 2623

Answers (3)

Loathian
Loathian

Reputation: 1227

This is an older thread, but I recently had a need to do this myself (run Zookeeper on a non-standard port using docker compose). If you are not limited to using the wurstmeister image, then the bitnami Zookeeper image can be used for this purpose, as this image exposes an environment variable for changing the default Zookeeper port: ZOO_PORT_NUMBER

The docker-compose configuration would look something like this:

services:
 zookeeper:
  image: bitnami/zookeeper:latest
  ports:
   - "2190:2190"
  platform: linux
  mem_limit: 512m
  environment:
    - ALLOW_ANONYMOUS_LOGIN=yes
    - ZOO_PORT_NUMBER=2190

Then set the environment variable for Kafka to use Zookeeper running on this port with an environment variable: KAFKA_ZOOKEEPER_CONNECT: zookeeper:2190

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191681

Zookeeper has its own config for modifying the client port, which you seem to be confusing with the host port-forwarding.

However, the wurstmeister container has no environment variables to configure it, so you're stuck with 2181.

You probably shouldn't even have a port forward because there's little reason to connect to Zookeeper from the host

Also worth pointing out that that image hasn't been updated since 2019 and Zookeeper has had a few releases since then

Upvotes: 0

Sachith Muhandiram
Sachith Muhandiram

Reputation: 2972

as @DavidMaze suggested. You try to connect 2182 which is exposed to outside. Just try the following.

version: '3'

services:
 zookeeper:
  image: wurstmeister/zookeeper
  container_name: zookeeper
  ports:
   - "2182:2181"
 
 kafka:
  image: wurstmeister/kafka
  container_name: kafka
  ports:
   - "9092:9092"
  environment:
   KAFKA_ADVERTISED_HOST_NAME: localhost
   KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181

Upvotes: 1

Related Questions