在Spring应用(docker-compose)中连接MongoDB时出现“Exception opening socket”错误。

huangapple go评论79阅读模式
英文:

Getting "Exception opening socket" on Mongodb connection from Spring App (docker-compose)

问题

尽管在应用程序属性中我将主机名设置为容器名和 MongoDB 在 docker-compose 文件中的主机名,

spring.data.mongodb.host=api-database4

作为主机名,这是 MongoDB 的容器名称和主机名,但 Spring 应用程序仍然无法连接到 MongoDB 实例。然而,我可以从 MongoDB Compass 连接到 localhost:27030,但无法连接到 mongodb://api-database4:27030/messagingServiceDb

我的 docker-compose 文件如下:

version: '3'

services:
  messaging-api6:
    container_name: 'messaging-api6'
    build: ./messaging-api
    restart: always
    ports:
      - 8085:8080
    depends_on:
      - api-database4
    networks:
      - shared-net

  api-database4:
    image: mongo
    container_name: api-database4
    hostname: api-database4
    restart: always
    ports:
      - 27030:27017
    networks:
      - shared-net
    command: mongod --bind_ip_all

networks:
  shared-net:
    driver: bridge

我的 Spring 应用程序的 Docker 文件如下:

FROM openjdk:12-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

我的 application.properties 文件如下:

#Local MongoDB config
spring.data.mongodb.database=messagingServiceDb
spring.data.mongodb.port=27030
spring.data.mongodb.host=api-database4

完整的代码可以在 这里 查看。

如何使我的运行在 Docker 容器中的 Spring 应用程序能够连接到另一个 Docker 容器中的 MongoDB 实例?

我已经尝试了类似问题的解决方案并复制了它们,但仍然出现相同的错误。

在Spring应用(docker-compose)中连接MongoDB时出现“Exception opening socket”错误。

编辑和解决方案:

我通过将下面的配置注释掉来解决了这个问题,

#Local MongoDB config
#spring.data.mongodb.database=messagingServiceDb
spring.data.mongodb.host=api-database4
spring.data.mongodb.port=27030

剩下的问题是,为什么呢?那个端口就是我尝试连接的正确端口。这可能与配置顺序有关吗?

英文:

Even though I'm giving in the application properties,

spring.data.mongodb.host=api-database4

as the hostname which is the container name and hostname of the MongoDB on the docker-compose file, Spring app still can't connect to the MongoDB instance. I can however connect from MongoDB Compass to localhost:27030 but not to mongodb://api-database4:27030/messagingServiceDb.

My docker-compose file;

version: '3'

services:
  messaging-api6:
    container_name: 'messaging-api6'
    build: ./messaging-api
    restart: always
    ports:
      - 8085:8080
    depends_on:
      - api-database4
    networks:
      - shared-net

  api-database4:
    image: mongo
    container_name: api-database4
    hostname: api-database4
    restart: always
    ports:
      - 27030:27017
    networks:
      - shared-net
    command: mongod --bind_ip_all

networks:
  shared-net:
    driver: bridge

and my Docker file for the Spring app is;

FROM openjdk:12-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

and my application.properties are;

#Local MongoDB config
spring.data.mongodb.database=messagingServiceDb
spring.data.mongodb.port=27030
spring.data.mongodb.host=api-database4

Entire code can be seen here.

How can I make my spring app on a docker container create a connection to the MongoDB instance which is on another docker container?

I have tried the solutions on similar questions and replicated them, it still gives the same error.

在Spring应用(docker-compose)中连接MongoDB时出现“Exception opening socket”错误。

Edit and Solution:

I solved the issue by commenting out configuration below,

#Local MongoDB config
#spring.data.mongodb.database=messagingServiceDb
spring.data.mongodb.host=api-database4
spring.data.mongodb.port=27030

The remaining question is, why? That was the correct port that I'm trying to connect. Could it be related to the configuration order?

答案1

得分: 1

docker-compose 中的 ports 指令将容器端口发布到主机机器上。这些容器使用暴露的端口进行彼此通信。您可以使用 netcat 测试一个容器是否能够访问另一个容器。

docker exec -it messaging-api6 bash
> apt-get install netcat
> nc -z -v api-database4 27030
> nc -z -v api-database4 27017
英文:

ports directive in docker-compose publishes container ports to the host machine. The containers communicate with each other on exposed ports. You can test whether a container can reach another with netcat.

docker exec -it messaging-api6 bash
> apt-get install netcat
> nc -z -v api-database4 27030
> nc -z -v api-database4 27017

huangapple
  • 本文由 发表于 2020年9月16日 01:08:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63906760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定