Cassandra on docker
Create a docker file to pull the official docker image. For directory tree refer the screenshot below.
$ cat ~/docker-app/cassandra/Dockerfile
FROM cassandra:3.11.3
RUN apt-get update -y
COPY . /app
WORKDIR /app
VOLUME /var/lib/cassandra/data
CMD ["cassandra", "-f"]
Note, that we have a VOLUME property that tells the container has a mount point that refers to a host directory. When running the container, we provide the mapping between the host directory and this volume path. Purpose of creating such mount point is to persist data if the container is terminated and also also share a directory among the containers.
Cassandra cql statements to create a keyspace, a table and a few sample records.
$ cat ~/docker-app/cassandra/init.cql
create KEYSPACE IF NOT EXISTS demo WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
create TABLE IF NOT EXISTS demo.customer(id int primary key, name text);
insert into demo.customer(id, name) values (1, 'Elon');
insert into demo.customer(id, name) values (2, 'Jeff');
insert into demo.customer(id, name) values (3, 'Satya');
insert into demo.customer(id, name) values (4, 'Sundar');
Directory tree
$ tree ~/docker-app/
/home/training/docker-app/
├── cassandra
├── Dockerfile
└── init.cql
Build local image
$ sudo docker build -t cassandra-db:3.3.1 .
Start a container
$ sudo docker run -p 9042:9042 -v /data:/var/lib/cassandra/data cassandra-db:3.3.1
Note, /data is mapped to the mount point inside the container.
Run the init.cql to create keyspace, table and sample records in cassandra.
$ sudo docker exec -it db20f8307642 cqlsh -f init.cql
$ sudo docker exec -it db20f8307642 cqlsh -e "select * from demo.customer"
id | name
----+--------
1 | Elon
2 | Jeff
4 | Sundar
3 | Satya
Force flush to persist the data on disk.
$ sudo docker exec -it 583dcea04e8b nodetool flush
Terminate the container
$ sudo docker container rm -f 583dcea04e8b
Create a new container for Cassandra node.
$ sudo docker run -p 9042:9042 -v /data:/var/lib/cassandra/data cassandra-db:3.3.1
Check if the records that were inserted earlier are visible
$ sudo docker exec -it db20f8307642 cqlsh -e "select * from demo.customer"
id | name
----+--------
1 | Elon
2 | Jeff
4 | Sundar
3 | Satya
Note: at any point run $ sudo docker ps -a command to find the container id.