How to run cassandra in docker

How to run cassandra in docker

1) Run `docker pull cassandra:latest`. This is to get the image used to run the cassandra node locally.

2) Run `docker run -p 7000:7000 -p 7001:7001 -p 7199:7199 -p 9042:9042 -p 9160:9160 --name cassandra -d -ti --entrypoint=sh cassandra`. This command will create and run the docker container in the background.
Note: 
- If you are looking to edit your cassandra.yaml file, restarting the docker container will not persist the change that you've made in the docker container. To solve this, you need to mount a volume that mounts the file from your local PC to the docker container. To achieve this, use this command:
    -v "{absolute_path_to_your_file}:/etc/cassandra/cassandra.yaml"
    For example, if I were to store my file in C:\Users\nic35, then my absolute_path_to_my_file would be:
    C:\Users\nic35\cassandra.yaml.
- If you are looking to run the docker container in the background, make sure to include the flag `-d` in your command.

How to run interactive prompt in your Cassandra docker container?

To run interactive prompt in your cassandra docker container, you need to run this command:
`docker exec -it {docker_container_name} bash
Note:
- Make sure that the `bash` is placed at the end of the command, or else it won't work
- If you ever encounter an error that states your container is not ran, try using the following steps:
    1) Run `docker pull cassandra`
    2) Run `docker run --name {container_name} -d -ti --entrypoint=sh 
cassandra`
    The above command should create and run your docker container in the background.
Let's say if your container still doesn't run, you can try the following alternative:
    1) `docker commit {container_name} {new_image_name}
    2) `docker run {new_image_name} -ti --entrypoint=sh`
That would do the trick. Essentially what it is doing is, it creates an image with the same file system from the container. So you can run a brand new container with the newly created image. More info here


What if the above solution doesn't work?

If the above doesn't work, here is an alterative, and the steps you can take to resolve this:
1) Ensure Cassandra, Java 8 (much preferred as many programmers find this version works) and and Python 3 (Python 2 is no longer supported) is installed locally in your PC
2) Once you have installed and configured the environment path of Cassandra in your PC, head over to the `cassandra.bat` file under the directory that you have installed Cassandra. For my case, it's `C:\apache-cassandra-3.11.9-bin\apache-cassandra-3.11.9\bin`.
3) Then, edit the file. Search for `set UNINSTALL="UNINSTALL"`, and under it, add `set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_202`
4) Do the same for the file nodetool.bat. Under the comment, add the same line `set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_202`. The comment is in the form of  `@REM {context}.
5) Once that's done, run the `cassandra` command, and your cassandra server should start running.

Now, this is what worked for me, there could be much more efficient way of doing it:

6) Go to your docker
7) Create a container with the cassandra:latest image
8) Access the container prompt
9) Run `cqlsh {cassandra_server_ip_address} {port}

I've encountered some error, while following the above steps, what should I do?

Most likely, you wouldn't be able to connect to the cassandra server when trying to perform `cqlsh`, here is what to do:
1) Make sure that the rpc_address in the cassandra.yaml is configured to the actual IP of your PC. The cassandra.yaml file is located under cassandra installed in your PC, instead of the one in docker
2)  Create your container by specifying the port 9042:9042. For example, docker run -p 9042:9042 {image}

Comments

Popular posts from this blog

Support for the experimental syntax 'decorators' isn't currently enabled (5:3):

How to add environment variables into Heroku container

Running CQL scripts in Cassandra