I was wondering if someone has tried to build a cassandra docker image with default keyspace, I've tried to do it on BUILD time but it doesn't work because cassandra is not running in that phase. It was something similar to this:
FROM cassandra:2.0
COPY ../somewhere/keyspace_definition.txt /src/keyspace_definition.txt
RUN /usr/bin/cqlsh -f /src/keyspace_definition.txt
My new approach will be to do it from the entrypoint script, but, I wanted to now if someone else has a better idea.
Happy shipping :D
Base on answers from @jan-oudrincky and @alexander-morozov, I build a new docker image which has a wrapper of original docker-entrypoint.sh
to create keyspace when environment variable CASSANDRA_KEYSPACE
is set. It will be useful in dev/test environment.
It doesn't modify docker-entrypoint.sh
so even if cassandra base image has any modification you just need a rebuild.
Dockerfile
FROM cassandra
COPY entrypoint-wrap.sh /entrypoint-wrap.sh
ENTRYPOINT ["/entrypoint-wrap.sh"]
CMD ["cassandra", "-f"]
entrypoint-wrap.sh
#!/bin/bash
if [[ ! -z "$CASSANDRA_KEYSPACE" && $1 = 'cassandra' ]]; then
# Create default keyspace for single node cluster
CQL="CREATE KEYSPACE $CASSANDRA_KEYSPACE WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};"
until echo $CQL | cqlsh; do
echo "cqlsh: Cassandra is unavailable - retry later"
sleep 2
done &
fi
exec /docker-entrypoint.sh "$@"