当前位置: 首页 > 知识库问答 >
问题:

在Docker中连接Spring Boot和MongoDB

靳彦
2023-03-14

我正在尝试用docker-compose.yml连接MongoDb和Spring Boot。

就这样-

version: "3.7"

services:
  app-server:
    container_name: spring
    build: .
    ports:
      - "8080:8080"
    restart: always
    depends_on:
      - db

  db:
    container_name: mongo
    image: mongo
    ports:
      - "27017:27017"
    restart: always
    volumes:
      - ./datadir:/var/lib/mongo

我的Dockerfile for Spring

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

pom.xml插件用于构建JAR_FILE-

            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.3.6</version>
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <repository>maximko/${project.artifactId}</repository>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>

application.properties文件

spring.data.mongodb.host=db
spring.data.mongodb.port=27017
spring.data.mongodb.database=HotelDB

我的问题

当我运行“maven package”构建我的jar时,我得到以下错误:我的jar文件没有创建

    com.mongodb.MongoSocketException: db
    at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar:na]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
Caused by: java.net.UnknownHostException: db
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_181]
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[na:1.8.0_181]
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[na:1.8.0_181]
    at java.net.InetAddress.getAllByName0(InetAddress.java:1276) ~[na:1.8.0_181]
    at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[na:1.8.0_181]
    at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[na:1.8.0_181]
    at java.net.InetAddress.getByName(InetAddress.java:1076) ~[na:1.8.0_181]
    at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:186) ~[mongodb-driver-core-3.8.2.jar:na]
    ... 5 common frames omitted

当我运行docker-compose.yml时,我得到以下错误-

    spring        | com.mongodb.MongoSocketOpenException: Exception opening socket
    spring        |         at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongodb-driver-core-3.8.2.jar!/:na]
    spring        |         at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar!/:na]
    spring        |         at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar!/:na]
    spring        |         at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
    spring        | Caused by: java.net.ConnectException: Connection refused (Connection refused)
    spring        |         at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
    spring        |         at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
    spring        |         at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
    spring        |         at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
    spring        |         at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
    spring        |         at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]

1)这些问题是来自“application.properties”文件,还是我在docker-compose文件中配置错误?

2)我可以改变我的MongoDB的主机名吗?

3)在运行“docker-compose.yml”之前,是否需要运行“mvn package”来构建app.jar文件?

谢谢你的帮助!

共有1个答案

石苏燕
2023-03-14

终于...我解决了我的问题。这是我的变化:

1)我从pom.xml中删除了这个插件

        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.3.6</version>
            <executions>
                <execution>
                    <id>build-image</id>
                    <phase>package</phase>
                    <goals>
                        <goal>build</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <repository>maximko/${project.artifactId}</repository>
                <buildArgs>
                    <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                </buildArgs>
            </configuration>
        </plugin>

并在DockerFile中替换此代码。在ENTRYPOINT中,我将mongodb连接uri添加到我的db服务中(这是主要的更改!)

FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY target/spring-mongo-demo-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c "touch /app.jar"
ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://db:27017/HotelDB","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

为了构建我的app.jar,我使用了mavem包,但如果没有生成jar文件,则应该同时使用mavem install

同样在application.properties文件中,我指示了spring.data.mongoDB.host=db,并捕获到以下错误

Caused by: java.net.UnknownHostException: db

在构建JAR文件时,为了避免这个异常,我编写了spring.data.mongodb.host=localhost

 类似资料:
  • 我拉出了mysql docker映像,并用命令运行容器: 此时,springBoot在本地工作。它与MySQL连接: 现在,我希望springBoot应用程序也在单独的docker容器上,在同一服务器上。为此,我使用了DockerFile: 同样,它无法连接到数据库:

  • 使用docker通过docker compose将springboot连接到postgres。在postgres上使用5432端口可以正常工作,如果我尝试其他端口,它就会失败 工作代码 Spring docker compose **拒绝连接代码** Spring docker compose 错误: spring boot rest服务器| org。postgresql。util。PSQLExc

  • 警告会话0x0,因为服务器空、意外错误、关闭套接字连接并试图重新连接(org.apache.zookeeper.clientcnxn)java.net.ConnectException:连接被拒绝 zookeeper首先开始,它在2181上,当开始Kafka时,它看着2181“zookeeper”然后在我身上爆炸。忠告?

  • 我创建了一个docker compose文件,将MySql连接到SpringBoot应用程序。但我得到了这个错误: 我尝试在本地机器中使用docker默认ip运行Spring Boot应用程序,同时在这个docker-compose.yml文件中只运行MySql容器,结果运行得非常好。但当我尝试docker编写文件时。我知道这个错误。

  • 我在我的本地机器上设置了一个单节点Kafka Docker容器,就像在融合留档中描述的那样(步骤2-3)。 此外,我还公开了Zookeeper的端口2181和Kafka的端口9092,以便能够从本地机器上运行的客户端连接到它们: 问题:当我试图从主机连接到Kafka时,连接失败,因为它无法解析地址:Kafka:9092。 这是我的Java代码: 例外情况: 问:如何连接到在Docker中运行的Ka