连接到ZooKeeper映像容器并运行zookeeper命令

时间:2017-10-30 11:15:03

标签: docker docker-compose dockerfile apache-zookeeper

我在ZooKeeper上通过docker-compose运行Docker图片。

我的档案是:

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

docker-compose.yaml包含名称和属性:

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目前仅包含图像

FROM zookeeper:3.4

在本地我可以运行zkCli.sh并与zookeeper通信,但我希望在Dockerfile.zookeeper运行时自动执行。

我是否需要使用vm创建容器,安装zookeeper并将zkCli.sh复制到容器中以运行命令?

或者是否可以通过Dockerfile运行zookeeper命令?

我已尝试过太依附于容器并在dockerfile中使用CMD,但它无效。

任何想法我怎么能这样做?

谢谢

1 个答案:

答案 0 :(得分:0)

为了解决我写的bash脚本,谁获得了zookeeper主机和一个zookeeper脚本(带有zookeeper命令的逐行文件)

并运行包含zookeeper映像的远程docker上的所有命令:

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

zookeeper脚本示例:

create /x 1
create /y 2

用法:

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