Step-by-step guide to set up Apache Cassandra using Docker

Step-by-step guide to set up Apache Cassandra using Docker:


🧰 Prerequisites


🚀 Step-by-Step Guide

Step 1: Pull Cassandra Docker Image

Open your terminal and run:

docker pull cassandra:latest

You can also use a specific version like cassandra:4.1.


Step 2: Start Cassandra Container

docker run --name cassandra-node1 -d cassandra:latest
  • --name: Gives the container a name.
  • -d: Runs it in the background.

This will start a single-node Cassandra instance.


Step 3: Wait for Cassandra to Fully Start

Check logs:

docker logs -f cassandra-node1

Wait until you see:

Startup complete

Step 4: Access Cassandra CLI (CQLSH)

Run CQLSH (Cassandra shell) from inside the container:

docker exec -it cassandra-node1 cqlsh

This opens an interactive CQL shell prompt:

Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh]>

Step 5: Create a Keyspace (Database)

In the CQL shell, create a keyspace:

CREATE KEYSPACE testks
WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};

Step 6: Create a Table

USE testks;

CREATE TABLE users (
    id UUID PRIMARY KEY,
    name text,
    email text
);

Step 7: Insert Data

INSERT INTO users (id, name, email)
VALUES (uuid(), 'Alice', 'alice@example.com');

Step 8: Query Data

SELECT * FROM users;

🛑 Optional: Stop and Remove the Container

Stop:

docker stop cassandra-node1

Remove:

docker rm cassandra-node1

🧪 Multi-node Setup (Optional Advanced)

You can spin up more nodes with Docker networks and environment variables like:

docker network create cassandra-net

docker run --name cassandra-node1 --network cassandra-net -d cassandra:latest

docker run --name cassandra-node2 --network cassandra-net -e CASSANDRA_SEEDS=cassandra-node1 -e CASSANDRA_CLUSTER_NAME="Test Cluster" -d cassandra:latest