Warm tip: This article is reproduced from stackoverflow.com, please click
apache-kafka apache-zookeeper docker docker-compose

Kafka and Zookeeper Scripts Not Found Using Docker

发布于 2020-03-27 10:17:27

Detail: I'm trying to run wurstmeister/kafka and wurstmeister/zookeeper image using docker-compose.yml file which is attached at the bottom of this post.

To test the containers, I have followed the official site of kafka. It refers to some scripts like bin/kafka-server-start.sh that isn't available in the image. I have also tried to go through the containers using docker exec -it zookeeper bash and docker exec -it kafka bash and found zkServer.sh and some other scripts in the /bin/ directory.

Problem: The problem is that I don't know how to use it, because the config/server.properties is available in the kafka container not zookeeper. So I'm not aware of the proper solution. Is it a good solution to download the scripts and put them in the host directory and copy them to the container?

docker-compose.yml

version:'2'

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

  kafka:
    build: .
    container_name: kafka
    links:
     - zookeeper
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka
      KAFKA_ADVERTISED_PORT: 9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_OPTS: -javaagent:/usr/app/jmx_prometheus_javaagent.jar=7071:/usr/app/prom-jmx-agent-config.yml
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

Dockerfile

FROM wurstmeister/kafka

ADD prom-jmx-agent-config.yml /usr/app/prom-jmx-agent-config.yml
ADD jmx_prometheus_javaagent-0.10.jar /usr/app/jmx_prometheus_javaagent.jar
Questioner
Mostafa Ghadimi
Viewed
138
1,241 2019-07-03 21:57

You don't have to use bin/kafka-server-start.sh script. There is script called start-kafka.sh in wurstmeister/kafka image. Here is how I did it in one of my projects:

docker-compose.yml

zookeeper:
    container_name: zookeper
    image: wurstmeister/zookeeper:3.4.6
    ports:
    - "2181:2181"
  kafka:
    container_name: kafka
    build:
      context: .
      dockerfile: kafka.Dockerfile
    image: kafka-service
    ports:
        - "9092:9092"

kafka.Dockerfile

FROM wurstmeister/kafka:2.11-2.0.0
COPY wait-for-it.sh /wait-for-it.sh
RUN chmod +x /wait-for-it.sh
CMD ["/wait-for-it.sh", "zookeper:2181", "--", "start-kafka.sh"]

Here in dockerfile, I essentially say: "Wait for zookeeper, he has to be up first,then you can start, then after him, run start-kafka.sh". You can download wait-for-it.sh script here: https://github.com/vishnubob/wait-for-it/blob/master/wait-for-it.sh

PS: You can see the essential information of any image using the following command:

docker inspect <image-name>