3

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!

9
  • check your post because you have different ip's: 172 and 127... and it's pretty confusing Commented Jun 12, 2018 at 7:15
  • Did you expose the port of the Cassandra Docker container? docker run -p [host_port]:[cassandra_port] ? Commented Jun 12, 2018 at 7:28
  • Sorry. It is actually 127. :p I am experimenting different IP that is why.. but generally its 127.0.0.1 Commented Jun 12, 2018 at 7:29
  • @Mornor what should i write on the [host_port]:[cassandra_port] ? Should i write, docker run -p 8080:8080 Commented Jun 12, 2018 at 7:32
  • 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 Commented Jun 12, 2018 at 7:36

1 Answer 1

5

Just adding 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.