spring-boot - 将Maven项目化后,出现错误。构建Dockerfile时发生错误。怎么解决呢?

标签 spring-boot docker maven jenkins

我想在 Jenkins 建立一个 docker 形象。
我的要求是建立 war ,并使用Dockerfile在tomcat中部署一个 Spring 启动应用程序。
我的Dockerfile是

FROM maven:3.5-jdk-8-alpine
WORKDIR /app
COPY pom.xml /app/
RUN cd /app/ && mvn install



FROM tomcat:9
EXPOSE 8087
COPY /target/*.war /usr/local/tomcat/webapps/
CMD ["catalina.sh","run"]
我得到的错误是:
[INFO] Total time: 49:49 min
[INFO] Finished at: 2020-07-23T12:21:16Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.4.0-SNAPSHOT:repackage (repackage) on project restcurd: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.4.0-SNAPSHOT:repackage failed: Unable to find main class -> [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/PluginExecutionException
The command '/bin/sh -c cd /app/ && mvn install' returned a non-zero code: 1

Build step 'Execute shell' marked build as failure
运行Dockerfile的命令:docker build -t boot .我找不到错误的原因。
  • 我该如何解决该问题?

  • 有人告诉我pom.xml中存在错误
    有我的pom.xml
    <?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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.0-SNAPSHOT</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>restcurd</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>restcurd</name>
        <packaging>war</packaging>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>2020.0.0-SNAPSHOT</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
    
                <!-- Dockerfile Maven -->
    
                <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>dockerfile-maven-plugin</artifactId>
                    <version>1.4.10</version>
                    <executions>
                        <execution>
                            <id>default</id>
                            <phase>package</phase>
                            <goals>
                                <goal>build</goal>
                                <!-- <goal>push</goal> -->
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <repository>pomkiticat/${project.name}</repository>
                        <tag>${project.version}</tag>
                        <skipDockerInfo>true</skipDockerInfo>
                    </configuration>
                </plugin>
                <!--docker-->
            </plugins>
        </build>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </repository>
            <repository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </pluginRepository>
            <pluginRepository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    
    </project>
    
    
    我找不到问题。
    2. pom.xml有什么问题?
    提前致谢。

    最佳答案

    问题不是 docker ,而是您的pom。尝试使用shell以交互方式运行docker容器,并检查您的maven错误并进行修复:

    FROM maven:3.5-jdk-8-alpine
    WORKDIR /app
    COPY pom.xml /app/
    
    然后尝试进入容器:
    docker build -t boot .
    docker run -ti boot sh
    # # Type in whatever shell command like your maven command and debug it
    # mvn install
    
    编辑:
    我给出的解释是尝试验证您的Dockerfile并了解它是否无效。您的Dockerfile包含“mvn install”命令以构建应用程序。但是,正如您在运行此命令时所解释的那样,该命令失败。
    严格来说,我需要4个步骤:
    第1步:确保您的mvn安装构建程序在本地正常运行,并且不涉及任何docker。看来您错过了主要类。您是否在没有docker的情况下在本地重现此错误?是=>通过添加该Main类对其进行修复。否=>转到下一步。
    第2步:使用Docker创建一个容器,以构建您的应用并使其正常运行。如果它不起作用,但是您的本地构建有效,则您的容器具有与本地不同的执行上下文,这是一个问题。我在答复中提出的解决方案是再次确认这一点。您需要在容器内运行命令。显然,您无法通过收到的新错误来执行此操作。我强烈建议您在尝试进一步使用docker之前接受过培训,因为您似乎有点迷失了方向。 (无罪,有时我们只需要训练更多)。
    第3步:理想情况下,您的容器只能建立,产生 war ,然后使用另一个容器进行 war 。您不需要源代码+最终容器中的所有中间生成文件。
    步骤4:测试您的最终容器。

    关于spring-boot - 将Maven项目化后,出现错误。构建Dockerfile时发生错误。怎么解决呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63057933/

    相关文章:

    docker - 如何从 docker 容器访问主机的 localhost 127.0.0.1

    dependencies - Maven提供的范围

    java - 如何获取 Maven Artifact 的文件路径?

    java - 如何在 Java 中计算以下 JSON 值 U.O.M Wise 的总值

    spring-boot - 当SpringBoot应用程序请求ElasticSearch时,是否可以将所有索引数据搜索上的Fuzzy参数设置为应用程序参数?

    java - 由 : org. springframework.data.mapping.PropertyReferenceException: No property findAll found for type User - Redis 引起

    java - 无法使用 MAVEN 进行构建,出现错误 - 无法执行目标 org.apache.maven.plugins :maven-compiler-plugin:2. 3.2:compile

    java - 将 Spring boot 1.5 迁移到 2.x 无法启动应用程序

    java - 连接被拒绝 : accessing a spring boot application running in docker container

    python-3.x - 容器化后,Python Websocket Server不起作用( docker )