简体   繁体   English

Docker 无法访问 jar 文件

[英]Docker unable to access jar file

The docker container is not able to access the jar file, that is being accessed over the mount point /my/project/dir . docker 容器无法访问通过挂载点/my/project/dir访问的 jar 文件。 I am certain it is not a permission issue, because I changed the access rights locally, so it should be able to read/write/execute it.我确定这不是权限问题,因为我在本地更改了访问权限,所以它应该能够读/写/执行它。

This is the Dockerfile :这是Dockerfile

FROM tomcat:9-jre8
RUN apt-get update && apt-get install librrds-perl rrdtool -y
VOLUME ["/data/rrdtool", "/my/project/dir"]
ENTRYPOINT [ "java","-jar","/my/project/dir/build/libs/spring-project-0.1.0.jar" ]

And this is the docker-compose.yml file:这是docker-compose.yml文件:

version: '2'
services:
 db:
   container_name: db1
   image: mysql:8
   restart: always
   environment:
     MYSQL_ROOT_PASSWORD: password123
     MYSQL_USER: user123
     MYSQL_PASSWORD: pasw
     MYSQL_DATABASE: mydb
   expose:
     - "3307"
 db2:
   container_name: db2
   image: mysql:8
   restart: always
   environment:
     MYSQL_ROOT_PASSWORD: password123
     MYSQL_USER: user123
     MYSQL_PASSWORD: pasw
     MYSQL_DATABASE: mydb2
   expose:
     - "3308"
 spring:
   container_name: spring-boot-project
   build: 
     context: ./
     dockerfile: Dockerfile
   links:
     - db:db1
     - db2:db2
   depends_on:
     - db
     - db2
   expose:
     - "8081"
   ports:
     - "8081:8081"
   restart: always

This is the output from docker-compose logs spring :这是来自docker-compose logs spring

Error: Unable to access jarfile /my/project/dir/build/libs/spring-project-0.1.0.jar

I don't see you copying the jar into the container anywhere. 我看不到您将jar复制到容器中的任何位置。 You should try moving a VOLUME declaration from Dockerfile to the compose file into the spring service like this: 您应该尝试将VOLUME声明从Dockerfile移到compose文件到spring服务中,如下所示:

  volumes:
    - /my/project/dir:/app

And then inside Dockerfile you should point to the dir: 然后在Dockerfile中,您应该指向目录:

ENTRYPOINT [ "java","-jar","/app/build/libs/spring-project-0.1.0.jar" ]

Later on if you'd like to deploy it (for example) you should copy the project files directly into the image instead of utilizing the volumes approach. 以后,如果您想部署它(例如),则应将项目文件直接复制到映像中,而不要使用volumes方法。 So in Dockerfile you'd then do: 因此,在Dockerfile您需要执行以下操作:

COPY . /app

instead of VOLUME [..] 代替VOLUME [..]

Putting it all together: 放在一起:

development: 发展:

Dockerfile: Dockerfile:

FROM tomcat:9-jre8
RUN apt-get update && apt-get install librrds-perl rrdtool -y
ENTRYPOINT [ "java","-jar","/app/build/libs/spring-project-0.1.0.jar" ]

compose-file: 撰写文件:

version: '2'
services:
    [..]
    spring:
       container_name: spring-boot-project
       build: .
       links:
         - db:db1
         - db2:db2
       depends_on:
         - db
         - db2
       ports:
         - "8081:8081"
       restart: always
       volumes:
         - /my/project/dir:/app

deployment: 部署:

Dockerfile (that is placed inside project's folder, docker build requires it's build context to be in a current directory): Dockerfile(放置在项目的文件夹中,docker build要求其build上下文位于当前目录中):

FROM tomcat:9-jre8
RUN apt-get update && apt-get install librrds-perl rrdtool -y
COPY . /app
ENTRYPOINT [ "java","-jar","/app/build/libs/spring-project-0.1.0.jar" ]

compose-file: 撰写文件:

version: '2'
services:
    [..]
    spring:
       container_name: spring-boot-project
       build: .
       links:
         - db:db1
         - db2:db2
       depends_on:
         - db
         - db2
       expose:
         - "8081"

If you are using Spring-Boot Project with Maven build.如果您使用Spring-Boot项目和Maven构建。 Try with below试试下面

Dockerfile . Dockerfile

FROM maven:3.8.4-openjdk-17 as maven-builder
COPY src /app/src
COPY pom.xml /app

RUN mvn -f /app/pom.xml clean package -DskipTests
FROM openjdk:17-alpine

COPY --from=maven-builder app/target/dockube-spring-boot.jar /app-service/dockube-spring-boot.jar
WORKDIR /app-service

EXPOSE 8080
ENTRYPOINT ["java","-jar","dockube-spring-boot.jar"]

dockube-spring-boot.jar // replace with your generated jar name dockube-spring-boot.jar // 替换为您生成的 jar 名称

Here is the Sample Code Available这是可用的示例代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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