0

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

Yaxet
  • 23
  • 4
  • Is your file located at `/app/email.html`? You set the `WORKDIR` to `/app` in your `Dockerfile`, so any relative file references in your code should be relative to `/app`. – mpriscella May 02 '22 at 04:30
  • the file is in the root directory. I tried putting it in the src folder, and also just commenting out the WORKDIR line (so it stays at the root level), but neither worked – Yaxet May 03 '22 at 03:52

2 Answers2

3

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
0

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.

GeertPt
  • 16,398
  • 2
  • 37
  • 61