Spring-Boot 应用程序中的 MongoSocketOpenException

问题描述 投票:0回答:1

我正在尝试在 Docker 上运行我的应用程序。问题是,当我运行

mongo
的图像时,它工作得很好。但是然后我运行 myapp 图像,它使用
mongo
作为数据库,它给出
com.mongodb.MongoSocketOpenException: Exception opening socket
。顺便说一句,我在同一网络下创建它们。

异常

注意:当我从本地运行 myapp 时,它运行良好。问题只是关于我的应用程序的图像。我不明白有什么区别?为什么会出现这个错误?

第一个命令,运行 mongo 镜像:

docker run --name=mongo --rm --network=mynet  -p 27017:27017 mongo

第二个命令,运行 myapp 镜像:

docker run --name=myapp --rm --network=mynet  -p 8088:8080 -e MONGO_URL=mongodb://mongo:27017 myapp

mynet网络检查:

"Name":"enote",
"Id":"95c6704778676fded0f856b2f905d60544bf67a83910b96193f00ef6a23730be",
"Created":"2023-08-21T07:59:34.479354749Z",
"Scope":"local",
"Driver":"bridge",
"EnableIPv6":false,
"IPAM":{
   "Driver":"default",
   "Options":{
      
   },
   "Config":[
      {
         "Subnet":"172.22.0.0/16",
         "Gateway":"172.22.0.1"
      }
   ]
},
"Internal":false,
"Attachable":false,
"Ingress":false,
"ConfigFrom":{
   "Network":""
},
"ConfigOnly":false,
"Containers":{
   "bf1bd274fcfdd61f93afc8be87d3542f1cefe81d62587a3d669b09b06cef2e84":{
      "Name":"enote-java",
      "EndpointID":"af36329326a5ce2c0ac3b24d1dc60c63d27a22e2df2b50c5469094bae21b8a05",
      "MacAddress":"02:42:ac:16:00:03",
      "IPv4Address":"172.22.0.3/16",
      "IPv6Address":""
   },
   "fa44ba0e009744610ec7f93ff7c04078692d853714193e2dc542cfe2ed815845":{
      "Name":"mongo",
      "EndpointID":"89be69458e5614976622731e89351f93cd406f8f1dfdc474480310039b8c4a74",
      "MacAddress":"02:42:ac:16:00:02",
      "IPv4Address":"172.22.0.2/16",
      "IPv6Address":""
   }
},
"Options":{
   
},
"Labels":{
   
}

myapp 的 Dockerfile:

FROM adoptopenjdk/openjdk11:jdk-11.0.2.9-slim
WORKDIR /opt
ENV PORT 8080
EXPOSE 8080
COPY target/*.jar /opt/app.jar
ENTRYPOINT exec java -jar app.jar
java mongodb spring-boot docker docker-networking
1个回答
0
投票

此配置解决了我的问题 -

docker-compose:

version: '3.1' services: mongodb: image: mongo container_name: "mongodb" ports: - '27017:27017' networks: - myapp myapp: image: myuser/myapp:1.0.0 environment: - MONGO_URL=mongodb://mongodb:27017/dev container_name: "myapp" ports: - "8080:8080" depends_on: - mongodb networks: - myapp networks: myapp: 

.属性:

spring.data.mongodb.uri=${MONGO_URL:mongodb://mongodb:27017/dev}

© www.soinside.com 2019 - 2024. All rights reserved.