Home > Net >  Java can't find file while running in Docker container
Java can't find file while running in Docker container

Time:05-03

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related