SeekanDestroy
SeekanDestroy

Reputation: 611

Connectivity issue between containers (I suppose...)

I'm using docker-compose to run 3 containers:

  1. My webapplication
  2. Postgres
  3. Cassandra

Once I use: docker-compose up

My webapp launches this exception:

com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: cassandra/172.17.0.3:9042

Once all containers are running, I'm able to enter into my webapps and try to ping cassandras container before it dies (webapp container), and all packets are successfully returned so I guess there actually IS connectivity between them.

The weirdest thing is that once I got this exception:

.InvalidQueryException: Keyspace 'myKeyspace' does not exist

Which means connection has been stablished, but was before I add persistence and created the mentioned schema, but I did change nothing on my compose.yml to get this new result

Here is my docker-compose.yml:

version: '3.1' 

services:


cassandra:
    container_name: "cassandra"
    image: cassandra
    ports:
        - 9042:9042
    
    
    volumes:
      - /home/cassandra:/var/lib/cassandra

     
postgresql:
 
    container_name: "postgresql"
    image: postgres:11.1-alpine
    restart: always
    environment:
        POSTGRES_DB: mywebapp
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres
    volumes:
       #- ./startup.sql:/docker-entrypoint-initdb.d/startup.sql 
       - postgresdata:/var/lib/postgresql/data
      
    ports:
        - 5432:5432
    
    

mywebapp:
    container_name: "mywebapp"
    image: openjdk:10-jre-slim
    hostname: mywebapp
    volumes:
        - ./lib:/home/lib
        - ./mywebapp-1.0.1-SNAPSHOT-exec.jar:/home/mywebapp-1.0.1-SNAPSHOT-exec.jar
        
       
    entrypoint:     
        - java
        - -jar
        - -Djava.library.path=/home/lib
        - /home/mywebapp-1.0.1-SNAPSHOT-exec.jar
    
    environment:
        - LD_LIBRARY_PATH=/home/lib
        - spring.datasource.url=jdbc:postgresql://postgresql:5432/mywebapp
        - spring.cassandra.contactpoints=cassandra
        - spring.cassandra.port=9042
        - spring.cassandra.keyspace=mywebapp
        #- spring.datasource.username=postgres
        #- spring.datasource.password=postgres
        #- spring.jpa.hibernate.ddlAuto=update+
       
        
        
    ports:
        - 8443:8443
        - 8080:8080
    depends_on:
        - cassandra
            

volumes: postgresdata:

Thank you all in advance

Upvotes: 1

Views: 120

Answers (1)

Kārlis Ābele
Kārlis Ābele

Reputation: 1021

I am assuming your web app requires for the cassandra service to be running when it starts. You should add depends_on entry to your web app service so docker starts it only when cassandra is started

And the links entry is not necessary as docker automatically will use the service names as hostnames in the network created for this docker-compose project. Same goes for the network_type: bridge - that is the default network type, so you can omit that in your case.

Upvotes: 1

Related Questions