连接 Spring Boot 容器到 Mongo 容器:java.net.UnknownHostException: mongo: 系统错误?

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

Connecting Spring boot Container to Mongo Container: java.net.UnknownHostException: mongo: System error?

问题

我已经创建了一个简单的Spring Boot Rest应用程序,并将其docker化,在我本地使用mongo时按预期工作。

然而,我现在尝试使用Docker Compose运行一个Mongo容器来连接,而不是使用我的本地主机。

Docker-compose up -d可以正常工作,但是当我尝试访问查询Mongo的端点时,出现以下错误:

Caused by: java.net.UnknownHostException: mongo: System error
    at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:na]
    at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(Unknown Source) ~[na:na]
    at java.base/java.net.InetAddress.getAddressesFromNameService(Unknown Source) ~[na:na]
    at java.base/java.net.InetAddress$NameServiceAddresses.get(Unknown Source) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName0(Unknown Source) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName(Unknown Source) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName(Unknown Source) ~[na:na]
    at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongodb-driver-core-3.11.2.jar!/:na]

在我的application.properties文件中,我有:

spring.data.mongodb.uri=mongodb://mongo:27017/pokerStats

我的Docker文件(名为pokerStats)中的内容:

#
FROM adoptopenjdk/openjdk11:alpine-jre

#
ARG JAR_FILE=/build/libs/pokerStats-0.0.1-SNAPSHOT.jar

#
WORKDIR /opt/app

#
COPY ${JAR_FILE} app.jar

#
ENTRYPOINT ["java","-jar","app.jar"]

我的docker-compose.yml文件:

version: "3"
services:
  pokerStats:
    image: pokerStats
    ports:
      - 8080:8080
    depends_on: 
      - db
  db:
    image: mongo
    volumes:
      - ./database:/data
    ports:
      - "27017:27017"
英文:

I have created a simple Spring Boot Rest Application that I have dockerized and is working as expected when I use mongo locally.

However I have now tried to use Docker Compose to run a Mongo container to connect to rather than my local host.

Docker-compose up -d works but when I try to hit my endpoint that queries Monogo I get:

Caused by: java.net.UnknownHostException: mongo: System error
    at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:na]
    at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(Unknown Source) ~[na:na]
    at java.base/java.net.InetAddress.getAddressesFromNameService(Unknown Source) ~[na:na]
    at java.base/java.net.InetAddress$NameServiceAddresses.get(Unknown Source) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName0(Unknown Source) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName(Unknown Source) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName(Unknown Source) ~[na:na]
    at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongodb-driver-core-3.11.2.jar!/:na]

In my application.properties file I have:

spring.data.mongodb.uri=mongodb://mongo:27017/pokerStats

My Docker file for my spring boot app (called pokerStats):

#
FROM adoptopenjdk/openjdk11:alpine-jre

#
ARG JAR_FILE=/build/libs/pokerStats-0.0.1-SNAPSHOT.jar

#
WORKDIR /opt/app

#
COPY ${JAR_FILE} app.jar

#
ENTRYPOINT ["java","-jar","app.jar"]

My docker-compose.yml file:

version: "3"
services:
  pokerStats:
    image: pokerStats
    ports:
      - 8080:8080
    depends_on: 
      - db
  db:
    image: mongo
    volumes:
      - ./database:/data
    ports:
      - "27017:27017"

答案1

得分: 2

属性文件指定了主机mongo,但是容器可以通过容器名称进行发现,该名称为db。要么需要更改属性以使用正确的名称,要么需要将容器名称更改为属性文件所期望的名称。

英文:

The properties file specifies the host mongo, but containers are discoverable via the container name, which is db.

Either the properties need to be changed to use the right name or the container name changed to what the properties file expects.

huangapple
  • 本文由 发表于 2020年4月7日 05:39:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61069384.html
匿名

发表评论

匿名网友

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

确定