我们如何在Cassandra Dockerfile中运行Flyway/迁移脚本?

3
我的Docker文件。
FROM cassandra:4.0
MAINTAINER me

EXPOSE 9042

我想在容器内提取Cassandra镜像并创建超级用户时运行类似的操作。
create keyspace IF NOT EXISTS XYZ WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

我还尝试过添加一个shell脚本,但它从未连接到cassandra,我的修改后的docker文件如下:

FROM cassandra:4.0
MAINTAINER me

ADD entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod 755 /usr/local/bin/entrypoint.sh
RUN mkdir scripts
COPY alter.cql scripts/
RUN chmod 755 scripts/alter.cql

EXPOSE 9042
CMD ["entrypoint.sh"]

我的入口点看起来像这样

#!/bin/bash

export CQLVERSION=${CQLVERSION:-"4.0"}
export CQLSH_HOST=${CQLSH_HOST:-"localhost"}
export CQLSH_PORT=${CQLSH_PORT:-"9042"}

cqlsh=( cqlsh --cqlversion ${CQLVERSION} )

# test connection to cassandra
echo "Checking connection to cassandra..."
for i in {1..30}; do
  if "${cqlsh[@]}" -e "show host;" 2> /dev/null; then
    break
  fi
  echo "Can't establish connection, will retry again in $i seconds"
  sleep $i
done

if [ "$i" = 30 ]; then
  echo >&2 "Failed to connect to cassandra at ${CQLSH_HOST}:${CQLSH_PORT}"
  exit 1
fi

# iterate over the cql files in /scripts folder and execute each one
for file in /scripts/*.cql; do
  [ -e "$file" ] || continue
  echo "Executing $file..."
  "${cqlsh[@]}" -f "$file"
done

echo "Done."

exit 0

我的代码一直连接不上Cassandra数据库,有什么建议吗?谢谢。


当Docker启动时,请附加输出。 - Alex Ott
@AlexOtt Docker一直打印这个Checking connection to cassandra... Can't establish connection, will retry again in 1 seconds Can't establish connection, will retry again in 2 seconds Can't establish connection, will retry again in 3 second,最终终止输出。 - Ankush Virmani
1
你的程序是否监听了本地地址?或许尝试使用容器的实际IP地址? - Alex Ott
@AlexOtt 它默认监听本地主机,我也在shell脚本中提到了它,可能是因为在同一线程上执行shell导致的,就像两个线程试图在同一个线程上运行一样?只是随口想想。 - Ankush Virmani
1个回答

3
你的问题在于你没有调用原始的入口点来启动Cassandra,你用自己的代码覆盖了它,但只是运行而没有启动Cassandra。

你需要修改你的代码,使用安装为/usr/local/bin/docker-entrypoint.sh的原始入口点脚本(源代码)来启动Cassandra,然后执行你的脚本,等待终止信号(不能仅从你的脚本退出,因为这将终止镜像)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接