Docker无法访问jar文件

时间:2018-05-07 14:09:25

标签: java docker spring-boot jar docker-compose

docker容器无法访问通过挂载点/my/project/dir访问的jar文件。 我确定这不是权限问题,因为我在本地更改了访问权限,因此它应该能够读/写/执行它。

这是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" ]

这是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

这是docker-compose logs spring的输出:

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

1 个答案:

答案 0 :(得分:1)

我没有看到你在任何地方将jar复制到容器中。您应该尝试将VOLUME声明从Dockerfile移至compose文件,转移到spring服务中,如下所示:

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

然后在Dockerfile中你应该指向dir:

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

如果您想要部署它(例如),您应该将项目文件直接复制到图像中,而不是使用volumes方法。所以在Dockerfile中,您可以这样做:

COPY . /app

而不是VOLUME [..]

全部放在一起:

<强>发展:

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" ]

撰写文件:

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

<强>部署:

Dockerfile(放置在项目文件夹中,docker 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" ]

撰写文件:

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