Docker compose : Cassandra not connecting to my application in same network

164 Views Asked by At

My application is dependent on Cassandra DB, so i have put all of them in a docker-compose file, now simply using plain docker as given here i'm able to run it fine locally but when i try using the same configuration in compose i keep getting the same error,

my compose file:

version: "3.9"
services:
  myapplication:
    image: eclipse-temurin:17.0.7_7-jre
    working_dir: /root/app
    restart: on-failure
    command: sh -c "java -jar -Dspring.profiles.active=docker /root/app/app.jar"
    depends_on:
      - db
    volumes:
      - jar:/root/app/
      - ./logs:/root/app/logs
  db:
    image: cassandra:5.0
    restart: always
    container_name: cassandra
    environment:
      CASSANDRA_CLUSTER_NAME: app
    ports:
      - "9042:9042"
      - "9160:9160"
      - "7199:7199"
    volumes:
      - cas_data:/var/lib/cassandra
volumes:
  cas_data:
    driver: local
networks:
  default:
    name: my-application

and my property file is

spring.cassandra.schema-action=CREATE_IF_NOT_EXISTS
#spring.cassandra.request.timeout=10s
#spring.cassandra.connection.connect-timeout=10s
#spring.cassandra.connection.init-query-timeout=20s
#spring.cassandra.keyspace-name=app
#spring.data.cassandra.port=9042
spring.data.cassandra.contact-points=cassandra
spring.cassandra.local-datacenter=datacenter1

I have tried to even hard code the contact-point but to no avail, I'm able to access the cassandra in the compose container, but my application can't seem to access it.

error:

Suppressed: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: /127.0.0.1:9042
                Caused by: java.net.ConnectException: Connection refused
                        at java.base/sun.nio.ch.Net.pollConnect(Native Method)
                        at java.base/sun.nio.ch.Net.pollConnectNow(Unknown Source)
                        at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source)
                        at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:337)
                        at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
                        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776)
                        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
                        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
                        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
                        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
                        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
                        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
                        at java.base/java.lang.Thread.run(Unknown Source)
        Caused by: io.netty.channel.StacklessClosedChannelException: null
                at io.netty.channel.AbstractChannel$AbstractUnsafe.flush0()(Unknown Source)

what's the cause of this issue and how can i resolve this?


EDIT

on top of the previous stacktrace, i will add another one as it comes at the top of the above error

Caused by: com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 1 nodes, use getAllErrors() for more): Node(endPoint=/127.0.0.1:9042, hostId=null, hashCode=7b503e49): [com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s0|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io.netty.channel.StacklessClosedChannelException)]
        at com.datastax.oss.driver.api.core.AllNodesFailedException.copy(AllNodesFailedException.java:141) ~[java-driver-core-4.15.0.jar!/:na]

I'm giving correct contact point as i get this in the container

# nc -zv cassandra 9042
Connection to cassandra (192.xxx.xxx.x) 9042 port [tcp/*] succeeded!

0

There are 0 best solutions below