This gist shows you how to easily create a cassandra image with initial keyspace and values populated.
It is very generic: the entrypoint.sh is able to execute any cql file located in /docker-entrypoint-initdb.d/,
a bit like what you do to initialize a MySQL container.
You can add any *.sh or *.cql scripts inside /docker-entrypoint-initdb.d, but note that:
*.shfiles will be executed BEFORE launching cassandra*.cqlfiles will be executed (withcqlsh -f) AFTER cassandra started
Files are executed in name order (ls * | sort)
- download the
Dockerfileandentrypoint.sh - edit the
Dockerfilein order to copy your init scripts inside/docker-entrypoint-initdb.d/ - build the image:
docker build -t my-cassandra-image . - run the image:
docker run --rm -p 9042:9042 --name cassandra-container -d my-cassandra-image
Note that the scripts in /docker-entrypoint.sh will only be called on startup. If you decide to persist the data using a volume,
this will work all right: the scripts won't be executed when you boot your container a second time. By using a volumne, I mean, e.g.:
docker run --rm -d \
-p 9042:9042 \
-v $PWD/data:/var/lib/cassandra \
--name cassandra-container \
my-cassandra-image
This works, but a few notes:
I had to remove the sed command on line 54 of
entrypoint.sh, the file doesn't seem to exist in my Cassandra 4.0 imageAlso, it's best to put
set -eat the beginning ofentrypoint.shso the container doesn't deploy with a failed init.In the end, I just used the Bitnami docker image instead, since it does the same thing by default, and it provides a non-root user for prod deployment