【问题标题】:Dockerized spring-boot web service throws FileNotFound ExceptionDockerized spring-boot web 服务抛出 FileNotFoundException
【发布时间】:2019-11-15 01:34:21
【问题描述】:

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

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

以下是可用的 docker 卷

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

application.properties

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

Java

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-boot docker dockerfile docker-container file-not-found


    【解决方案1】:

    我已经改变了我的 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>    
    

    【讨论】:

      【解决方案2】:

      您很可能没有文件 src/nlp/train/models/en/token/en-token.bin 与构建容器的目录相同。

      parent/
      ├── Dockerfile
      └── src/
          └── nlp/
              └── train/
                  └── models/
                      └── en/
                          └── token/
                              └── en-token.bin <--- does this exist?
      

      【讨论】:

        【解决方案3】:

        请注意 train 目录之前路径中的额外 slash /data//train/models/en/token/en-token.bin

        考虑将阅读行改为:

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

        【讨论】:

        • 我现在已经编辑了错误,这是错字对不起..你现在可以再检查一下吗?我相信它与额外的斜线无关
        • 在添加资源后尝试将RUN ls /data/train/models/en/tokenRUN cat /data/train/models/en/token/en-token.bin 添加到您的Dockerfile。构建映像时的输出是什么?
        • 它应该包含文件名/文件内容
        猜你喜欢
        • 2012-03-06
        • 2018-10-25
        • 2019-03-15
        • 2011-07-07
        • 2019-09-04
        • 2020-06-02
        • 1970-01-01
        • 1970-01-01
        • 2018-11-13
        相关资源
        最近更新 更多