title: Connecting to a Cassandra Docker Container Using Cassandra Spring Data
body: <p>So I am running in to issues connecting to a single node cassandra cluster using spring data-cassandra. I am using the docker image found at: <a href="https://hub.docker.com/_/cassandra/" rel="nofollow">https://hub.docker.com/_/cassandra/</a></p>
  
  <p>using a docker-compose with the following environment variables set:</p>
  
  <pre><code>cassandra_n1:
    image: cassandra:latest
    ports:
      - "9042:9042"
      - "9160:9160"
    hostname: cassandra_n1
    environment:
      CASSANDRA_CLUSTER_NAME: "mycluster"
      CASSANDRA_ENDPOINT_SNITCH: "PropertyFileSnitch"
      CASSANDRA_DC: "DC1"
      CASSANDRA_RACK: "R1"
  </code></pre>
  
  <p>then after this starts I try to connect to it using my spring boot application which is as simple as:</p>
  
  <pre><code>import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  
      @SpringBootApplication
      public class MvcApplication {
  
          public static void main(String[] args) {
              SpringApplication.run(MvcApplication.class);
          }
      }
  </code></pre>
  
  <p>which scans for a configuration file which is:</p>
  
  <pre><code>@Configuration
  @PropertySource(value = { "classpath:cassandra.properties" })
  @EnableCassandraRepositories(basePackages = { "myproject.repository" })
  public class CassandraConfig {
  
      private static final Logger LOG = LoggerFactory.getLogger(CassandraConfig.class);
  
      @Autowired
      private Environment env;
  
      @Bean
      public CassandraClusterFactoryBean cluster() {
  
          CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
          cluster.setContactPoints(env.getProperty("cassandra.contactpoints"));
          cluster.setPort(Integer.parseInt(env.getProperty("cassandra.port")));
  
          return cluster;
      }
  
      @Bean
      public CassandraMappingContext mappingContext() {
          return new BasicCassandraMappingContext();
      }
  
      @Bean
      public CassandraConverter converter() {
          return new MappingCassandraConverter(mappingContext());
      }
  
      @Bean
      public CassandraSessionFactoryBean session() throws Exception {
  
          CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
          session.setCluster(cluster().getObject());
          session.setKeyspaceName(env.getProperty("cassandra.keyspace"));
          session.setConverter(converter());
          session.setSchemaAction(SchemaAction.NONE);
  
          return session;
      }
  
      @Bean
      public CassandraOperations cassandraTemplate() throws Exception {
          return new CassandraTemplate(session().getObject());
      }
  }
  </code></pre>
  
  <p>that looks at the property file cassandra.properties which is:</p>
  
  <pre><code>cassandra.contactpoints=192.168.99.100
  cassandra.port=9042
  cassandra.keyspace=mykeyspace
  </code></pre>
  
  <p>I am using docker-machine as a docker daemon which has a address of 192.168.99.100</p>
  
  <p>in my pom i'm using the dependencies: </p>
  
  <pre><code>         &lt;dependency&gt;
                  &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-data-cassandra&lt;/artifactId&gt;
                  &lt;version&gt;1.0.0.RELEASE&lt;/version&gt;
                  &lt;exclusions&gt;
                      &lt;exclusion&gt;
                          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                          &lt;artifactId&gt;spring-expression&lt;/artifactId&gt;
                      &lt;/exclusion&gt;
                  &lt;/exclusions&gt;
              &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-expression&lt;/artifactId&gt;
              &lt;version&gt;4.1.7.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>After i build and run my application, the application fails to connect to cassandra showing a message:</p>
  
  <pre><code> Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException:
   All host(s) tried for query failed (tried: /192.168.99.100:9042 (com.datastax.driver.core.ConnectionException: 
  [/192.168.99.100:9042] Unexpected error during transport initialization (com.datastax.driver.core.TransportException: 
  [/192.168.99.100:9042] Unexpected exception triggered (java.lang.IndexOutOfBoundsException: 
  Not enough readable bytes - Need 4, maximum is 0))))
  </code></pre>
  
  <p>I have tried to set the listen_address, broadcast_address and the rpc_address to the docker daemon ip but have not had success.
  Any help would be appriciated</p>