简体   繁体   中英

How to deal with Error: Unable to access jarfile - docker

I'm new to docker and I struggle with "Error: Unable to access jar file target/nbd.jar" . My application is a CRUD api for cassandra cluster made with Springboot. I'm trying to execute docker-compose up . Every cassandra node is running, I only have problem with my API. I think the problem is with the Dockerfile, but I don't know how to solve it myself/ Tried different things but none of them worked.

Here's my Dockerfile:

FROM maven:3.6.0-jdk-11-slim 
ARG JAR_FILE
COPY target/${JAR_FILE} target/nbd.jar
ENTRYPOINT ["java","-jar","nbd.jar"]

My filesystem (I cut everything but docker and target files):

├── docker
│   ├── docker-compose.yaml
│   └── Dockerfile
(...)
├── src
│   ├── (...)                 
├── stop_all.sh
└── target
    ├── classes
    │   (...)
    ├── generated-sources
    │   └── annotations
    ├── generated-test-sources
    │   └── test-annotations
    ├── nbd.jar
    └── test-classes

And my pom.xml:

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.16.Final</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-cassandra</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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
   (...)

</project>

Thanks!

You copy jar file from target directory on the local computer to the target directory inside a docker container. But then you didn't specify target directory in ENTRYPOINT . Simply put, docker can't find your jar file when it tries to start.

Try this:

FROM maven:3.6.0-jdk-11-slim 
ARG JAR_FILE
COPY target/${JAR_FILE} nbd.jar
ENTRYPOINT ["java","-jar","nbd.jar"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM