How to set up Golang and Cassandra DB on docker

How to set up Golang and Cassandra DB on docker

Few days ago, I was working on a side project where I was using Golang for the backend. Due to the requirements, I needed to use a NoSQL database to store data so Cassandra DB was what I picked to use.

I use wsl2 mostly for my side projects. Installing Cassandra DB on wsl was straightforward but the problem arose when I was unable to connect my go client to Cassandra DB. After spending over 24hrs to get it to work, I was unable to fix the issue of connection so I decided to use docker to run my application instead.

In this article, I will discuss how I was able to set it up on docker

Prerequisites

  • Go module
  • How to use docker(The basic commands)
  • How to use Cassandra DB

In this article, we will create a basic go app and check the connection to Cassandra DB while running our app on docker.

STEP 1 - Set up Cassandra DB on docker.

  • Pull the Cassandra image - docker pull Cassandra

cassandra_pull.png

  • Run the Cassandra image - docker run -d -name cassandra_tutorial -p 9042:9042 cassandra:latest

lat.png

run.png

  • We need to get the IP of the running Cassandra - docker inspect --format='{{ .NetworkSettings.IPAddress }}' cassandra_tutorial

    The IP address is what we will use to connect our go client to Cassandra and execute Cqlsh.

ip.png

  • To execute the cqlsh - docker run -it --link cassandra_tutorial --rm cassandra:latest \bash -c 'exec cqlsh 172.17.0.2'

cqlsh.png

Let us create a Keyspace using Cqlsh

The syntax - CREATE KEYSPACE tutorial with REPLICATION = {'class':'SimpleStrategy','replication_factor':1};

create_keyspace.png

Now we have successfully created a keyspace

STEP 2 - Set up our project.

Our project structure project_structure.png

We will be using gocql to implement our Cassandra client

In our main.go file we will have

package main

import (
    "fmt"
    "github.com/gocql/gocql"
)

func main(){

    cluster := gocql.NewCluster("172.17.0.2")
    cluster.Keyspace = "tutorial"
    cluster.Consistency = gocql.Quorum

    if _,err := cluster.CreateSession(); err != nil{
        panic(err)
    }

    fmt.Println("Connection successfull...")
}

The next task to do is to set up our Dockerfile

FROM golang:alpine 

RUN mkdir /app

ADD . /app

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . .

RUN go build -o main .

CMD ["/app/main"]

Now we need to build and to do that we run - docker build -t tutorial .

build.png

Our build is successful

After a successful build, we run our built image - docker run tutorial:latest

run_build.png

As you can see from the above image, we got a connection successful and no error was shown so this means that we have successfully connected to Cassandra

IMPORTANT

In this guide, we ran our containers separately but there is a better way to run multi-containers. To do this, we can use docker compose. Check out the documentation here.

END NOTE

As you can see above, using Cassandra is super easy as it integrates well with your application

Thank you for reading!!