Docker can't find generated jar file - java

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

Related

Build maven project inside docker container

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"]

ClassPath not found Spring Boot only on Docker

the java code below allows you to insert values in a java report to then create a pdf file, when I run the code below I have the following error: Classpath resource not found when running as jar, what is it due to and how I solve this problem?
java code:
ClassPathResource reportResource = new ClassPathResource("templates/printcantiere.jasper");
Map<String, Object> reportParameters = new HashMap<>();
JRBeanCollectionDataSource itemsJRBean = new JRBeanCollectionDataSource(tablevalues);
reportParameters.put("ItemDataSource", itemsJRBean);
reportParameters.put("RagioneSociale", c.getRagioneSociale());
reportParameters.put("Commessa", c.getCommessa());
reportParameters.put("NomeCantiere", c.getNomeCantiere());
reportParameters.put("Stato", c.getStatoCantiere());
docker.sh
#!/bin/bash
#Kill process on port
kill -9 $(lsof -ti:8081)
#delete old jar file and regenerate jar file
mvn clean package
rm -rf test/*.jar
mvn clean install
#generate new jar file mvn clean package
mvn eclipse:eclipse
#define docker name
dockername=quote
#Delete all containers and all volumes
#docker system prune -a
#Build of container and image
docker rm $dockername
docker build -t $dockername .
#Run container
docker run --publish 8081:8081 $dockername
Dockefile:
FROM adoptopenjdk/openjdk16
ARG JAR_FILE=target/report-0.0.1-SNAPSHOT.jar
WORKDIR /opt/app
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","app.jar"]
Pom file link

Dockerfile COPY file generated by RUN sh script

I want to automate the building and execution of a java spring web service by using Docker.
I run a script from the dockerfile that generate a settings.xml file. I want to copy that file in the image, but because RUN create a new container (I think?) the build can't find the generated file.
Here is the begining of my Dockerfile:
FROM ubuntu:20.04
WORKDIR /app
COPY . .
RUN ./script.sh
COPY settings.xml /root/.m2/
and there is the output...
How can I copy the settings.xml file that was generated by my sh script?
Thanks!
COPY copies from the host to the image. If you want to copy from the image to the image, you use the 'normal' cp command.
You're right that each RUN statement runs in a separate shell. But since your generated file is stored in the file system, it'll work if you do it in different RUN statements.
But let's do it in one RUN statement to only get 1 new layer in the image:
FROM ubuntu:20.04
WORKDIR /app
COPY . .
RUN ./script.sh && \
cp settings.xml /root/.m2/
You can also use mv to move the file if you don't need a copy to stay in /app.
By executing RUN script.sh the file will be generated inside the container. Thus it cannot be copied to the image using the COPY Dockerfile command. Instead you could mv the file inside the container to the desired destination:
FROM ubuntu:20.04
WORKDIR /app
COPY . .
RUN ./script.sh \
&& mv /path/to/settings.xml /root/.m2/

Docker how to recompile jar file with maven

I am new to docker, and I am trying to setup docker for spring boot project.
Here is my Dockerfile
FROM maven:3.6.3-jdk-11-slim AS build
WORKDIR usr/src/springboot
COPY . ./
RUN mvn install
RUN mvn clean package
#
# Package stage
#
FROM openjdk:11-jre-slim
ARG JAR_NAME="springboot-0.0.1-SNAPSHOT"
WORKDIR /usr/src/springboot
EXPOSE 8080
COPY --from=build /usr/src/springboot/target/${JAR_NAME}.jar ./springboot.jar
CMD ["java","-jar", "./springboot.jar"]
Which works completely fine and I can access hello world from localhost:8080
But my confusion is how to make any changes in java file reflect in the docker container? how do I recompile the .jar file.
I tried something like docker exec -it strange_shaw "mvn clean package"
But it throws error exec: "mvn clean package": executable file not found in $PATH: unknown
when you are using double FROM instruction inside your Dockerfile , Docker will keep only the latest FROM and use the previous FROM to build the next one.
So it is simple to find this error because FROM maven:3.6.3-jdk-11-slim AS build only used to build the next step FROM openjdk:11-jre-slim and will be removed from the final image(this strategy used to minimize the docker image size).
I hope that give you a clear idea about Dockerfile with multiple stage.
Build your jar outside after your Dockerfile will be like that:
FROM openjdk:11-jre-slim
ARG JAR_NAME="springboot-0.0.1-SNAPSHOT"
WORKDIR /usr/src/springboot
EXPOSE 8080
COPY /usr/src/springboot/target/${JAR_NAME}.jar ./springboot.jar
CMD ["java","-jar", "./springboot.jar"]
The issue was that maven was not installed within the container. So I changed my Dockerfile to
FROM openjdk:11
ARG JAR_NAME="springboot-0.0.1-SNAPSHOT"
WORKDIR /usr/src/springboot
EXPOSE 8080
#COPY --from=build /usr/src/springboot/target/${JAR_NAME}.jar ./springboot.jar
#CMD ["java","-jar", "./springboot.jar"]
RUN apt-get update; apt-get install curl -y
ARG MAVEN_VERSION=3.6.3
# 2- Define a constant with the working directory
ARG USER_HOME_DIR="/root"
# 4- Define the URL where maven can be downloaded from
ARG BASE_URL=https://apache.osuosl.org/maven/maven-3/${MAVEN_VERSION}/binaries
# 5- Create the directories, download maven, validate the download, install it, remove downloaded file and set links
RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
&& echo "Downloading maven" \
&& curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
\
&& echo "Unziping maven" \
&& tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
\
&& echo "Cleaning and setting links" \
&& rm -f /tmp/apache-maven.tar.gz \
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn
# 6- Define environmental variables required by Maven, like Maven_Home directory and where the maven repo is located
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"
This will install maven inside the container and set $PATH
docker run -it -v "$(pwd)":/usr/src/springboot -p 8080:8080 spring-boot-app
Run above command to start container and map your project folder to container folder and local port to container port. So both are in sync if you do any changes in project.
docker exec -it <container_name> mvn verify
Above command will create the .jar file. Restart the container.
docker exec -it <container_name> java -jar target/<file_name>.jar
Will execute the jar file in port 8080 and will be accessible in your localhost:8080

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