R J
R J

Reputation: 4563

Python Unable to Connect to Cassandra through Docker

I have been able to start Cassandra on the Windows localhost ports and connect to it using the cassandra-driver module. However, when I try to connect to Cassandra through a Docker container, I receive the following connection refused exception:

cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'192.168.99.101': ConnectionRefusedError(10061, "Tried connecting to [('192.168.99.101', 9042)]. Last error: No connection could be made because the target machine actively refused it")})

Here is what I have setup:

System:
Windows 8.1
Docker 1.9.1
Virtualbox 4.3.34
Python 3.5.1
Cassandra 3.0.2
Cassandra-Driver 3.0.0

Dockerfile: https://hub.docker.com/r/rc42/lab-cassandra/~/dockerfile/

Docker Container Port Mappings:

7000/tcp -> 0.0.0.0:7000  
7001/tcp -> 0.0.0.0:7001  
7199/tcp -> 0.0.0.0:7199  
9042/tcp -> 0.0.0.0:9042  
9160/tcp -> 0.0.0.0:9160

Docker-Machine IP

192.168.99.101

cassandra.yaml (default setting)

listen_address: localhost

When I initialize cassandra in the container, I receive a normal result:

cassandra -f

INFO 22:38:18 Starting listening for CQL clients on localhost/127.0.0.1:9042 INFO 22:38:18 Not starting RPC server as requested. Use JMX (StorageService ->startRPCServer()) or nodetool (enablethrift to start it)

But, when I try to connect to it using a python script, it refuses my connection:

from cassandra.cluster import Cluster
cluster = Cluster(['192.168.99.101'])
session = cluster.connect()

cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'192.168.99.101': ConnectionRefusedError(10061, "Tried connecting to [('192.168.99.101', 9042)]. Last error: No connection could be made because the target machine actively refused it")})

Based upon suggestions in other posts, I have tried to adjust the listen_address in cassandra.yaml, but doing so has not been able to resolve the problem. Neither the docker-machine IP address:

listen_address: 192.168.99.101

nor the docker container IP address:

listen_address: 172.17.0.2

resolve the problem. Curiously though, even though I set the listen_address to 172.17.0.2, the stdout at startup is:

INFO 19:42:19 Starting listening for CQL clients on localhost/127.0.0.1:9042 (unencrypted)...

So far, I have found a similar error occur for this poster and a number of others are having their connection refused with a different error here and error here. There is also a post that links the problem to potentially how cassandra-driver works with pycharm. But so far all my attempts to solve this configuration problem have failed and the problem occurs for me even when I run python outside pycharm. I have other containers running on different ports on the same docker machine and clients do not have any problem connecting to the containers, just cassandra.

Can anyone help me understand why python is refusing to connect to Cassandra?
And, what I can do to fix it?

Upvotes: 2

Views: 3440

Answers (1)

R J
R J

Reputation: 4563

After discovering a thread on grokbase that dealt with problems getting Cassandra to listen to port 9042 on an interface rather than the localhost address, I was able to find a configuration setting that works.

find private ip of container:

CONTAINER_IP=`docker inspect -f '{{ .NetworkSettings.IPAddress }}' container`

edit cassandra.yaml file:

seed: "CONTAINER_IP"
listen_address: CONTAINER_IP
broadcast_address: CONTAINER_IP
start_rpc: true
rpc_address: 0.0.0.0
broadcast_address: CONTAINER_IP

Although this works for a single-node running in a Docker container in VirtualBox, this configuration is unlikely to work either when deployed to ec2 or when linking to other nodes in a cluster.

Upvotes: 4

Related Questions