Docker检查DB是否正在运行

时间:2017-03-12 19:39:26

标签: bash docker cassandra

entrypoint.sh包含需要Cassandra的各种cqlsh命令。没有像script.sh这样的东西,cqlsh命令会失败,因为Cassandra没有足够的时间来启动。当我在本地执行以下操作时,一切似乎都正常工作。但是,当我通过Docker运行时,script.sh永远不会完成。换句话说,$status永远不会从1更改为0.

Dockerfile

FROM cassandra
RUN apt-get update && apt-get install -y netcat
RUN mkdir /dir
ADD ./scripts /dir/scripts
RUN /bin/bash -c 'service cassandra start'
RUN /bin/bash -c '/dir/scripts/script.sh'
RUN /bin/bash -c '/dir/scripts/entrypoint.sh'

script.sh

#!/bin/bash

set -e
cmd="$@"

status=$(nc -z localhost 9042; echo $?)
echo $status

while [ $status != 0 ]
do
  sleep 3s
  status=$(nc -z localhost 9042; echo $?)
  echo $status
done

exec $cmd

或者,我可以为until cqlsh -e 'some code'; do ..做一些事情,如psql所指出的here,但这似乎对我不起作用。想知道如何最好地解决问题。

1 个答案:

答案 0 :(得分:3)

您在Dockerfile中滥用RUN命令。它不是用于启动服务,而是用于在图像中进行文件系统更改。 $status无法更新的原因是因为您无法通过RUN命令启动Cassandra。

您应该将service cassandra start/dir/scripts/entrypoint.sh添加到script.sh文件中,然后将CMD默认执行:

<强> Dockerfile

CMD ['/bin/bash', '-c', '/dir/scripts/script.sh']

<强> script.sh

#!/bin/bash

set -e
# NOTE: I removed your `cmd` processing in favor of invoking entrypoint.sh
# directly.

# Start Cassandra before waiting for it to boot.
service cassandra start

status=$(nc -z localhost 9042; echo $?)
echo $status

while [ $status != 0 ]
do
  sleep 3s
  status=$(nc -z localhost 9042; echo $?)
  echo $status
done

exec /bin/bash -c /dir/scripts/entrypoint.sh