Buildning docker image from Dockerfile with maven

2019-08-22 13:00发布

问题:

I'm building a docker image from a jax-rs web application built with Maven. The project builds an image from the Dockerfile, but when I'm about to run the container I get Error: Could not find or load main class.

I've been looking through the docker image with docker inspect and noticed that the Entrypoint from the Dockerfile is not copied in the right way. I have another project where building the image and running it is working, where the Dockerfile is set up in the same way, and in those case the ENTRYPOINT is copied correctly. So I think this is the problem, but I haven't found I way to solve it.

Edit: After correcting an error in my Dockerfile, I can exclude that the copying of entrypoints to the image is the problem. However, the error remains: I now get the error "Could not find or load main class .opt.cartservicejava-swarm.jar" where before it was only "Could not find or load main class".

This is the pom.xml file

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.cartservicejava</groupId>
    <artifactId>cartservicejava</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <finalName>cartservicejava</finalName>
        <plugins>
            <plugin>
                <groupId>org.wildfly.swarm</groupId>
                <artifactId>wildfly-swarm-plugin</artifactId>
                <version>${version.wildfly-swarm}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <properties>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.wildfly-swarm>2017.7.0</version.wildfly-swarm>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>jaxrs</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>7.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.wildfly.swarm</groupId>
                <artifactId>bom-all</artifactId>
                <version>${version.wildfly-swarm}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

This is the Dockerfile

FROM openjdk:jre-alpine

COPY target/cartservicejava-swarm.jar /opt/cartservicejava-swarm.jar

EXPOSE 8080
# preferIPv4Stack is needed to keep wildfly-swarm happy
ENTRYPOINT ["java", "-Djava.net.preferIPv4Stack=true", "/opt/cartservicejava-swarm.jar"]