title: Cassandra Container DB NoNodeAvailableException
body: <p>I use Cassandra docker image, spring boot services running on docker container, and use Datastax  Java driver.</p>
  <p>I can connect connect Cassandra and also see logs, with the below docker compose file,</p>
  <pre><code>version: '3.5'
  
  networks:
    my-network:
  
   cassandra-db:
      container_name: cassandra-db
      image: cassandra:4.0-beta1
      ports:
        - &quot;9042:9042&quot;
      restart: on-failure
      environment:
        - CASSANDRA_CLUSTER_NAME='cassandra-cluster'
        - CASSANDRA_NUM_TOKENS=256
        - CASSANDRA_RPC_ADDRESS=0.0.0.0
        - CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch
      volumes:
        - ./out/cassandra_data:/var/lib/cassandra
      networks:
        - my-network
  
  
   client-service:
      container_name: client-service
      image: data/client-service
      environment:
        - SPRING_PROFILES_ACTIVE=dev
      ports:
        - 8087:8087
      depends_on:
        - cassandra-db
      links:
        - cassandra-db
      networks:
        - my-network
  </code></pre>
  <p>My keyspace query is,</p>
  <pre><code>CREATE KEYSPACE IF NOT EXISTS SensorKeySpace WITH replication = {'class':'NetworkTopologyStrategy','replication_factor':3}
  </code></pre>
  <p>and building <code>Cqlsession</code> using,</p>
  <pre><code>.addContactEndPoint(new DefaultEndPoint(
                          InetSocketAddress.createUnresolved(&quot;cassandra-db&quot;,9042)))
                  .withLocalDatacenter(&quot;DC1&quot;)
                  .withKeyspace(CassandraConstant.KEY_SPACE_NAME.getValue())
                  .build();
  </code></pre>
  <p>The Cassandra is up and running, and verified it,</p>
  <pre><code>CONTAINER ID        IMAGE         COMMAND                  CREATED             STATUS              PORTS                                  NAMES
  57d1d29dbf00  client-service      &quot;java -Djava.securit…&quot;   4 minutes ago       Up 4 minutes   0.0.0.0:8087-&gt;8087/tcp                      client-service
  00548a4058e6  cassandra:4.0-beta1  &quot;docker-entrypoint.s…&quot;   4 minutes ago  Up 4 minutes   7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9042-&gt;9042/tcp   cassandra-db
  </code></pre>
  <p>The problem was in the beginning,</p>
  <ol>
  <li>I used <code>SimpleStrategy</code> to create Keyspace and got the below error,</li>
  </ol>
  <pre><code>Caused by: [com.datastax.oss.driver.api.core.CqlSession]: Factory method 'session' threw exception; nested exception is java.lang.IllegalStateException: Since you provided explicit contact points, the local DC must be explicitly set (see basic.load-balancing-policy.local-datacenter in the config, or set it programmatically with SessionBuilder.withLocalDatacenter). Current contact points are: Node(endPoint=cassandra-db:9042, hostId=729bd04f-b345-4df3-a148-80545d30e032, hashCode=5563e4c4)=datacenter1. Current DCs in this cluster are: datacenter1
  </code></pre>
  <p>Then configured <code>.withLocalDatacenter(&quot;DC1&quot;)</code> and <code>- CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch</code> in docker compose.</p>
  <ol start="2">
  <li>After configuring, NO ERROR during startup the container but when i hit the query, i got the below error,</li>
  </ol>
  <pre><code>The Select Query is [SELECT NAME,VALUE FROM READING ]
  client-service       | 2020-09-08 11:12:38.480 ERROR 1 --- [nio-8087-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.util.concurrent.ExecutionException: org.springframework.data.cassandra.CassandraConnectionFailureException: Query; CQL [com.datastax.oss.driver.internal.core.cql.DefaultSimpleStatement@1265401a]; No node was available to execute the query; nested exception is com.datastax.oss.driver.api.core.NoNodeAvailableException: No node was available to execute the query] with root cause
  client-service       | com.datastax.oss.driver.api.core.NoNodeAvailableException: No node was available to execute the query
  </code></pre>
  <p>I read the API doc, <a href="https://docs.datastax.com/en/drivers/java/4.0/com/datastax/oss/driver/api/core/NoNodeAvailableException.html" rel="nofollow noreferrer">https://docs.datastax.com/en/drivers/java/4.0/com/datastax/oss/driver/api/core/NoNodeAvailableException.html</a> which tell me</p>
  <pre><code>This can happen if all nodes are down, or if all the contact points provided at startup were invalid.
  </code></pre>
  <p>My Cassandra DB is up(verified), and connection is success to it. Anything is missing here?</p>