2
votes

I have the following java code:

String serverIP = "127.0.0.1";  
String keyspace = "testkeyspace"; 
Cluster cluster = Cluster.builder().addContactPoints(serverIP).build();  
Session session = cluster.connect(keyspace);

which i am trying to use to check my connection to Cassandra.

I have Cassandra locally installed in my own machine, and connecting there is successful. My maven project is using the following

spring-data-cassandra 2.0.6.RELEASE

However, when i am trying to connect my java application to Cassandra run by docker (with my local cassandra stopped) i encounter the following error:

All host(s) tried for query failed (tried: /127.0.0.1:9042 (com.datastax.driver.core.exceptions.TransportException: [/127.0.0.1:9042] Cannot connect))

I do not know if my IP is wrong, but on my docker CQLSH the following message is written whenever i open my CQLSH:

Connected to Test Cluster at 127.0.0.1:9042. [cqlsh 5.0.1 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4] Use HELP for help.

Please help, i must have missed something here. Thank you!

1
check your post because you have different ip's: 172 and 127... and it's pretty confusing - Horia
Did you expose the port of the Cassandra Docker container? docker run -p [host_port]:[cassandra_port] ? - Mornor
Sorry. It is actually 127. :p I am experimenting different IP that is why.. but generally its 127.0.0.1 - TeeKei
@Mornor what should i write on the [host_port]:[cassandra_port] ? Should i write, docker run -p 8080:8080 - TeeKei
I don't know which port Cassandra uses by default. From your post, it seems tp be :9042. So the Docker command should ne something like: docker run -p 10080:9042 cassandra:latest. You will then be able to access Cassandra on localhost:10080 - Mornor

1 Answers

4
votes

Just add my answer here, as it's been accepted as a comment in order to help others with the same issue.

You should expose your Cassandra Docker container port to localhost with:

docker run -p 9042:9042 cassandra:latest

Cassandra should now be accessible on localhost:9042