Spring Boot tries to connect to Mongo localhost

8/23/2018

I have a Spring Boot 2.x project using Mongo. I am running this via Docker (using compose locally) and Kubernetes. I am trying to connect my service to a Mongo server. This is confusing to me, but for development I am using a local instance of Mongo, but deployed in GCP I have named mongo services.

here is my application.properties file:

#mongodb
spring.data.mongodb.uri= mongodb://mongo-serviceone:27017/serviceone

#logging
logging.level.org.springframework.data=trace
logging.level.=trace

And my Docker-compose:

version: '3'

# Define the services/containers to be run
services:
  service: #name of your service
    build: ./ # specify the directory of the Dockerfile
    ports:
      - "3009:3009" #specify ports forwarding
    links:
      - mongo-serviceone # link this service to the database service
    volumes:
      - .:/usr/src/app
    depends_on:
      - mongo-serviceone

  mongo-serviceone: # name of the service
    image: mongo
    volumes:
        - ./data:/data/db
    ports:
        - "27017:27017"

When I try docker-compose up . I get the following error:

mongo-serviceone_1 | 2018-08-22T13:50:33.454+0000 I NETWORK [initandlisten] waiting for connections on port 27017 service_1 | 2018-08-22 13:50:33.526 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017 service_1 | service_1 | com.mongodb.MongoSocketOpenException: Exception opening socket service_1 | at com.mongodb.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.6.3.jar!/:na]

running docker ps shows me:

692ebb72cf30        serviceone_service        "java -Djava.securit…"   About an hour ago   Up 9 minutes        0.0.0.0:3009->3009/tcp, 8080/tcp   serviceone_service_1
6cd55ae7bb77        mongo                      "docker-entrypoint.s…"   About an hour ago   Up 9 minutes        0.0.0.0:27017->27017/tcp           serviceone_mongo-serviceone_1

While I am trying to connect to a local mongo, I thought that by using the name "mongo-serviceone"

-- sonoerin
docker
docker-compose
kubernetes

2 Answers

9/18/2018

Hard to tell what the exact issue is, but maybe this is just an issue because of the space " " after "spring.data.mongodb.uri=" and before "mongodb://mongo-serviceone:27017/serviceone"?

If not, maybe exec into the "service" container and try to ping the mongodb with: ping mongo-serviceone:27017

Let me know the output of this, so I can help you analyze and fix this issue.

Alternatively, you could switch from using docker compose to a Kubernetes native dev tool, as you are planning to run your application on Kubernetes anyways. Here is a list of possible tools:

Allow hot reloading:

Pure CI/CD tools for dev:

For most of them, you will only need minikube or a dev namespace inside your existing cluster on GCP.

-- LukasGentele
Source: StackOverflow

8/23/2018

Looks like another application was running on port 27017 on your localhost Similar reported issue

quick way to check on linux/mac:

telnet 127.0.01 27017

check logs files:

docker logs serviceone_service

-- Ryan Z
Source: StackOverflow