如何为docker配置Maven / SpringBoot项目的pom.xml?

时间:2019-06-15 17:40:46

标签: maven spring-boot docker

我正在尝试通过Maven对我的SpringMVC应用程序进行docker化。我的意图是要获得一张图像,然后可以继续通过Web浏览器进行显示和显示。

不幸的是,in following this guide,对于实现这一点我必须进行的pom.xml编辑以及Dockerfile,我似乎仍然缺乏关键的理解。

======

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

======

这是最新修订的source code's pom.xml

======

这是我根据上面引用的SpringIO指南的example pom.xml(不包括依赖项)进行的最新修订。

<modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.davidonus</groupId>
    <artifactId>davidonusSpringDemo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- tag::packaging[] -->
    <packaging>jar</packaging>
    <name>davidonusSpringDemo1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <!-- tag::docker[] -->
        <docker.image.prefix>springio</docker.image.prefix>
    </properties>

    <profiles>
        <profile>
            <id>DavidSpringTime</id>
        </profile>
    </profiles>


    <build>


        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- tag::plugin[] -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.9</version>
                <configuration>
                    <repository>${docker.image.prefix}/${project.artifactId}</repository>
                </configuration>
            </plugin>
            <!-- end::plugin[] -->

            <!-- tag::unpack[] -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>${project.groupId}</groupId>
                                    <artifactId>${project.artifactId}</artifactId>
                                    <version>${project.version}</version>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- end::unpack[] -->
        </plugins>
    </build>

这是我当前的结果,使用命令mvn install build:docker

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12.050 s
[INFO] Finished at: 2019-06-15T13:24:01-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:3.1.1:unpack (unpack) on project davidonusSpringDemo1: Unable to update Marker timestamp: /home/david/Desktop/DevOps2019/springBoot/teluskoSpringBoot/target/dependency-maven-plugin-markers/com.davidonus-davidonusSpringDemo1-jar-0.0.1-SNAPSHOT.marker: Unable to update last modified timestamp on marker file /home/david/Desktop/DevOps2019/springBoot/teluskoSpringBoot/target/dependency-maven-plugin-markers/com.davidonus-davidonusSpringDemo1-jar-0.0.1-SNAPSHOT.marker -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

======

总而言之,given my original pom.xml您将实施哪些更改,以使我的SpringBoot + Maven项目可部署为Docker映像和容器?

此外,您是否会对我的Dockerfile进行修改?感谢您的咨询。谢谢。

2 个答案:

答案 0 :(得分:2)

如果已经使用pom.xml准备了springboot代码。然后按照以下步骤容器化您的应用程序。

  • git clone https://github.com/dnmorris7/teluskoSpringBoot(我正在克隆您的springboot代码)
  • git checkout module5(已检出module5分支)
  • 在git代码库中创建具有以下内容的Dockerfile:
FROM maven:3.6-jdk-8-slim AS build
COPY . /usr/src/app/
WORKDIR /usr/src/app/
RUN mvn -f /usr/src/app/pom.xml clean package

FROM java:8-alpine
WORKDIR /
COPY --from=build /usr/src/app/target/*.jar /app.jar
CMD java -jar app.jar

注意::我正在使用docker多阶段构建,其中在第一个步骤中,maven构建了jar,在第二阶段中,我们将该jar复制到了Java映像中。

  • 现在构建您的docker映像docker build -t appimage:v1 .
  • 运行您的docker容器docker run -it -d -p 9090:9090 appimage:v1
  • 点击api以检查其是否正常运行。
$ curl localhost:9090/home
{"timestamp":"2019-06-16T05:34:26.655+0000","status":404,"error":"Not Found","message":"/pages/home.jsp","path":"/home"}

请输入正确的基本网址,我尝试使用/home

注意::如果您想提供自己的自定义application.properties,则将java -jar中的Dockerfile命令更改为CMD java -jar app.jar --spring.config.additional-location=application.properties并更改docker向docker run -it -d -v application.properties:/application.properties -p 9090:9090 appimage:v1运行命令,其中application.properties是您从外部提供的命令。

答案 1 :(得分:0)

我认为没有太多丢失。甚至更好,甚至可能有太多东西。

首先,您需要告诉docker maven插件运行。 maven lifecycle定义哪些插件在哪个阶段运行。因此,所有其他插件都需要在某处(在父pom或您自己的pom中)执行配置。其次,无需解压缩创建的jar文件。 Spring Boot将自动创建一个可执行的jar文件。您只需要告诉docker maven插件(创建位置)

这将是Dockerfile:

FROM openjdk:8-jdk-alpine
VOLUME /tmp

ARG JAR_FILE

ADD target/${JAR_FILE} /usr/share/myapp.jar

ENTRYPOINT ["java","-jar","/usr/share/myapp.jar"]

这是您的pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot-docker</artifactId>
    <version>0.1.0</version>
    <packaging>jar</packaging>
    <name>Spring Boot Docker</name>
    <description>Getting started with Spring Boot and Docker</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <docker.image.prefix>springio</docker.image.prefix>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- tag::plugin[] -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.9</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <repository>${docker.image.prefix}/${project.artifactId}</repository>
                    <buildArgs>
                        <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
            <!-- end::plugin[] -->
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

依赖插件的解包目标可以删除。这样,将jar添加到图像中并直接运行。希望它能起作用!