com.mongodb.MongoSocketOpenException: Exception opening socket(MongoDB, Docker)
Asked Answered
A

4

12

I try to starting my application(Spring Boot + Spring Cloud + Eureka + MongoDB) with using docker image, but i can't get connection to MongoDB.

Exception:

exception "com.mongodb.MongoSocketOpenException: Exception opening socket."

I starting my application with execute command: docker-compose up --build

Docker log:

com.mongodb. MongoSocket0penException: Exception opening socket at com.mongodb. connection. SocketStream.open(SocketStream.java: 63) ~|mongo-java-driver-3.2.2.jar!/:naj
  at com. monsoob: connection. berettaberverwontortser/ersonito-kunnablearancerainitservertonitolnava:1203-dionso-java-driver*3?2.2.jarl/:nal
  at : java. lang. Thread.run (Thread. java: 745) Lna:1.8.0_111]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
  at java.net.PlainSocketImpl.socketConnect (Native Method) ~ [na: 1.8.0_1111 at java.net.AbstractPlainSocketImp1.doConnect (AbstractplainSocketImpl.java:350) ~ [na: 1.8.0_111]
  at java.net.AbstractPlainSocketImp1.connectToAddress(AbstractPlainSocketImp1.java:206)~[na:1.8.0_1111 
  at java.net .AbstractPlainSocketImp1.connect (AbstractPlain5ocket Imp],java:188) ~[na:1.8.0_111]
  at java.net.SocksSocketImpl.connect (SocksSocketImpl.java:392) ~|na:1.8.0_111] java.net Socket. connect (Socket, java: 589) ~[na:1.8:0-111]
  at com.mongodb.connection. SocketStreamHelper.initialize(SocketStreamHelper. java: 50) ~ [mongo-java-driver-3.2.2.ar!/:na] at com. mongodb. connection. SocketStream.open(SocketStream. java:58) ~ [mongo-java-driver-3.2.2.jar!/:na]
3 common frames omitted

application.yml:

# Spring properties
spring:
  application:
    name: car-service
  data:
    mongodb.host: localhost
    mongodb.port: 32769
    mongodb.uri: mongodb://localhost/test
    mongo.repositories.enabled: true


# Discovery Server Access
eureka:
    client:
      serviceUrl:
        defaultZone: http://localhost:8761/eureka/

# HTTP Server (Tomcat) Port
server: 
  port: 2220

error:
  whitelabel:
    enabled: false

docker-compose.yml:

eureka:
  build: ./eureka-discovery-service
  ports:
    - "8761:8761"

mongodb:
  image: mongo:3.0.4
  ports:
    - "32769:32769"

postgresql:
  image: postgres:9.6.1
  ports:
    - "32770:32770"

gateway-service:
  build: ./gateway-service
  ports:
    - "9090:9090"
  links:
    - eureka
  environment:
    SPRING_APPLICATION_NAME: gateway-service
    SPRING_PROFILES_ACTIVE: enableEureka
    EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
    EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/

airplane-service:
  build: ./airplane-service
  ports:
    - "2222:2222"
  links:
    - eureka
    - postgresql
  environment:
    SPRING_APPLICATION_NAME: airplane-service
    SPRING_PROFILES_ACTIVE: enableEureka
    EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
    EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
    SPRING_SLEUTH_ENABLED: "true"
    SPRING_DATASOURCE_POSTGRESQL_URL: jdbc:postgresql://localhost:32770/postgres

car-service:
  build: ./car-service
  ports:
    - "2220:2220"
  links:
    - eureka
    - mongodb
  environment:
    SPRING_APPLICATION_NAME: car-service
    SPRING_PROFILES_ACTIVE: enableEureka
    EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
    EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
    SPRING_SLEUTH_ENABLED: "true"
    SPRING_DATA_MONGODB_URI: mongodb://localhost:32769/test


machine-service:
  build: ./machine-service
  ports:
    - "2224:2224"
  links:
    - eureka
  environment:
    SPRING_APPLICATION_NAME: machine-service
    SPRING_PROFILES_ACTIVE: enableEureka
    EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
    EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
    SPRING_SLEUTH_ENABLED: "true"

Why I have exception opening a socket? How to fix this problem?

Arrears answered 17/8, 2017 at 18:56 Comment(0)
G
15

You are setting mongodb host in property file as localhost. In a container localhost address itself, but your mongodb is not in that container(car-service) which car-service run in. While you are using docker compose, you can address a container with its name. In your case it is mongodb.

Glyptograph answered 18/8, 2017 at 11:4 Comment(0)
C
10

to clarify @barbakini's answer, to define it in applications.yaml use:

spring.data.mongodb.host: mongodb
Coronel answered 5/7, 2019 at 15:46 Comment(2)
how does this answer differ from the previous Answer?Fragonard
its explicit, not implicit :)Coronel
C
0

Your mongoDB service is not UP ,

Check the status by following command

sudo service mongodb status

sudo service mongodb start

Hope it should work, there could be several reasons as well , like the configuration you defined in your application for mongodb service is incorrect such as port .

Camelot answered 13/7, 2020 at 12:56 Comment(3)
Might be Mongo is not yet installed.Groundnut
mongodb: unrecognized serviceHusbandman
Will never work because you are not searching inside your docker container, but on your host only, and mongodb could be installed (and different one) or not at all. That's why some people get "unrecognized service" or "connection refused" messages.Jordon
C
0

Try to add
network_mode: host
and remove
links: ....
for all services, that want to connect with mongo or eureka or postgresql, inside your docker-compose.yml.
By doing so you will connect to docker localhost.

Cartload answered 30/9, 2020 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.