1

I am new to docker and trying to connect a spring boot app with mongo db which is running on docker at port 27017. In my pom I have this:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

I have not specified anything in application.properties. Below are logs from spring boot failure:

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar:na]
    at java.base/java.lang.Thread.run(Thread.java:830) ~[na:na]
Caused by: java.net.ConnectException: Connection refused: no further information
    at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:579) ~[na:na]
    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:549) ~[na:na]
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:339) ~[na:na]
    at java.base/java.net.Socket.connect(Socket.java:603) ~[na:na]
    at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar:na]

And logs from mongodb(docker)

2020-05-30T00:31:55.387+0000 I  NETWORK  [listener] Listening on /tmp/mongodb-27017.sock
2020-05-30T00:31:55.388+0000 I  NETWORK  [listener] Listening on 0.0.0.0
2020-05-30T00:31:55.391+0000 I  NETWORK  [listener] waiting for connections on port 27017
2020-05-30T00:31:55.405+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: done building index _id_ on ns config.system.sessions
2020-05-30T00:31:55.425+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: starting on config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 } using method: Hybrid
2020-05-30T00:31:55.426+0000 I  INDEX    [LogicalSessionCacheRefresh] build may temporarily use up to 200 megabytes of RAM
2020-05-30T00:31:55.426+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: collection scan done. scanned 0 total records in 0 seconds
2020-05-30T00:31:55.429+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: inserted 0 keys from external sorter into index in 0 seconds
2020-05-30T00:31:55.431+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: done building index lsidTTLIndex on ns config.system.sessions
2020-05-30T00:31:55.434+0000 I  SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version: <unsharded>
2020-05-30T00:31:56.006+0000 I  SHARDING [ftdc] Marking collection local.oplog.rs as collection version: <unsharded>

Can someone please help me out?

Thanks!

5
  • How are you running the docker containers, could you specify the commands? typical error is you are not managing the networks as you should, for example you might trying to connect to localhost inside the container, thinking you are connecting to localhost as your machine and not the container. Commented May 30, 2020 at 2:12
  • Can you check the mentioned answers and see if it solves the problem and can be accepted as answer? Commented May 31, 2020 at 23:40
  • @nischay goyal I was able to resolve this issue by getting the Windows 2004 version upgrade and then installing Docker Desktop for Windows(after uninstalling the Docker Toolbox legacy one, which was required for previous windows versions). Not getting the error anymore. Commented Jun 2, 2020 at 0:26
  • That's great actually. shall i add this to answer and can you close it? Commented Jun 2, 2020 at 0:27
  • Sure, go ahead. Commented Jun 3, 2020 at 2:08

2 Answers 2

1

I was able to resolve this issue by getting the Windows 2004 version upgrade and then installing Docker Desktop for Windows(after uninstalling the Docker Toolbox legacy one, which was required for previous windows versions).

Sign up to request clarification or add additional context in comments.

Comments

1

The solution to above problem:-

By getting the Windows 2004 version upgrade and then installing Docker Desktop for Windows(after uninstalling the Docker Toolbox legacy one, which was required for previous windows versions).


I would suggest you to run your containers via docker-compose and check the below docker-compose.yml for reference.

version: "3"
services:
  api-database:
    image: mongo:3.2.4
    container_name: "api-database"
    ports:
      - 27017:27017
    command: --smallfiles
  api:
    image: XXXX
    ports:
      - XXXX:XXXX
    links:
      - api-database

Configure mongodb host name using spring config property using service name defined in docker-compose.

spring.data.mongodb.host=api-database

and then run

docker-compose up 

Ref:- https://nirajsonawane.github.io/2019/12/16/Spring-Boot-Mongodb-Docker-Compose/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.