Docker容器未连接mongodb主机

时间:2019-01-29 13:55:52

标签: mongodb docker docker-compose dockerfile docker-networking

我正从docker容器中遇到MongoDB连接问题,我读了一些stackoverflow答案,但没有帮助我解决问题。

我的application.yml文件具有以下内容,

spring:
   application:
    name: web-ui-service
   data:
     mongodb:
       uri: mongodb://<username>:<pwd>@172.17.0.1:27017/database

并且我的docker-compose文件具有以下内容,

版本:“ 3.5”     服务:

sp-app-web-ui:
  container_name: sp-app-web-ui
  image: swimming-pool/web-ui-service
    environment:
    - DOCKER_HOST_IP=mongodb://172.17.0.1:27017
     - SPRING_DATA_MONGODB_DATABASE=swimming-pool
   ports:
     - 8080:8080
   networks:
     - swimming-pool-app
       restart: unless-stopped

     networks:
       swimming-pool-app:
     default:
       driver: bridge

我读了此博客From inside of a Docker container, how do I connect to the localhost of the machine?

但仍然面临以下问题,

sp-app-web-ui | com.mongodb.MongoSocketOpenException:异常打开套接字 sp-app-web-ui |在com.mongodb.connection.SocketStream.open(SocketStream.java:63)〜[mongodb-driver-core-3.4.2.jar!/:na] sp-app-web-ui |在com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)〜[mongodb-driver-core-3.4.2.jar!/:na] sp-app-web-ui |在com.mongodb.connection.DefaultServerMonitor $ ServerMonitorRunnable.run(DefaultServerMonitor.java:113)〜[mongodb-driver-core-3.4.2.jar!/:na] sp-app-web-ui |在java.lang.Thread.run(Thread.java:745)[na:1.8.0_111] sp-app-web-ui |引起原因:java.net.ConnectException:连接被拒绝(连接被拒绝) sp-app-web-ui |在java.net.PlainSocketImpl.socketConnect(本机方法)〜[na:1.8.0_111] sp-app-web-ui |在java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)〜[na:1.8.0_111] sp-app-web-ui |在java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)〜[na:1.8.0_111] sp-app-web-ui |在java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)〜[na:1.8.0_111] sp-app-web-ui |在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)〜[na:1.8.0_111] sp-app-web-ui |在java.net.Socket.connect(Socket.java:589)〜[na:1.8.0_111] sp-app-web-ui |在com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57)〜[mongodb-driver-core-3.4.2.jar!/:na] sp-app-web-ui |在com.mongodb.connection.SocketStream.open(SocketStream.java:58)〜[mongodb-driver-core-3.4.2.jar!/:na]

我正在使用Debian和Docker 18.06。如果有人帮助我,那就太好了。预先感谢!

2 个答案:

答案 0 :(得分:1)

我已经解决了这个问题,我的docker-compose文件如下所示,

preg_match('/data:image\/(\w+);base64,(.+)/', $src, $match);

并在bindIp变量的mongo.conf文件中添加“ 127.0.0.1”。

我的application.yml具有以下内容,

<img src="data:image/gif;base64,--base64-coded-data---">

在Linux上ping ip a时,此IP 127.0.0.1是从docker0获得的。

答案 1 :(得分:0)

您可能正在寻找的是将network_mode: "host"添加到您的docker compose服务中:

sp-app-web-ui:
  container_name: sp-app-web-ui
  image: swimming-pool/web-ui-service
    environment:
    - DOCKER_HOST_IP=mongodb://172.17.0.1:27017
     - SPRING_DATA_MONGODB_DATABASE=swimming-pool
   ports:
     - 8080:8080

   # https://docs.docker.com/compose/compose-file/#network_mode
   network_mode: "host"

您可以在docs

中了解更多信息。