Java can't find file while running in Docker container

2.7k views Asked by At

I reference an HTML file in my code, and access it with:

Path filePath1 = Path.of("./email.html");

When I run the project locally, the project works fine, and the file loads normally. However, when running the project in a Docker container, I get the following error:

java.nio.file.NoSuchFileException: ./email.html

Here is my Docker file for reference

FROM openjdk:11.0-jdk-slim as builder

VOLUME /tmp
COPY . .
RUN apt-get update && apt-get install -y dos2unix
RUN dos2unix gradlew
RUN ./gradlew build

# Phase 2 - Build container with runtime only to use .jar file within
FROM openjdk:11.0-jre-slim
WORKDIR /app
# Copy .jar file (aka, builder)
COPY --from=builder build/libs/*.jar app.jar
ENTRYPOINT ["java", "-Xmx300m",  "-Xss512k", "-jar", "app.jar"]
EXPOSE 8080

Thank you for the answers. So this is a Java project, so there is no index.html to add. I tried changing the work directory to /src, but it is still not picking it up

2

There are 2 answers

0
GeertPt On BEST ANSWER

There's a couple of options:

  1. Copy the index.html in the docker image (solution by ~dominik-lovetinsky)
  2. Mount the directory with your index.html file as a volume in your docker instance.
  3. Include the index.html as a resource in your app.jar, and access it as a classpath resource.

The last option: including resources as classpath resource, is the normal way webapps work, but I'm not sure if it works for you.

0
Dominik Lovetinsky On

Docker has no access to the filesystem fromm the host OS. You need to put it in there as well:

COPY ./index.html index.html