Introduction
Apache Cassandra is a highly scalable, distributed NoSQL (Not Only SQL) database management system designed to handle large amounts of data across many commodity servers without any single point of failure. It is part of the Apache Software Foundation and is open-source, meaning that its source code is freely available for the public to use, modify, and distribute.
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define an entire application stack, including services, networks, and volumes, in a single file called docker-compose.yml. With a single command, you can then spin up your entire application stack, simplifying the process of managing and deploying multi-container Docker applications.
Prerequisites
- Up and running ubuntu 22.04 LTS machine.
- Basic knowledge in linux commands.
- Internet connectivity.
- Docker and docker compose should installed on your machine.
To install Docker and docker compose on ubuntu 22.04 LTS, We can use the given link.
In this post, We will deploy Cassandra database container using Docker Compose on Ubuntu 22.04 LTS machine.
Step 1: Creating docker-compose.yml
Using Docker Compose simplifies the process of deploying and managing multi-container Docker applications. Here are the steps to deploy a Cassandra container with Docker Compose:
To create a docker-compose.yml
sudo nano docker-compose.yml
Copy and paste the followings configuration.
version: '3'
services:
cassandra:
image: cassandra:latest
container_name: my-cassandra-container
ports:
- "9042:9042"
environment:
- CASSANDRA_CLUSTER_NAME=mycluster
- CASSANDRA_SEEDS=127.0.0.1
volumes:
- cassandra-data:/var/lib/cassandra
volumes:
cassandra-data:
This docker-compose.yml
file defines a single service named “cassandra” using the official Cassandra Docker image. The service is configured to expose Cassandra’s default port (9042), set the cluster name, and specify the seed node.
Save this content in a file named docker-compose.yml
Step 2: Deploy Cassandra Container
In the directory where your docker-compose.yml
file is located, run the following command to start the Cassandra container:
sudo docker-compose up -d
The -d flag runs the containers in the background, It will take few sec or mins to pull the image and deploy the Cassandra.
Step 3: Validate the Container
To check the running containers launched by Docker Compose, you can use the following command:
sudo docker-compose ps
This command provides a summary of the status of each service defined in your docker-compose.yml file. It shows information such as the service name, container ID, status, ports, and names.
Step 4: Logging
Some time we need to also check the real time logs, So we can use the given commands.
sudo docker-compose logs -f Cassandra
Step 5: Cassandra connection PHP
To connect to Apache Cassandra using PHP, you can use a PHP driver for Cassandra called DataStax PHP Driver for Apache Cassandra. Here’s a simple example demonstrating how to connect to Cassandra and execute a basic query using this driver. Before running this code, make sure you have the DataStax PHP Driver installed. You can use Composer to install it:
composer require datastax/php-driver
Now, you can create a PHP script with the following code:
<?php
// Load the Composer autoloader
require_once __DIR__ . '/vendor/autoload.php'; // Adjust the path as needed
use Cassandra\Cluster;
use Cassandra\Session;
// Set the connection parameters
$host = '127.0.0.1';
$port = 9042;
$keyspace = 'your_keyspace';
$username = 'your_username';
$password = 'your_password';
// Create a Cassandra cluster
$cluster = Cluster::build()
->withContactPoints($host)
->withPort($port)
->withDefaultConsistency(\Cassandra::CONSISTENCY_QUORUM)
->withCredentials($username, $password)
->build();
// Create a session to interact with Cassandra
$session = $cluster->connect($keyspace);
// Execute a basic query
$query = 'SELECT * FROM your_table'; // Replace 'your_table' with the actual table name
$statement = new Cassandra\SimpleStatement($query);
$result = $session->execute($statement);
// Process the result
foreach ($result as $row) {
print_r($row);
}
// Close the Cassandra session and cluster
$session->close();
$cluster->close();
Make sure to replace placeholders like ‘your_keyspace‘, ‘your_username‘, ‘your_password‘, and ‘your_table‘ with your actual Cassandra keyspace, username, password, and table information.
Step 6: Destroy Cassandra Container
To destroy (stop and remove) the Cassandra container launched with Docker Compose, you can use the following command in the directory where your docker-compose.yml
file is located:
sudo docker-compose down
This command stops and removes all the containers, networks, and volumes defined in your docker-compose.yml
file.
The down command stops and removes the containers but retains the data volumes by default. If you want to remove the volumes as well, you can use the -v
option:
Make sure you are in the correct directory containing your docker-compose.yml
file when running these commands. This ensures that Docker Compose identifies the correct configuration file.
After running the docker-compose down command, you can use the docker-compose ps
command to verify that the containers are no longer running. The output should be empty, indicating that no containers are currently running.
Remember that this command will stop and remove all the services defined in your docker-compose.yml
file, not just the Cassandra service. If you only want to remove a specific service, you can specify the service name:
docker-compose down -v <service_name>
Conclusion
We have successfully deployed Apache Cassandra database server on docker’s container using docker compose on ubuntu 22.04 LTS machine, If you still have questions, please post them in the comments section below.
finasteride brand name