dockerizing Spring Boot Application错误:错误:jarfile /app.jar

时间:2019-11-02 04:11:52

标签: spring-boot docker docker-image

我开始使用docker,并尝试对我的Spring Boot应用程序进行dockerize。我使用spring initializr生成了这个。我正在和Maven合作。我的DockerFile看起来像这样:

FROM java:8-jdk-alpine

COPY ${JAR_FILE} app.jar

WORKDIR /usr/app

EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]

这很简单。

我执行了以下命令:

docker build -t shortenurl .

我可以在控制台中看到带有docker images的图像:

enter image description here

但是当我尝试使用以下命令运行映像时:docker run -p 8080:8080 shortenurl,我收到此错误消息:错误:jarfile /app.jar无效或损坏

我尝试了不同的配置,但不确定。有什么建议吗?

谢谢。

这是我的插件pom.xml插件部分:

       <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.2.3</version>
                <configuration>
                    <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>

这是我的命令后的日志:docker build -tshortenurl。

$ docker build -t shortenurl .
Sending build context to Docker daemon  32.89MB
Step 1/5 : FROM java:8-jdk-alpine
 ---> 3fd9dd82815c
Step 2/5 : COPY ${JAR_FILE} app.jar
 ---> 248aa4be697d
Step 3/5 : WORKDIR /usr/app
 ---> Running in 5fcd71c548af
Removing intermediate container 5fcd71c548af
 ---> bc344b970b11
Step 4/5 : EXPOSE 8080
 ---> Running in 46b4e4f8e9b7
Removing intermediate container 46b4e4f8e9b7
 ---> 6b4f55a86a23
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in 838a6052f4c8
Removing intermediate container 838a6052f4c8
 ---> 7d7d272ea42d
Successfully built 7d7d272ea42d
Successfully tagged shortenurl:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

1 个答案:

答案 0 :(得分:0)

您正在使用spring-boot-maven-plugin进行构建,但没有生成可执行jar。将插件的配置更新为:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

你应该没事。