Lorybi
Lorybi

Reputation: 23

cassandra.cluster.NoHostAvailable when connecting with Python to a docker container

I have a docker container created with this command (the volume is empty for the moment):

docker run --name mabase-cassandra -v /db/docker_saves/cassandra:/var/lib/cassandra -d cassandra:3

When I try to connect to it using python (when the container is running of course), I get an error which says :

session = cassandra_cluster.connect()
  File "cassandra\cluster.py", line 1667, in cassandra.cluster.Cluster.connect
  File "cassandra\cluster.py", line 1703, in cassandra.cluster.Cluster.connect
  File "cassandra\cluster.py", line 1690, in cassandra.cluster.Cluster.connect
  File "cassandra\cluster.py", line 3488, in cassandra.cluster.ControlConnection.connect
  File "cassandra\cluster.py", line 3533, in cassandra.cluster.ControlConnection._reconnect_internal
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'127.0.0.1:9042': ConnectionRefusedError(10061, "Tried connecting to [('127.0.0.1', 9042)]. Last error: no connection could be made because the target machine actively refused it

I have checked that the value of start_native_transport is true in cassandra.yaml.

For information, I'm using Python 3.9.2 and the docker container must be using Cassandra 3.11 .

I forgot to say it but the code used is the following one :

from cassandra.cluster import Cluster 

cassandra_cluster = Cluster() 
session = cassandra_cluster.connect()

Upvotes: 1

Views: 982

Answers (2)

Mike
Mike

Reputation: 531

As you pointed out, in order to do it you have to forward the port by adding -p 9042:9042. In order to access it from outside you will have to forward the port.

Regarding another option that does not require forwarding it, that depends on your use case. Avoiding forwarding a port is not a general rule, it just depends on the use case. Depending on who connects to the port you could avoid forwarding it if you use another container in the same network. But don't do that just as a proxy. Meaning instead of running your own queries from the host you first connect to another container that then connects to Cassandra. If you have like a web application that stores data in Cassandra, then sure, forward port 80 on the container for the application and then the application connects to Cassandra. That makes sense. Sometimes you could have external clients that will need to connect to the database so the port will need to be accessible.

The solution is to try to restrict using a firewall the IPs that can access the machine/port and also very important from a security point of view, activate authentication and authorization. SSL could help too.

Upvotes: 2

Lorybi
Lorybi

Reputation: 23

After working a bit on Cassandra documentation on Docker, I figured out I could just expose the port. However, I'm not sure whether it's a good way to handle this issue or not : according to the official web page of Cassandra on Dockerhub, I should have used a network to run another container to run my cql queries, but I couldn't figure out how to do so.

Upvotes: 1

Related Questions