1

I'm trying to set up a single-broker per node kafka cluster using docker. The idea is multiple docker containers will contain a broker each, and I'll have another docker container as a zookeeper node. I'm building an image where I can pass the zookeeper address and the broker id as environment variables which will be used to start the broker. This will be useful to bring up multiple nodes with different broker id's by just passing them as env variables when I launch the container from the image.

This is my dockerfile:

FROM openjdk:8

RUN mkdir /kafka
WORKDIR /kafka

COPY kafka_2.12-2.3.0 kafka_2.12-2.3.0

COPY cmd.sh cmd.sh

RUN cat /etc/os-release
RUN chmod +x ./cmd.sh

CMD ["./cmd.sh"]

EXPOSE 9092

And this is my cmd.sh

#!/bin/sh

export KAFKA_DIR=/kafka/kafka_2.12-2.3.0

if [[ -z $KAFKA_BROKER_ID || -z $KAFKA_ZOOKEEPER_ADDRESS ]]; then
  echo 'Zookeeper address and broker id need to be set by env variables KAFKA_ZOOKEEPER_ADDRESS and KAFKA_BROKER_ID'
  exit 1
fi

echo "Broker ID: ${KAFKA_BROKER_ID}"
echo "Zookeeper addresss: ${KAFKA_ZOOKEEPER_ADDRESS}"

"${KAFKA_DIR}"/bin/kafka-server-start.sh "${KAFKA_DIR}"/config/server.properties --override broker.id="${KAFKA_BROKER_ID}" --override zookeeper.connect="${KAFKA_ZOOKEEPER_ADDRESS}"

I got this from the question How to check if multiple variables are defined or not in bash

I expect to see the error message but instead I'm getting this:

./cmd.sh: 5: ./cmd.sh: [[: not found ./cmd.sh: 5: ./cmd.sh: -z: not
found Broker ID: Zookeeper addresss: [2019-07-03 08:56:04,172] INFO
Registered kafka:type=kafka.Log4jController MBean
(kafka.utils.Log4jControllerRegistration$) [2019-07-03 08:56:04,752]
ERROR Exiting Kafka due to fatal exception (kafka.Kafka$)
org.apache.kafka.common.config.ConfigException: Invalid value  for
configuration broker.id: Not a number of type INT

syam
  • 799
  • 1
  • 12
  • 30

1 Answers1

1

if ( -z "$KAFKA_ZOOKEEPER_ADDRESS" || -z "$KAFKA_BROKER_ID" ) will not worked on bash that is invalid syntax:

you should use:

if [ -z "$KAFKA_ZOOKEEPER_ADDRESS" || -z "$KAFKA_BROKER_ID" ]; then

and change your shebang to #!/bin/bash

LinPy
  • 16,987
  • 4
  • 43
  • 57
  • I edited the question and the output to reflect the syntax in the original answer. The parenthesis was my own idea after which it didn't work either – TheLastBrainCell Jul 03 '19 at 08:59
  • after trying that I got this: docker run -p 9092:9092 localhost:5000/openjdk-kafka-broker Broker ID: Zookeeper addresss: ./cmd.sh: 5: [: missing ] ./cmd.sh: 5: ./cmd.sh: -z: not found – TheLastBrainCell Jul 03 '19 at 09:00
  • ```if [ -z "$KAFKA_ZOOKEEPER_ADDRESS" || -z "$KAFKA_BROKER_ID" ]; then``` didn't work either. it says ```./cmd.sh: 5: [: missing ] ./cmd.sh: 5: ./cmd.sh: -z: not found``` – TheLastBrainCell Jul 03 '19 at 09:08