首页 > 解决方案 > Dockerized spring-boot web 服务抛出 FileNotFound 异常

问题描述

我尝试使用 Docker File 命令将文件从我的 Windows 机器复制到 Docker 容器,并从 spring web service 读取这些文件。Webservice 抛出错误文件未找到!

在这里,我试图将我的本地目录 src/nlp 复制到 /data 容器目录

以下是可用的 docker 卷

在此处输入图像描述

码头工人文件

FROM openjdk:8-jdk-alpine

EXPOSE 8080

ARG JAR_FILE=/target/nlp-0.0.1-SNAPSHOT.jar

ADD ${JAR_FILE} nlp-0.0.1-SNAPSHOT.jar

ADD src/nlp  /data

ENTRYPOINT ["java","-jar", "nlp-0.0.1-SNAPSHOT.jar"]`

应用程序属性

server.port=8080
logging.level.radial.nlp=DEBUG
logging.file = mylogfile.log
nlp.learning.dir = /data/

爪哇

InputStream inputStream = new FileInputStream(environment.getProperty("nlp.learning.dir")+ "/train/models/en/token/en-token.bin"); 

错误

java.io.FileNotFoundException: /data/train/models/en/token/en-token.bin (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)

标签: spring-bootdockerdockerfiledocker-containerfile-not-found

解决方案


我已经更改了我的 Maven 插件依赖项,现在它工作正常

<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.7</version>
<!-- Wire up to the default build phases -->
<executions>
  <execution>
    <id>default</id>
    <goals>
      <goal>build</goal>
      <goal>push</goal>
    </goals>
  </execution>
</executions>
<configuration>
  <repository>${project.artifactId}</repository>
  <buildArgs>
    <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
  </buildArgs>
</configuration>    


推荐阅读