Ami Hollander
Ami Hollander

Reputation: 2535

Connect to ZooKeeper image container and run zookeeper commands

I am running ZooKeeper image on Docker via docker-compose.

My files are:

--repo
----script.zk (contains zookeeper script commands such as `create`
----docker-compose.yaml
----Dockerfile.zookeeper
----zoo.cfg

docker-compose.yaml contains names and properties:

services:
  zoo1:
      restart: always
      hostname: zoo1
      container_name: zoo1
      build:
        context: .
        dockerfile: Dockerfile.zookeeper
      volumes:
       - $PWD/kyc_zookeeper.cfg:/conf/zoo.cfg
      ports:
          - 2181:2181
      environment:

      .... and two more nodes

Dockerfile.zookeeper currently contains only image

FROM zookeeper:3.4

Locally I can run zkCli.sh and communicate with zookeeper, but i wish to do it automatically when Dockerfile.zookeeper runs.

do I need to create a container with a vm, install zookeeper and copy the zkCli.sh into the container in order to run commands?

Or is it possible to run zookeeper commands via Dockerfile?

I've tried too attach to the container and using CMD in dockerfile but it's not working.

any idea how I can do it?

Thank you

Upvotes: 1

Views: 3290

Answers (1)

Ami Hollander
Ami Hollander

Reputation: 2535

In order to resolve that I wrote bash script, who gets zookeeper host and a zookeeper script (file with zookeeper commands line-by-line)

and run all the commands on the remote docker who contains zookeeper image:

config_script_file="$2"
zookeeper_host_url="$1"

#Retrieve all commands from file
    TMPVAR=""
    while read -r line
    do
        if [ -z "$TMPVAR" ]; then
            TMPVAR="$line"
        else
            TMPVAR="$TMPVAR\n$line"
        fi
    done < "$config_script_file"

#Run ZooKeeper commands on remote machine
docker exec -i $zookeeper_host_url bash << EOF
./bin/zkCli.sh -server localhost:2181
$(echo -e ${TMPVAR})
quit
exit
EOF

example of a zookeeper script:

create /x 1
create /y 2

Usage:

./zkCliHelper.sh <zookeeper_url> <script.zk file>

Upvotes: 1

Related Questions