6

My web app can't connect to the MongoDB container

here are my application.yml

spring:
  data:
    mongodb:
      uri: mongodb://mongo:27017
      host: mongo
      port: 27017
      database: my-db-name

and this is my Docker-Compose

version: "3"
services:

  java:
    build:
      context: ./
    ports:
      - "8080:8080"
    links:
      - mongo
    depends_on:
      - mongo
    networks:
      - shared-net

  mongo:
    image: 'mongo'
    ports:
      - 27017:27017
    hostname: mongo
    volumes:
      - ./data/db:/data/db
    networks:
      - shared-net

networks:
  shared-net:
    driver: bridge

and this is the Dockerfile wrote for running java

FROM openjdk:11
COPY ./code/lemon-backend/target/lemon-0.0.1-SNAPSHOT.jar /usr/src/
WORKDIR /usr/src/
EXPOSE 8080
CMD ["java", "-jar", "lemon-0.0.1-SNAPSHOT.jar"]

I can't even build the application using these options I get this exception:

org.mongodb.driver.cluster: Exception in monitor thread while connecting to server mongo:27017

if possible try giving solutions with docker-compose, thanks

3
  • 1
    A common cause of this is the application starting up before the database is fully ready; while Compose will start the services in order it has know way of knowing when one is "ready". If you re-run docker-compose up again, does it work after a minute or so? Commented Dec 14, 2019 at 12:05
  • no, it wont.i even tried docker-compose up -d db to run the db only and tring to run the application later. Commented Dec 14, 2019 at 16:32
  • in adition to that i have the depends_on property which makes the db service come first Commented Dec 14, 2019 at 16:35

1 Answer 1

12

OLD VERSION ANSWER

IMPORTANT NOTE: older versions of MongoDB ignore this configuration in application.properties, proceed ahead & use the new solutions I added


This workaround is used for old versions of spring and mongo that ignore the normal configuration (other than uri)

. I had a warning that this property cant be resolved but hopefully, it worked :)

dockerspring.data.mongodb.uri= mongodb://<your_mongodb_container_name>:27017/<name_of_your_db>

the mongodb part is not changeable but mongo before the port number is actually the name of the container witch you have specified in your docker-compose


SPRING BOOT SOLUTION

spring:
  data:
    mongodb:
      host: <mongo-db-container-name>
      port: <mongo-db-port>
      database: <database-name>

DOCKER SOLUTION

In Your Dockerfile Add This Option For Executing Java

ENTRYPOINT [“java”,”-Dspring.data.mongodb.uri=mongodb://mongo:27017/name_of_your_db”, “-Djava.security.egd=file:/dev/./urandom”,”-jar”,”/<name_of_your_app>.jar”]

Linking Java And Mongo Containers + Giving Them Names

here this is my final docker-compose.yml, I hope that it helps you

version: "3"
services:

  java:
    build:
      context: ./
    ports:
      - "8080:8080"
    container_name: java
    links:
      - mongo
    depends_on:
      - mongo
    networks:
      - shared-net
 
 mongo:
    image: 'mongo'
    ports:
      - 27017:27017
    container_name: mongo
    volumes:
      - /home/sinoed/data/db:/data/db
    networks:
      - shared-net

networks:
  shared-net:
    driver: bridge

Compare this version and the one specified in the question carefully

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

2 Comments

Great! Thanks for sharing your solution! I had the same problem and fixed it by applying your suggestions. The only thing I would add to your solution is that the DB host (specified both in Dockerfile and in Java application url properties file) is not necessarily named "mongo"; if in your Docker compose, you named the container (container_name property) as mongo_whatever, then that is the host name you are going to have to use. Thanks for posting this.
Im glad that this was usefull. Yeah you are right i have used container_name in docker-compose.yml but i've forgot to mention it in the answer. Thanks for mentioning it ill edit the answer.

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.