尝试运行spring-boot应用程序时,我的命令docker-compose up给我一个错误

时间:2019-09-05 00:42:39

标签: maven spring-boot docker-compose

我正在使用Maven插件通过构建生成Dockerfile,当我运行docker-compose up时,我的spring容器启动了,但无法访问我的jar

错误:

$ docker-compose up
postgres_container is up-to-date
Starting spring_container ... done
Attaching to postgres_container, spring_container
spring_container | Error: Unable to access jarfile /demo-0.0.1-SNAPSHOT.jar

我的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.1.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lucasmarinzeck</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.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-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>2.1.7.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.13</version>
                <configuration>
                    <imageName>aps-04s</imageName>
                    <baseImage>openjdk:8-jdk-alpine</baseImage>
                    <entryPoint>["java ", "-jar ", "/${project.build.finalName}.jar"]</entryPoint>
                    <exposes>8080</exposes>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

spotify插件中的Dockerfile:

FROM openjdk:8-jdk-alpine
ADD /demo-0.0.1-SNAPSHOT.jar /
EXPOSE 8080
ENTRYPOINT ["java ", "-jar ", "/demo-0.0.1-SNAPSHOT.jar"]

我进行了搜索,但确实没有找到解决方法

1 个答案:

答案 0 :(得分:2)

出现错误Error: Unable to access jarfile /demo-0.0.1-SNAPSHOT.jar的原因是该文件不存在。

ADD中的

Dockerfile将自动尝试提取存档(.jar文件是其中一个)。

我的建议是即使复制COPY也可以使用ADD