Issue
I want to build a docker image in Jenkins. My requirement is build war and deploy in tomcat a spring boot application using Dockerfile.
My Dockerfile is
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"]
I have getting error is :
[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
The command for run the Dockerfile :docker build -t boot .
I can not find the cause of the error.
- How can I solve the problem?
Some one tell me there is an error in my pom.xml There is my 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>
I can not find the problem. 2. What is problem with pom.xml ? Thanks in advance.
Solution
The problem isn't docker but your pom. Try run your docker container in interactive mode with a shell, and check your maven error and fix it:
FROM maven:3.5-jdk-8-alpine
WORKDIR /app
COPY pom.xml /app/
Then try to get inside your container:
docker build -t boot .
docker run -ti boot sh
# # Type in whatever shell command like your maven command and debug it
# mvn install
Edit:
The explanation I give is to try to validate your Dockerfile and understand while it doesn't work. Your Dockerfile include the "mvn install" command to build the application. But that command fail as you explain while running this command.
Strictly speaking for me you need 4 steps:
Step 1: Ensure your mvn install build work fine locally, without any docker involved. It seems you miss a main class. Does you reproduce this error locally without docker ? Yes => Fix it by adding that Main class. No => Go to next step.
Step 2: Create a container with docker that build your app and make it work. If it doesn't work but your local build works, then you have a problem with your container having a different execution context than locally. The solution I propose in my response was to double check that. You need to run the command inside the container. Apparently you failed to do that with the new error you got. I strongly advice that you get a training on using docker before you try to go further in that direction as you seems a bit too lost. (No offence, sometime we just need to train more).
Step 3: Ideally your container only build, produce a war, and another container is used to run the war. You don't need the source code + all intermediate generated files in your final container.
Step 4: Test your final container.
Answered By - Nicolas Bousquet