Search code examples
dockercassandraplayframework-2.6

Can my docker image connect with a database running on same machine without network configuration?


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


Solution

  • 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