我按照Spring 官方教程(https://spring.io/guides/gs/spring-boot-docker/)来构建基于springBoot的应用程序到docker镜像。 docker镜像已成功构建,但是当我想执行docker run
命令来启动容器时,我收到以下错误消息:
Error: Invalid or corrupt jarfile /app.jar
并且包含者无法正常运行。
之前是否有人收到过相同的错误消息?我真的需要你的帮助。非常感谢你!
答案 0 :(得分:7)
java aplication的结构
Demo
└── src
| ├── main
| │ ├── java
| │ │ └── org
| │ │ └── demo
| │ │ └── Application.java
| │ └── resources
| │ └── application.properties
| └── test
| └── java
| └── org
| └── demo
| └── Application.java
├──── Dockerfile
├──── pom.xml
pom.xml的内容
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.executablejar</groupId>
<artifactId>demo</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<java-version>1.8</java-version>
<docker.image.prefix>springDemo</docker.image.prefix>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>demo</finalName>
</build>
Dockerfile的内容
FROM java:8
EXPOSE 8080
ADD /target/app.jar demo.jar
ENTRYPOINT ["java","-jar","demo.jar"]
构建和运行图像的命令
转到项目目录。让我们说D:/ Demo
$ cd D/demo
$ mvn clean install
$ docker build demo .
$ docker run -p 8080:8080 -t demo
答案 1 :(得分:3)
您必须将JAR_FILE构建arg添加到dockerfile-maven-plugin中,因为在春季教程提供的代码示例中缺少它:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.9</version>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
答案 2 :(得分:0)
我遇到了同样的问题,请在dockerFile中检查jar应用程序的名称