In this guide we are going to explore how to run Cassandra locally with docker and docker compose. This can be helpful if you want to run
Cassandra locally without installing it in your server or if you want to run multiple versions of Cassandra seamlessly.Also check:
- How to Install and Configure Apache Cassandra 4.0 in Rocky/Alma Linux 9
- Running Postgresql 14 with Docker and Docker-Compose
- How to run MsSQL Server 2019 with Docker and Docker-Compose
- How to run Mysql 8 with Docker and Docker-Compose
- How to run Redis 6 with Docker and Docker-Compose
Prerequisites
To follow along, ensure you have the following:
- Docker installed locally and permissions to use it to launch containers
- Docker compose is installed locally
- Basic knowledge of the command line
Table of content
- Using the
docker run
command - Using the
docker-compose
tool
1. Using the docker run command
We are going to use the docker run
command to achieve our goal. The version of Cassandra that we want is cassandra:4.0
– the latest version of cassandra.
➜ docker run -d \
--name my-cassandra \
-p 9042:9042 \
-v ~/apps/cassandra:/var/lib/cassandra \
-e CASSANDRA_CLUSTER_NAME=citizix \
cassandra:4.0
Unable to find image 'cassandra:4.0' locally
4.0: Pulling from library/cassandra
7b1a6ab2e44d: Pull complete
237daeb1ae28: Pull complete
5525469b77d0: Pull complete
3d55dee44697: Pull complete
e9433e47fbec: Pull complete
2b83006bed25: Pull complete
e6aac180715e: Pull complete
7743412d1917: Pull complete
4b91229d79d5: Pull complete
Digest: sha256:cf6ae1b3e6253c9cde91dbde6dad1d6c0314939e0f9e18253c6308482e505728
Status: Downloaded newer image for cassandra:4.0
6071e47c57f7b90d58a03ede26ce2eee220298108fd12d356525694363db4c80
In the above command:
- The
-d
instructs docker container to run as a detached process. It run container in background and print container ID -p
is for port mapping. We are instructing the container to expose the container port externally. Container port9042
is mapped to host port9042
. That means the service can be accessed throughlocalhost:9042
- The
-v
directive is used to mount volumes. In our case we are mounting the container volume/var/lib/cassandra
to host path~/apps/cassandra
. Containers are ephemeral devices that will contain its data for the time it is running. Once a container is stopped, its data is lost. Mounting volumes ensures that the data is added to a host path that can be reused when the container is restarted. - The
-e
argument is for the environment variables. The supplied environment variables will be used to set up cassandra cluster name.
To check that our container is running as expected, use the docker ps
command:
➜ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6071e47c57f7 cassandra:4.0 "docker-entrypoint.s…" 50 seconds ago Up 47 seconds 7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9042->9042/tcp, :::9042->9042/tcp my-cassandra
In my case the container is running as my-cassandra
the name we gave it. We can login to the container using the docker exec
command while executing /bin/bash
interactively. Here we are also logging in to posgtres with the credentials we specified above and checking the version.
➜ docker exec -it my-cassandra /bin/sh
# cqlsh -u cassandra -p cassandra
Connected to citizix at 127.0.0.1:9042
[cqlsh 6.0.0 | Cassandra 4.0.1 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
cassandra@cqlsh>
cassandra@cqlsh> show version
[cqlsh 6.0.0 | Cassandra 4.0.1 | CQL spec 3.4.5 | Native protocol v5]
cassandra@cqlsh> describe keyspaces
system system_distributed system_traces system_virtual_schema
system_auth system_schema system_views
cassandra@cqlsh>
If you need to clean up the container when not in use, you can stop and remove the container using this command:
docker stop my-<meta charset="utf-8">cassandra
# Removing
docker rm my-<meta charset="utf-8">cassandra
2. Using the docker-compose tool
We can achieve the same functionality with docker-compose
. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services.
Docker Compose allows you to define the service (Cassandra in our case) with properties like the image
to use, ports
to expose, volumes
to mount and environment
variables.
Here is how we would use docker-compose
to achieve the functionality above. Save this as docker-compose.yaml
:
version: '3.9'
services:
cassandra:
image: cassandra:4.0
ports:
- 9042:9042
volumes:
- ~/apps/cassandra:/var/lib/cassandra
environment:
- CASSANDRA_CLUSTER_NAME=citizix
Now bring up the containers:
➜ docker-compose up -d
Starting tmp_cassandra_1 ... done
The commands:
up
brings up the container-d
in a detached mode
Verify the container processes using the ps command:
➜ docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------------------------------------------------------------
tmp_cassandra_1 docker-entrypoint.sh cassa ... Up 7000/tcp, 7001/tcp, 7199/tcp, 0.0.0.0:9042->9042/tcp,:::9042->9042/tcp, 9160/tcp
To login to the container and login to Cassandra, use this:
➜ docker-compose exec cassandra /bin/bash
root@08cda36eec24:/# cqlsh -u cassandra -p cassandra
Connected to citizix at 127.0.0.1:9042
[cqlsh 6.0.0 | Cassandra 4.0.1 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
cassandra@cqlsh> show version
[cqlsh 6.0.0 | Cassandra 4.0.1 | CQL spec 3.4.5 | Native protocol v5]
cassandra@cqlsh> describe keyspaces
system system_distributed system_traces system_virtual_schema
system_auth system_schema system_views
cassandra@cqlsh>
Conclusion
In this guide we managed to run Cassandra 4 as a container in our system, we explored using the docker run
command while passing the required arguments an alternative approach of simplifying the process with docker-compose