Docker Community Forums

Share and learn in the Docker community.

Docker port binding conflict on Cassandra incoming port 9042 and Spring Boot outgoing port 9042

docker

(Joethecoder2) #1

Problem Statement:
The problem is that after setting up a local Cassandra instance that runs with Docker, I test that the CQLSH interpreter runs standalone with Docker. That test passes successfully. I later test with SpringBoot and Datastax driver inside a Docker container, and am getting a port binding error on port 9042, and a Transport Exception, can someone help by telling me how to fix this error? This is needed, because I want to be able to run my application in Docker, and then later Kubernetes Pods. Without this fix, to this error, I can not run spring boot in a Docker container, and use with Docker Swarm, or Kubernetes for application Scaling, and must use AWS Application scaler instead.

Then, a Spring Boot application, that invokes a CQL command using the Datastax driver is executed, and an Exception is thrown, due to NoHostAvailableException, followed by a TransportException.
The spring boot application runs perfectly fine, without a docker container and was tested with a standalone jar file:
java -jar ./target/spring-boot-web-0.0.1-SNAPSHOT.jar -Dcassandra_ip=127.0.0.1 -Dcassandra_port=9042

How to Setup the Problem:

Cassandra is setup as follows:
docker run -d --name main-cassandra -v main-cassandra-db:/var/lib/cassandra -p 127.0.0.1:9160:9160 -p 127.0.0.1:9042:9042 -m 1G cassandra:3.11.1

docker exec -it main-cassandra cqlsh 127.0.0.1 9042
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.11.1 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> exit
The spring boot application is then executed similarly to the java jar file:
docker run --name spring-boot-web -p 8080:8080 -p 9042:9042 -d docker.io/joethecoder2/spring-boot-web

These exceptions are thrown:
docker: Error response from daemon: driver failed programming external connectivity on endpoint spring-boot-web-start (64ae462a44517d57ba85a2bef03630c355a1cded4ec8d9453b20c07b0bda0b38): Error starting userland proxy: listen tcp 0.0.0.0:9042: bind: address already in use.

The spring boot application is then re-run without the conflicting 9042 ports that are already bonded to, address in use:
docker run --name spring-boot-web -p 8080:8080 -d joethecoder2/spring-boot-web

Once the spring boot application loads:
docker logs spring-boot-web

Then execute from command line:
curl -X POST --header ‘Content-Type: application/json’ --header ‘Accept: text/plain’ ‘http://localhost:8080/restaurant/setupRestaurantDatabaseTables’

See application logs again with docker:
docker logs spring-boot-web

The problem manifestation:

Caused by: 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))

Conclusions, and help needed by Docker experts:

Please help by telling me how to map to port 9042 in Docker, after the Cassandra ports are already bound to with 9042. I need the spring-boot-web to have outgoing ports for 9042 in order to communicate with Cassandra already running in docker.