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
Easy and concise! Thank you very much for this, hard to believe that a simple migration is so complicated in Cassandra.
When trying this for first time I got an error related with "entrypoint.sh", it was a permission issue and I fixed it by adding a chmod command after the copy command. My final Dockerfile now looks like this and works fine:
Hopefully this will be useful for people encountering the same problem.