在 Docker 中连接 Spring Boot 和 MongoDB

我正在尝试在 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

我的 Spring Dockerfile


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"]

用于构建 JAR_FILE 的 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>

application.properties 文件


spring.data.mongodb.host=db

spring.data.mongodb.port=27017

spring.data.mongodb.database=HotelDB

我的问题

1)所有这些问题是否都来自“application.properties”文件,或者我在 docker-compose 文件中错误配置了某些内容?

2)我可以以某种方式更改我的mongoDB的主机名吗?

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

谢谢您的帮助!


慕盖茨4494581
浏览 116回答 2
2回答

慕标5832272

终于...我解决了我的问题。这是我的改变:1)我从pom.xml中删除了这个插件&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.spotify</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>dockerfile-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.3.6</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>build-image</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>package</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>build</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <repository>maximko/${project.artifactId}</repository>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <buildArgs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </buildArgs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>并在 Dockerfile 中替换此代码。在 ENTRYPOINT 中,我将 mongodb 连接 uri 添加到我的数据库服务(这是主要更改!)FROM openjdk:8-jdk-alpineVOLUME /tmpCOPY target/spring-mongo-demo-0.0.1-SNAPSHOT.jar app.jarRUN 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 package,但是如果没有生成你的 jar 文件,你应该使用mavem install另外,在 application.properties 文件中,我指示spring.data.mongodb.host=db并捕获以下错误Caused by: java.net.UnknownHostException: db为了避免在构建 JAR 文件时出现此异常,我改为编写spring.data.mongodb.host=localhost

慕少森

在 docker compose 文件中 -对 docker 容器使用旧版容器链接或用户定义的桥接网络。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java