0

I've managed to create a docker image. And under the Docker Container files section I am able to see the directory as is.

Docker Container file-structure image

My working directory : "usr/local/bin/ClientApp"

Dockerfile:

FROM openjdk:11
EXPOSE 8080
WORKDIR /usr/local/bin/ClientApp
ADD . .
ADD target/ClientApp-0.0.1-SNAPSHOT.jar .
ENTRYPOINT ["java", "-jar", "ClientApp-0.0.1-SNAPSHOT.jar"]

Build logs:

[+] Building 10.0s (9/9) FINISHED                                                                                                                                        docker:default
 => [internal] load build definition from Dockerfile                                                                                                                               0.0s
 => => transferring dockerfile: 214B                                                                                                                                               0.0s 
 => [internal] load .dockerignore                                                                                                                                                  0.0s 
 => => transferring context: 2B                                                                                                                                                    0.0s 
 => [internal] load metadata for docker.io/library/openjdk:11                                                                                                                      1.2s 
 => [1/4] FROM docker.io/library/openjdk:11@sha256:99bac5bf83633e3c7399aed725c8415e7b569b54e03e4599e580fc9cdb7c21ab                                                                0.0s
 => [internal] load build context                                                                                                                                                  0.1s 
 => => transferring context: 30.23kB                                                                                                                                               0.1s
 => CACHED [2/4] WORKDIR /usr/local/bin/ClientApp                                                                                                                                  0.0s 
 => [3/4] ADD . .                                                                                                                                                                  6.7s 
 => [4/4] ADD target/ClientApp-0.0.1-SNAPSHOT.jar .                                                                                                                                1.0s
 => exporting to image                                                                                                                                                             0.7s
 => => exporting layers                                                                                                                                                            0.7s 
 => => writing image sha256:f98263f732ad48664c0cb0fed8e0f4bee8ca5ee652c964c7d028be953a631cae                                                                                       0.0s
 => => naming to docker.io/library/client-publisher-image:latest                                                                                                                   0.0s 

Error:

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)
Caused by: java.io.FileNotFoundException: src\main\resources\anaplan-connector-393817-7f22b1ca301b.json (No such file or directory)
        at java.base/java.io.FileInputStream.open0(Native Method)
        at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
        at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
        at java.base/java.io.FileReader.<init>(FileReader.java:75)
        at connector.demo.clientapp.ClientAppApplication.main(ClientAppApplication.java:21)
        ... 8 more

I am new to docker, can someone please help me resolving this error

Error404
  • 25
  • 1
  • 7
  • [FileNotFoundException while running as a jar](https://stackoverflow.com/q/41762124) might help you. Don't use files for accessing anything in `src/main/resources`. Also, don't add the whole project to the `Dockerfile`. The JAR is sufficient. – dan1st Jul 28 '23 at 18:58
  • I have tried that. It's not working – Error404 Jul 28 '23 at 19:15
  • 1
    And what happens when trying that? Can you share the code responsible for loading the resource? – dan1st Jul 28 '23 at 19:24
  • Since you're new to docker, your Dockerfile expects your jar to exist. It's recommended to use a multi stage build so that your Dockerfile is self contained to build and run your app. you'll find a lot of tutorials ex https://aboullaite.me/multi-stage-docker-java/ – Sylwit Aug 03 '23 at 16:57

2 Answers2

0

Try checking out the files inside the container yourself

docker run --rm -it --entrypoint bash <docker-img>
  • File exists in the directory. Permissions are also fine. Logs below: root@0b5dec5f1827:/usr/local/bin/ClientApp/src/main/resources# pwd /usr/local/bin/ClientApp/src/main/resources root@0b5dec5f1827:/usr/local/bin/ClientApp/src/main/resources# ls -l total 16 -rwxr-xr-x 1 root root 2406 Jul 26 19:21 anaplan-connector-393817-7f22b1ca301b.json – Error404 Jul 28 '23 at 19:17
0

Thanks all, however, I managed to resolve the issue by providing the filepath as : "spring.cloud.gcp.credentials.location=classpath:/service-key.json" in application.properties.

Error404
  • 25
  • 1
  • 7