无法连接到mongo容器

时间:2019-01-28 15:40:47

标签: java spring mongodb docker docker-compose

我之前有一个问题(至少我认为),当我设法解决出现的错误时,它或多或少变得无用了。但是,我的新错误是,我似乎无法从内部连接到mongodb我的泊坞窗图片

docker-compose.yml:

version: '3.1'
services:
  company-repo-docker-app:
    image: company-repo-image
    build:
        context: ./
    dockerfile: Dockerfile
    depends_on:
        - mongo
ports:
    - 8080:8080
mongo:
image: mongo
ports:
  - '27017:27017'
networks:
  mongo_net:
    ipv4_address: 172.28.0.2

networks:
  mongo_net:
    driver: bridge
    ipam:
      driver: default
      config:
      -
       subnet: 172.28.0.2/24

application.properties:

spring.data.mongodb.database=myDb
spring.data.mongodb.host=172.28.0.2 
spring.data.mongodb.port=27017

以及实际错误:

 com.mongodb.MongoSocketOpenException: Exception opening socket
   at com.mongodb.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.6.4.jar!/:na]
   at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.6.4.jar!/:na]
   at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.4.jar!/:na]
   at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
Caused by: java.net.SocketTimeoutException: connect timed out 

2 个答案:

答案 0 :(得分:1)

您已在docker-compose.yml文件中明确创建了一个网络,并将数据库设置为使用该网络。 Docker Compose还会为您创建一个默认网络,并且由于您的应用程序未明确声明任何网络,因此它仅附加到默认网络。然后,这两个容器位于不同的网络上,而一个无法到达另一个。

这里最简单的解决方案是删除所有手动网络配置:

version: '3.1'
services:
  company-repo-docker-app:
    image: company-repo-image
    build:
      context: ./
    depends_on:
      - mongo
    ports:
      - '8080:8080'
  mongo:
    image: mongo
    ports:
      - '27017:27017'

服务块的名称将是有效的DNS主机名,在您的配置中,您可以设置spring.data.mongodb.host=mongo

答案 1 :(得分:1)

我同意David的回答是正确的。但是这里有个要点,您的配置也可以,我尝试了我的示例,这是结果。

问题是您的应用未连接到网络。

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http, RoleHierarchy roleHierarchy() {
        return http
            .csrf().disable()
            .formLogin().disable()
            .httpBasic().disable()
            .authenticationManager(reactiveAuthenticationManager)
            .securityContextRepository(securityContextRepository)
            .authorizeExchange()
                .pathMatchers("/user/**").access(new CustomReactiveAuthorizationManager<>("USER", roleHierarchy))
            .anyExchange().permitAll()
            .and()
            .logout().disable()
            .build();
    }

样本docker-compose.yml

services:
  company-repo-docker-app:
    image: company-repo-image
    build:
        context: ./
    dockerfile: Dockerfile
    depends_on:
        - mongo
    ports:
     - 8080:8080
    networks:
     - mongo_net

mongo容器ip地址

version: '3.1'
services:
  mongo-app:
    image: barath-mongo
    ports:
      - "9000:9000"
    depends_on:
      - mongo
    environment:     
      SPRING_DATA_MONGODB_PORT: 27017
      SPRING_DATA_MONGODB_HOST: 172.28.0.2 
      SPRING_DATA_MONGODB_DATABASE: myDb
    networks:
      - mongo_net
  mongo:
    image: mongo
    ports:
    - '27017:27017'
    networks:
     mongo_net:
        ipv4_address: 172.28.0.2
networks:
  mongo_net:
    driver: bridge
    ipam:
      driver: default
      config:
      -
       subnet: 172.28.0.2/24

docker网络设置

$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ec2-user_mongo_1
172.28.0.2

输出

 "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.28.0.2/24"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "02b8b4148dd830eaddeb7e9ebd6305a6ae3c106bae5a495103c632aec7999274": {
                "Name": "ec2-user_mongo-app_1",
                "EndpointID": "885088d551888e1dec95b1c0e337c6a4b84f1bb2aebc4407963d3c0b4f6ec09d",
                "MacAddress": "02:42:ac:1c:00:03",
                "IPv4Address": "172.28.0.3/24",
                "IPv6Address": ""
            },
            "3e4266fca3c8163412cc1c4733cad004aa0f366dc9fe30d03c7ecafe87b4e826": {
                "Name": "ec2-user_mongo_1",
                "EndpointID": "a6a72c7fc314a294c53d5c394ac1757702a0a95c588af2f730e971953dbbaa5f",
                "MacAddress": "02:42:ac:1c:00:02",
                "IPv4Address": "172.28.0.2/24",
                "IPv6Address": ""
            }
        },