Home > Net >  How can we run flyway/migrations script inside Cassandra Dockerfile?
How can we run flyway/migrations script inside Cassandra Dockerfile?

Time:09-28

My docker file

FROM cassandra:4.0
MAINTAINER me

EXPOSE 9042

I want to run something like when cassandra image is fetched and super user is made inside container.

create keyspace IF NOT EXISTS XYZ WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

I have also tried out adding a shell script but it never connects to cassandra, my modified docker file is

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"]

My entrypoint looks like this

#!/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

This never connects to my cassandra Any ideas please help. Thanks .

CodePudding user response:

Your problem is that you don't call the original entrypoint to start Cassandra - you overwrote it with your own code, but it just running the cqlsh, without starting Cassandra.

You need to modify your code to start Cassandra using the original entrypoint script (source) that is installed as /usr/local/bin/docker-entrypoint.sh, and then execute your script, and then wait for termination signal (you can't just exit from your script, because it will terminate the image.

  • Related