具有docker-compose的mongodb [SocketTimeoutException]

时间:2017-05-23 07:43:17

标签: mongodb docker docker-compose

我有一个运行spring-boot的应用程序来访问mongodb

当我使用单独的docker run命令时,应用程序可以正常工作。但是,当我尝试将所有这些服务作为服务运行时,由于某种原因,应用程序永远无法连接到mongo。

我尝试过:

  • 首先撰写撰写文件[下面列出]
  • 分别运行mongo并为mongo [still。提供external_links 失败]
  • 在撰写文件中,还提到了depends_on [仍然失败]
  • 覆盖撰写文件中的主机名变量[失败]

搬运工-compose.yaml

version: '2'
services:
 mongo:
  image: mongo:latest
  container_name: mongo
  ports:
   - "27017:27017"

 hygieia-api:
  image: hygieia-api:latest
  container_name: hygieia-api
  ports:
   - "8080:8080"
  volumes:
   - ./logs:/hygieia/logs
  links:
   - mongo:mongo
  environment:
    - JASYPT_ENCRYPTOR_PASSWORD=hygieiasecret
  depends_on:
   - mongo

 hygieia-ui:
  image: hygieia-ui:LATEST
  container_name: hygieia-ui
  ports:
   - "8088:80"
  links:
   - hygieia-api

撰写具有外部依赖关系的文件:

mongo以docker run --name mongo -p 27017:27017 -d mongo

开头
version: '2'
services:

 hygieia-api:
  image: hygieia-api:latest
  container_name: hygieia-api
  ports:
   - "8080:8080"
  volumes:
   - ./logs:/hygieia/logs
  external_links:
   - mongo:mongo
  environment:
    - JASYPT_ENCRYPTOR_PASSWORD=hygieiasecret
    - SPRING_DATA_MONGODB_HOST=mongo
  depends_on:
   - mongo

 hygieia-ui:
  image: hygieia-ui:LATEST
  container_name: hygieia-ui
  ports:
   - "8088:80"
  links:
   - hygieia-api

在上面列出的任何情况下,我得到的错误是:

hygieia-api    | 2017-05-22 19:20:43,918 INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server 172.17.0.2:27017 hygieia-api    | com.mongodb.MongoSocketOpenException: Exception opening socket hygieia-api    |    at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongo-java-driver-3.0.2.jar!/:na] hygieia-api    |  at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114) ~[mongo-java-driver-3.0.2.jar!/:na] hygieia-api    |     at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127) ~[mongo-java-driver-3.0.2.jar!/:na] hygieia-api    |    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111] hygieia-api    | Caused by: java.net.SocketTimeoutException: connect timed out hygieia-api    |     at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_111] hygieia-api    |   at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_111] hygieia-api    |    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_111] hygieia-api    |     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_111] hygieia-api    |  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_111] hygieia-api    |  at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_111] hygieia-api   |     at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50) ~[mongo-java-driver-3.0.2.jar!/:na] hygieia-api    |    at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongo-java-driver-3.0.2.jar!/:na] hygieia-api    |  ... 3 common frames omitted hygieia-api    | 2017-05-22 19:20:55,118 INFO  o.a.catalina.core.StandardService - Stopping service Tomcat hygieia-api    | 2017-05-22 19:20:55,127 WARN  o.a.c.loader.WebappClassLoaderBase - The web application [api] appears to have started a thread named [cluster-ClusterId{value='592339f7e03dd80008647086', description='null'}-`172.17.0.2:27017`] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:

docker inspect提供了应用程序所指的正确IP地址:

    "Ports": {
        "27017/tcp": [
            {
                "HostIp": "0.0.0.0",
                "HostPort": "27017"
            }
        ]
    },
    "SandboxKey": "/var/run/docker/netns/27e81b7954c9",
    "SecondaryIPAddresses": null,
    "SecondaryIPv6Addresses": null,
    "EndpointID": "ac2ded64c0bb0c9c1d82f4081b84ba2ae7f72e90527f310c7b107d0d3b7df1e0",
    "Gateway": "172.17.0.1",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "IPAddress": "172.17.0.2",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "MacAddress": "02:42:ac:11:00:02",
    "Networks": {
        "bridge": {
            "IPAMConfig": null,
            "Links": null,
            "Aliases": null,
            "NetworkID": "dc798dbca17eaced0a9bdb9f87da3672cdaa4424bafb0f49ba526c374cbfcc5b",
            "EndpointID": "ac2ded64c0bb0c9c1d82f4081b84ba2ae7f72e90527f310c7b107d0d3b7df1e0",
            "Gateway": "172.17.0.1",
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "MacAddress": "02:42:ac:11:00:02"

当应用程序和mongo独立运行时,它可以作为魅力

docker run --name mongo -p 27017:27017 -d mongo

docker run --name hygieia-api --link mongo:mongo -e "JASYPT_ENCRYPTOR_PASSWORD=hygieiasecret"    -p 8080:8080 -d hygieia-api:latest

我错过了什么?

1 个答案:

答案 0 :(得分:2)

当您使用docker撰写时,您可能会使用您的服务名称访问您的容器。因此,只需更改您的spring应用程序的mongodb IP配置,如下所示:

mongo:27017

使用您的第一个撰写文件。

version: '2'
services:
 mongo:
  image: mongo:latest
  container_name: mongo
  ports:
   - "27017:27017"

 hygieia-api:
  image: hygieia-api:latest
  container_name: hygieia-api
  ports:
   - "8080:8080"
  volumes:
   - ./logs:/hygieia/logs
  links:
   - mongo:mongo
  environment:
    - JASYPT_ENCRYPTOR_PASSWORD=hygieiasecret
  depends_on:
   - mongo

 hygieia-ui:
  image: hygieia-ui:LATEST
  container_name: hygieia-ui
  ports:
   - "8088:80"
  links:
   - hygieia-api

以下是**版本的示例:“3”

version: "3"
services:
    mongodb:
      image: mongo:3.2
      volumes:
       - ${HOME}/gocart_db:/data/db
      ports:
       - "27017:27017"

    blabla-service:
      image: blabla-service
      depends_on:
       - mongodb

在此示例中,我的blabla-service成功连接mongodb:27017