Build maven project inside docker container - java

I'm having some problems while trying to build a mvn project inside a Docker container. Everytime I try to build using the following Dockerfile, I get this error:
[10/10] COPY target/*.jar app.jar:
------
lstat /var/lib/docker/tmp/buildkit-mount9100795/target: no such file or directory
My current Dockerfile:
FROM maven:3.8.1-openjdk-17 as builder
COPY src /tmp/src/
COPY pom.xml /tmp/
WORKDIR /tmp/
RUN mvn clean install
FROM maven:3.8.1-openjdk-17
WORKDIR /tmp/app
COPY target/*.jar app.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","app.jar"]

I've found the correct configurations for the dockerfile and relative paths. Since I've set the WORKDIR for the buider as /tmp/ my target folder was being generated at /tmp/target/, so specifying the source files with the ---from=builder tag, and updating the path, I was able to succesfully copy the .jar file
My current working dockerfile:
FROM maven:3.8.1-openjdk-17 as builder
COPY src /tmp/src/
COPY pom.xml /tmp/
WORKDIR /tmp/
RUN mvn clean install
FROM maven:3.8.1-openjdk-17
COPY --from=builder /tmp/target/*.jar app.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","app.jar"]

Related

Docker : class path resource cannot be resolved for Spring Boot app

In my Spring Boot app, I read some csv file located in resources\data folder and I have the following Dockerfile:
FROM maven:3.8.2-openjdk-17-slim AS builder
# add pom.xml and source code
ADD ./pom.xml pom.xml
ADD ./src src/
# package jar
RUN mvn clean install
# Second stage: minimal runtime environment
FROM openjdk:17-alpine
# copy jar from the first stage
ARG JAR_FILE=target/*.jar
COPY --from=builder ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]
When I run the app after building the container, I get the following error:
"class path resource [data/employees.csv] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar!/BOOT-INF/classes!/data/employees.csv"
I think I need to add the resources\data folder to my container. So, how can I do this? What is the most proper way?
Note: I also tried to fix the problem using new ClassPathResource(filename).getInputStream(), but it did not make any sense and still the same problem.

Java can't find file while running in Docker container

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
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
There's a couple of options:
Copy the index.html in the docker image (solution by ~dominik-lovetinsky)
Mount the directory with your index.html file as a volume in your docker instance.
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.

Java -jar is not running the tests compiled in the container

I can't run the tests in maven install because the database service isn't up to date, so I want to run the test when I run the .jar but it's not working
#
# Build stage
#
FROM maven:3.8.3-openjdk-16 AS build
WORKDIR /app
COPY src /app/src
COPY pom.xml /app
RUN mvn -f /app/pom.xml clean package -DskipTests=true
RUN mvn test-compile
#
# Package stage
#
FROM openjdk:16
COPY --from=build /app/target/bank-api.jar /usr/local/lib/bank-api.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/lib/bank-api.jar"]

Docker can't find generated jar file

I can't figure out why Docker can't find the created jar file in the following Dockerfile:
FROM maven:latest
ENV APP_HOME=/app/
COPY pom.xml $APP_HOME
COPY src $APP_HOME/src/
WORKDIR $APP_HOME
RUN mvn package -DskipTests
ENV JAR_FILE=target/spring-boot-app-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} /app.jar
EXPOSE 8300
ENTRYPOINT ["java", "-jar", "/app.jar"]
when building the image with: docker build -t spring-boot-app ., it fails with:
Step 9/11 : COPY ${JAR_FILE} /app.jar
COPY failed: stat /var/lib/docker/tmp/docker-builder834657272/target/spring-boot-app-0.0.1-SNAPSHOT.jar: no such file or directory
If I run mvn clean package before building the image, it works. If remove target folder as rm -rf target and rebuild the image, it fails.
What am I missing?
You're trying to copy your locally built jar file to the image, but most probably you just want to move the jar file you build inside the image to another path?
Just replace the COPY ${JAR_FILE} /app.jar with RUN mv ${JAR_FILE} /app.jar

Unable to run 'RUN ./mvnw dependency:go-offline -B' when building docker image from "openjdk:8-jdk-alpine" for Spring Boot app

So I am trying to run a spring boot app with maven wrapper inside the docker container. Here is my Docker file:
### Stage 1: Build the application
FROM openjdk:8-jdk-alpine as build
RUN mkdir -p /app
#Set the current working directory inside the image
WORKDIR /app
#copy maven executable to the image
COPY mvnw .
COPY .mvn .mvn
#Copy the pom.xml file
COPY pom.xml .
#Build all the dependencies in preparation to go offline
#This is a separate step so the dependencies will be cached unless
#the pom.xml file has changed
RUN ./mvnw dependency:go-offline -B
#Copy the project source
COPY src src
#Package the application
RUN ./mvnw package -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
I have this error:
Step 7/16 : RUN ./mvnw dependency:go-offline -B
---> Running in 642a32f86392
/bin/sh: ./mvnw: not found
ERROR: Service 'app-server' failed to build: The command '/bin/sh -c ./mvnw dependency:go-offline -B' returned a non-zero code: 127
I am working with windows 10 pro. Please I need your help
Maybe a duplicate of Unable to run './mvnw clean install' when building docker image based on "openjdk:8-jdk-alpine" for Spring Boot app
Can you check the line endings of the mvnw shell script?
You could fix it by adding this before executing the mvnw command:
RUN dos2unix mvnw
Alternatively, if the file is in git, you can also fix it by adding the following to a .gitattributes file and checking the file out again:
*.bat text eol=crlf
mvnw text eol=lf
You have to copy the project files into the /app dir first. And you don't have the maven wrapper in the context folder where you run the docker build.
Try change the end of line mvnw file from Windows style CRLF to Unix style LF. Then rebuild the image.

Categories

Resources