Home > Back-end >  docker image not connecting to mongodb
docker image not connecting to mongodb

Time:11-14

when I build my docker image and run it it gives this and error however if I run the jar file everything runs fine if I had it guess it would have to do with my docer-compose.yml file but idk

    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(Thread.java:833) ~[na:na]
Caused by: java.net.ConnectException: Connection refused
        at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
        at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
        at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
        at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
        at java.base/java.net.Socket.connect(Socket.java:633) ~[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

Dockerfile

FROM openjdk:17
ADD target/docker-document-rest-api.jar docker-document-rest-api.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "docker-document-rest-api.jar"]

application.properties

spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
spring.data.mongodb.database=example
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost

docer-compose.yml

version: "3.8"
services:
  mongodb:
    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
    container_name: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
      - ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
      - ME_CONFIG_MONGODB_SERVER=mongodb

volumes:
  data: {}

networks:
  default:
    name: mongodb_network

CodePudding user response:

By default docker containers are using bridge networking, so you cannot connect to localhost from a container. There can be several ways to solve this, one would be to connect your container, which is running the Java application, to the network where MongoDB is placed.

Since in your docker-compose you explicitly specify the name of the network (mongodb_network), we can do the following:

  1. In the properties file we have to change localhost to mongodb, so:
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
spring.data.mongodb.database=example
spring.data.mongodb.port=27017
spring.data.mongodb.host=mongodb

The reason for this is that name of the container can be used as hostname if we connect to the network in which the container is placed.

  1. Rebuild the docker image for the Java application, run it as such:
docker run --network=mongodb_network -p 8080:8080 <docker-image-name>

(fill in your docker image name there)

While these steps might work, I suggest to add the your application to the docker-compose:

version: "3.8"
services:
  mongodb:
    image: mongo
    container_name: mongodb
    expose:
      - 27017
    ports:
      - 27017:27017
    volumes:
      - data:/data
    environment:
      - MONGO_INITDB_ROOT_USERNAME=rootuser
      - MONGO_INITDB_ROOT_PASSWORD=rootpass
  mongo-express:
    image: mongo-express
    container_name: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
      - ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
      - ME_CONFIG_MONGODB_SERVER=mongodb
  spring_application:
    build: .
    container_name: spring_application
    ports:
      - 8080:8080
    depends_on:
      mongodb:
        condition: service_started

volumes:
  data: {}

networks:
  default:
    name: mongodb_network

This has to be build first with docker-compose build and afterwards can be executed with docker-compose up.

CodePudding user response:

You must use docker internal host network to connect with mongodb. So from your dockerized application you have to connect with the service name (as that is the hostname) instead of connecting with localhost. In your case: http://mongodb:27017 is the service (and thus hostname) of your mongo db container.

  • Related