title: Python Unable to Connect to Cassandra through Docker
body: <p>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:</p>
  
  <pre><code>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")})
  </code></pre>
  
  <p>Here is what I have setup:</p>
  
  <p>System:<br>
  Windows 8.1<br>
  Docker 1.9.1<br>
  Virtualbox 4.3.34<br>
  Python 3.5.1<br>
  Cassandra 3.0.2<br>
  Cassandra-Driver 3.0.0  </p>
  
  <p>Dockerfile:
  <a href="https://hub.docker.com/r/rc42/lab-cassandra/~/dockerfile/" rel="nofollow">https://hub.docker.com/r/rc42/lab-cassandra/~/dockerfile/</a></p>
  
  <p>Docker Container Port Mappings:  </p>
  
  <pre><code>7000/tcp -&gt; 0.0.0.0:7000  
  7001/tcp -&gt; 0.0.0.0:7001  
  7199/tcp -&gt; 0.0.0.0:7199  
  9042/tcp -&gt; 0.0.0.0:9042  
  9160/tcp -&gt; 0.0.0.0:9160
  </code></pre>
  
  <p>Docker-Machine IP  </p>
  
  <pre><code>192.168.99.101
  </code></pre>
  
  <p>cassandra.yaml (default setting)</p>
  
  <p>listen_address: localhost</p>
  
  <p>When I initialize cassandra in the container, I receive a normal result:</p>
  
  <pre><code>cassandra -f
  </code></pre>
  
  <p>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)</p>
  
  <p>But, when I try to connect to it using a python script, it refuses my connection:</p>
  
  <pre><code>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")})
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>listen_address: 192.168.99.101
  </code></pre>
  
  <p>nor the docker container IP address:</p>
  
  <pre><code>listen_address: 172.17.0.2
  </code></pre>
  
  <p>resolve the problem. Curiously though, even though I set the listen_address to 172.17.0.2, the stdout at startup is:  </p>
  
  <pre><code>INFO 19:42:19 Starting listening for CQL clients on localhost/127.0.0.1:9042 (unencrypted)...
  </code></pre>
  
  <p>So far, I have found a similar error occur for <a href="https://stackoverflow.com/questions/33824392/how-fix-error-in-install-and-connect-apache-cassandra-on-win/33843334">this poster</a> and a number of others are having their connection refused with a different <a href="https://stackoverflow.com/questions/33901933/connection-refused-error-with-cassandra-and-docker-py">error here</a> and <a href="http://grokbase.com/t/gg/docker-user/155m4qcth6/docker-docker-port-mapping" rel="nofollow">error here</a>. There is also a post that links the problem to potentially how <a href="https://stackoverflow.com/questions/30608548/unable-to-connect-to-cassandra-remotely-using-datastax-python-driver">cassandra-driver works with pycharm</a>. 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.</p>
  
  <p>Can anyone help me understand why python is refusing to connect to Cassandra?<br>
  And, what I can do to fix it?</p>