0

I'm trying to use docker-compose to run my Springboot REST API, which uses Mongo as a database, but I'm receiving the following error. Could someone please assist me in resolving this?

When my spring-boot application tries to connect to MongoDB via the Docker container, it gives me problems. Spring-boot appears to be unable to connect to MongoDB on port 27017.

Below is the part of the code and exception.

DockerFile

FROM adoptopenjdk/openjdk11:alpine-jre
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]

Docker-Compose.yml


version: '3.8'

services:

      sprintpoker-springboot-container:
            image: sprintpoker-springboot
            build:
                  context: ./
                  dockerfile: Dockerfile
            volumes:
                  - /data/sprintpoker-springboot-container
            ports:
                  - 8080:8080
            depends_on:
                  - mongo

      mongo:
            image: mongo
            container_name: mongodb
            ports:
                  - 27017:27017
            volumes:
                  - data:/data
            environment:
                  MONGO_INITDB_ROOT_USERNAME: rootuser
                  MONGO_INITDB_ROOT_PASSWORD: rootpass

      mongo-express:
            image: mongo-express
            restart: always
            ports:
                  - 8081:8081
            environment:
                  ME_CONFIG_MONGODB_ADMINUSERNAME: rootuser
                  ME_CONFIG_MONGODB_ADMINPASSWORD: rootpass
                  ME_CONFIG_MONGODB_SERVER: mongo
volumes:
      data: {}
networks:
      default:
            name: mongodb_network

Command I am use to run docker:

docker-compose up -d --build --force-recreate --renew-anon-volumes

Exception on spring-boot docker container:

2021-08-16 03:16:02.453 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}

2021-08-16 03:16:02.545 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017


com.mongodb.MongoSocketOpenException: Exception opening socket

at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar!/:na]

at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.2.3.jar!/:na]

at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.2.3.jar!/:na]

at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.2.3.jar!/:na]

at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]

Caused by: java.net.ConnectException: Connection refused (Connection refused)

at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]

at java.base/java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:na]

at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:na]

at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:na]

at java.base/java.net.SocksSocketImpl.connect(Unknown Source) ~[na:na]

at java.base/java.net.Socket.connect(Unknown Source) ~[na:na]

at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.2.3.jar!/:na]

at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.2.3.jar!/:na]

at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.2.3.jar!/:na]

... 4 common frames omitted

application.properties

spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
spring.data.mongodb.database=sprint
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
spring.data.mongodb.auto-index-creation=true

Thanks!

7
  • 1
    Can you also share the spring boot snippet, where you configured spring boot to connect to mongodb ? Commented Aug 16, 2021 at 3:35
  • 2
    According to the error, spring is using localhost:27017, therefore trying to on connect to itself, not mongo:27017, the other container Commented Aug 16, 2021 at 3:41
  • @RafafTahsin I am using JPA, so did you want me to share application.properties file? Commented Aug 16, 2021 at 4:55
  • @OneCricketeer I didn't understand, which line you talking about. Commented Aug 16, 2021 at 4:56
  • 1
    He means 2021-08-16 03:16:02.545 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017. The address should be mongo:27017. Commented Aug 16, 2021 at 6:09

3 Answers 3

1

I am able to resolve the issue Thanks to @OneCricketeer and @Michał Krzywański (Mongodb connection error though docker in springboot).

Solution: Added below line in docker-compose file:

environment:
      - SPRING_DATA_MONGODB_.HOST=mongo

 expose:
                  - 27017

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

Comments

0

Your error is here

2021-08-16 03:16:02.545 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017

The Spring app container isn't running anything on port 27017, let alone a MongoDB server.

Change

spring.data.mongodb.host=localhost

to

spring.data.mongodb.host=mongo

2 Comments

I tried this to do, still getting same error: ``` 2021-08-18 04:51:33.254 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'} 2021-08-18 04:51:33.366 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017 com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar!/:na] ```
0
  1. Application.yaml / properties file
spring:
        data:
          mongodb:
            database: test
            host: mymongodb
            port: 27017
        spring.data.mongodb.port=27017
        spring.data.mongodb.host=<mongo-image-name>
        spring.data.mongodb.database=<any-database-name>
  1. Create mongo image :-

docker pull mongo:latest

  1. Stop Local Mongo Service as it runs on the same port

  2. Run docker image :-

docker run -d -p 27017:27017 --name mymongodb mongo:latest --mymongodb is the container name and will be the host name in yaml or properties file

  1. DockerFile Example :-
FROM openjdk:11
      ADD target/springboot-mongo-docker.jar app.jar
      EXPOSE 8080
      ENTRYPOINT ["java","-jar","app.jar"]
  1. Build the jar :- mvn clean install

  2. Build Docker Image :- docker build -t springboot-mongodb:1.0 .

  3. Run The Linked Container as follows :-

docker run -p 8080:8080 --name springboot-mongodb --link mymongodb:mongo -d springboot-mongodb:1.0

  1. docker-compose.yml :-

docker-compose up

> version: "3"      
     services:       mymongodb:
>          image: mongo:latest
>          container_name: "mymongodb"
>          ports:
>            - 27017:27017       springboot-mongodb:
>          image: springboot-mongodb:1.0
>          container_name: springboot-mongodb
>          ports:
>            - 8080:8080
>          links:
>            - mymongodb

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.