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!
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