【问题标题】:How can we run flyway/migrations script inside Cassandra Dockerfile?我们如何在 Cassandra Dockerfile 中运行 flyway/migrations 脚本?
【发布时间】:2021-11-18 17:18:40
【问题描述】:

我的 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 启动时附加输出
  • @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,并最终终止
  • 你有它在 localhost 上监听吗?也许试试实际的容器 IP?
  • @AlexOtt 默认情况下,它听本地主机,我也在 shell 脚本中提到它,可能是因为在同一个线程上执行 shell 有点像两个线程试图在同一个线程上运行??只是大声思考?

标签: docker cassandra dockerfile cqlsh


【解决方案1】:

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

您需要修改您的代码以使用安装为 /usr/local/bin/docker-entrypoint.sh 的原始入口点脚本 (source) 启动 Cassandra,然后执行您的脚本,然后等待终止信号(您不能只退出你的脚本,因为它会终止图像。

【讨论】:

  • 非常感谢@Alex
猜你喜欢
  • 2018-04-12
  • 1970-01-01
  • 2016-04-05
  • 2020-08-20
  • 2021-08-26
  • 2020-01-02
  • 2022-01-15
  • 1970-01-01
  • 2020-08-17
相关资源
最近更新 更多