3

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 .

  • attache output when docker starts – Alex Ott Sep 26 '21 at 07:46
  • @AlexOtt Docker keeps on printing this `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`, and terminate out finally – Ankush Virmani Sep 26 '21 at 13:22
  • 1
    Do you have it listening on localhost? Maybe try actual container IP instead? – Alex Ott Sep 26 '21 at 20:55
  • @AlexOtt It by defaults listen to localhost and i have also mentioned it in shell script, can it be because of execution of shell on the same thread which sort of like a two threads trying to run on same ?? Just thinking out loud? – Ankush Virmani Sep 27 '21 at 05:15

1 Answers1

3

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.

Alex Ott
  • 80,552
  • 8
  • 87
  • 132