-1

I have created a docker image of my web application. I have a cassandra database running on my laptop and I am starting the container of my web application from the same laptop. The uri to connect to database is localhost:9042. However, the image is not able to connect with the database. Do I need to do some network configuration for the container to connect to the database?

[trace] CassandraRepositoryComponents - database will connect using parameters uri: cassandra://localhost:9042/, cluster name: myCluster
[trace] s.d.c.CassandraConnectionUri - created logger Logger[services.db.cassandra.CassandraConnectionUri]
[trace] s.d.c.CassandraConnectionManagementService - creating session with uri CassandraConnectionUri(cassandra://localhost:9042/) and cluster name myCluster
[trace] s.d.c.CassandraConnectionManagementService - exception in connecting with database com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1:9042 (com.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1:9042] Cannot connect))
Oops, cannot start the server.

docker file

FROM openjdk:8
RUN mkdir deploy
WORKDIR deploy
COPY target/universal/myapp-1.0.zip .

COPY conf/logback_dev.xml ./logback.xml
COPY conf/application_dev.conf ./application.conf
RUN unzip myapp-1.0.zip
RUN chmod +x myapp-1.0/bin/myapp
EXPOSE 9000
ENTRYPOINT myapp-1.0/bin/myapp -Dplay.http.secret.key=changemeplease -Dlogger.file=/deploy/logback.xml -Dconfig.file=/deploy/application.conf

Cassandra is running as a standalone application

Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
  • Can you add `Dockerfile` of the app and how you are starting the application? and How casandara is running? Is it container also or stand-alone process on host? – nischay goyal May 31 '20 at 11:29
  • A wild guess would be to run the application using `docker run --network="host" IMAGENAME` and other options and should allow the web application to connect to `localhost:9042` – nischay goyal May 31 '20 at 11:31

3 Answers3

0

Run your docker container with the host network mode which enable that container to use the host network.

docker run --network="host" -p 9000:9000 IMAGENAME

and then access your application on http://localhost:9000 and shouldn't see any errors.

1 suggestion for Dockerfile, you don't have to mkdir if you are specifying the WORKDIR as it will create it for you.

FROM openjdk:8
WORKDIR deploy
COPY target/universal/myapp-1.0.zip .

COPY conf/logback_dev.xml ./logback.xml
COPY conf/application_dev.conf ./application.conf
RUN unzip myapp-1.0.zip
RUN chmod +x myapp-1.0/bin/myapp
EXPOSE 9000
ENTRYPOINT myapp-1.0/bin/myapp -Dplay.http.secret.key=changemeplease -Dlogger.file=/deploy/logback.xml -Dconfig.file=/deploy/application.conf
nischay goyal
  • 3,206
  • 12
  • 23
  • sorry. No love. Still getting error `exception in connecting with database com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/0:0:0:0:0:0:0:1:9042 (com.datastax.driver.core.exceptions.TransportException: [localhost/0:0:0:0:0:0:0:1:9042] Cannot connect), localhost/127.0.0.1:9042 (com.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1:9042] Cannot connect))` – Manu Chadha May 31 '20 at 12:25
  • but `cqlsh` works when I try to connect to `cassandra` from client – Manu Chadha May 31 '20 at 12:25
  • As you can see, it tries to connect `localhost/127.0.0.1:9042`, not sure why both strings are coming? – nischay goyal May 31 '20 at 20:48
  • looks like this is normal way of printing error - https://stackoverflow.com/questions/40239278/cannot-connect-to-cassandra-on-localhost – Manu Chadha Jun 01 '20 at 05:34
0

referring to From inside of a Docker container, how do I connect to the localhost of the machine?, I changed the uri string to connect to Cassandra to cassandra://host.docker.internal:9042/. And the application is up now!!!

Also, it seems that host.docker.internal maps to the ip address of main network interface. When I tried to run cqlsh (in another experiment of running cassandra as well via docker - Unable to start Cassandra docker image on Win10 Home), I noticed this.

C:\Users\manuc>cqlsh host.docker.internal 9042
Connection error: ('Unable to connect to any servers', {'192.168.1.12': error(10061, "Tried connecting to [('192.168.1.12', 9042)]. Last error: No connection could be made because the target machine actively refused it")})

Also, ping host.docker.internal

Pinging host.docker.internal [192.168.1.12] with 32 bytes of data:
Reply from 192.168.1.12: bytes=32 time<1ms TTL=128
Reply from 192.168.1.12: bytes=32 time<1ms TTL=128
Reply from 192.168.1.12: bytes=32 time<1ms TTL=128
Reply from 192.168.1.12: bytes=32 time<1ms TTL=128

This domain is configured when docker gets installed in etc/hosts in windows

# Added by Docker Desktop
192.168.1.12 host.docker.internal
192.168.1.12 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
0

You can not access port opened on the host machine using 127.0.0.1. You need to find the IP assigned to the network interface of your VM (in Mac and Linux you may use ifconfig command ). Access the Cassandra using the same IP from your docker container.

Also, check if Cassandra accepts connection from any ip. Setting this to 0.0.0.0 will listen on all network interfaces. Make these changes in the cassandra.yaml config file:

rpc_address: 0.0.0.0
Alok Singh
  • 488
  • 2
  • 6
  • I am on Windows and use Dockers for Windows. No VMs. – Manu Chadha Jun 02 '20 at 07:30
  • Did you check Cassandra config? Is it configured to listen to all IPs? Also, please find the IP assigned to docker machine network interface and use the same to connect to Cassandra server. – Alok Singh Jun 02 '20 at 07:50