Nagendra Nigade
Nagendra Nigade

Reputation: 876

FileNotFoundException while running docker image

I have configured a Firebase properties file as follows -

@Bean
Firestore firestore() throws IOException {
    InputStream serviceAccount = new FileInputStream("my-karwaan-firebase-adminsdk.json");
    GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);

JSON file is present under the resources folder.

Everything works fine on the localhost. But on docker it's throwing while running docker image

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[com.google.cloud.firestore.Firestore]: Factory method 'firestore' threw exception; nested
 exception is java.io.FileNotFoundException: my-karwaan-firebase-adminsdk.json

Docker file content as follows -

# Build Jar File
FROM maven:3.6.3-jdk-8-slim as stage1
WORKDIR /home/app
COPY . /home/app
RUN mvn -f /home/app/pom.xml clean package

# Create an Image
FROM openjdk:8-jdk-alpine
EXPOSE 8080
COPY --from=stage1 /home/app/target/myapp.jar myapp.jar
ENTRYPOINT ["java", "-jar", "myapp.jar"]

I tried to give an absolute path but it's not working on docker.

Directory structure as follows - enter image description here

Upvotes: 0

Views: 2582

Answers (1)

Dupinder Singh
Dupinder Singh

Reputation: 7759

See the basic problem in your dockerfile is

You are setting WORKDIR then while COPY you said COPY in /home/app so if you set WORKDIR then it means Any RUN, CMD, ADD, COPY, or ENTRYPOINT command will be executed in the specified working directory.

enter image description here

So to avoid the confusions try as following

# Build Jar File
FROM maven:3.6.3-jdk-8-slim as stage1
WORKDIR /home/app
#COPY FROM PROJECT ROOT DIR TO WORKING ROOT DIR i.e: /home/app 
COPY . .
RUN mvn -f /home/app/pom.xml clean package

# Create an Image
FROM openjdk:8-jdk-alpine
EXPOSE 8080
COPY --from=stage1 /home/app/target/myapp.jar myapp.jar
ENTRYPOINT ["java", "-jar", "myapp.jar"]

Try this :)

PS: Once you start the container please do exec and check is all the project details are at right directory or not and mvn build is building jar with correct name or not

Upvotes: 1

Related Questions