I am trying to perform mvn install from Dockerfile but it is not working says mvn not found - maven

I am trying to perform "mvn install" to create war file from Dockerfile. Bellow is the Dockerfile
FROM scratch
FROM ubuntu:16.04
RUN mkdir /opt/java8
RUN mkdir /opt/tomcat8
RUN mkdir /opt/maven3
ENV JAVA_HOME /opt/java8
ENV CATALINA_HOME /opt/tomcat8
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin:$M2_HOME/bin
ADD jdk1.8.0_112 /opt/java8
ADD apache-tomcat-8.0.38 /opt/tomcat8
WORKDIR /home/veni/git/M_UserTP
RUN mvn install
WORKDIR /home/veni/git/M_UserTP/target
RUN mv M_UserTP.war
/home/veni/Documents/dhaval_bhoot/docker_images/tomcat1
ADD M_UserTP.war /opt/tomcat8/webapps
EXPOSE 8080
CMD ["catalina.sh", "run"]
I also added the path of bin directory of maven in PATH environment variable.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/veni/Documents/apache-maven-3.3.9/bin/
This path I set from root user in my machine, I also added same path in PATH variable as normal user already.
So now I exit from root user and come back by sudo su to be root user and check PATH variable, it has not path of maven
So when I make docker build of image I get the bellow error
/bin/sh: 1: mvn: not found
The command '/bin/sh -c mvn install' returned a non-zero code: 127

Given that you want to run your application in a Tomcat 8 Docker container:
Your maven project should be laid out like:
M_UserTP
+ Dockerfile
+ pom.xml
+ src
+ target
This is a standard maven layout plus your Dockerfile.
Ensure that your pom.xml contains the following finalName defined in it:
<build>
<finalName>${project.artifactId}</finalName>
...
</build>
Your Dockerfile can be really simple:
FROM tomcat:8.0
COPY target/M_UserTP.war $CATALINA_HOME/webapps/
(Note how the finalName is used by the Dockerfile)
To build it, execute:
mvn clean install && docker build . -t Bhoot/M_UserTP
You can use what ever -t tag that you want.
It will take some time the first time that you do this while the standard Tomcat 8 image is downloaded.
Now you can run it:
docker run --detach --publish 8080:8080 Bhoot/M_UserTP
You don't really want to build your WAR file in the docker image. This will suck all the maven repository components used by maven to build your application into the image. This space is not recoverable as images will only ever grow - they never shrink again.

Have you considered using a maven:onbuild image in conjunction with a multi-stage build for this?
An example of such a usage (with a Spring Boot application) is available here: https://github.com/anokun7/docker-springframework/blob/master/Dockerfile

Your container have its own filesystem.
So the mvn binary available on your system, is not available on a base ubuntu image.
You should install it first in your container (with a RUN apt-get install..)
You will then be able to use it in the next RUN

Try getting inside your container (after commenting out maven layer and everything below) and type which mvn. This will display the absolute path of the maven binary. Then use that absolute path inside your Dockerfile instead of just mvn.
Example:
mvn -> /opt/maven/bin/mvn

Related

Unable to create .exe using GraalVM docker image in Windows

In Windows 10 environment, with IntelliJ Idea, I have a sample Spring Boot application created with bootify, called Bootifytwo, which is located in the C:\CODIGO\IDEA_PROJECTS\bootifytwo folder.
In the pom.xml of said application I have the following dependency configured:
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
My intention is, using GraalVM, to get to generate the bootifytwo.exe and be able to run it correctly.
To do this, I have downloaded the following Docker image with GraalVM from the Oracle repositories; and performed the following commands:
docker pull container-registry.oracle.com/graalvm/community:ol8-java17-22.3.0-b1
docker images
docker run -it --rm container-registry.oracle.com/graalvm/community:ol8-java17-22.3.0-b1 bash
java -version
gu install native-image
native-image --version
Everything is working fine until I try to change to my project folder.
As long as I can´t change to my project folder (cd C:\CODIGO\IDEA_PROJECTS\bootifytwo) I can't create the .exe with the following:
.\mvnw native:compile -Pnative
And finally locate myself in the target folder (cd C:\CODIGO\IDEA_PROJECTS\bootifytwo\target) and launch the desired bootifytwo.exe
I would appreciate help in this complex path. (I don't know if I need to define the GRAALVM_HOME variable, or map a volume...)
The solution to generate the .exe was:
Copy the app folder to (future) volume destination:
In my case:
C:\CODIGO\IDEA_PROJECTS\bootifytwo to C:\Volumenes-Docker\vol-graalvm-one\bootifytwo
Then launch the following commands:
docker run --name container-graalvm-one -v "C:\Volumenes-Docker\vol-graalvm-one:/app" -it container-registry.oracle.com/graalvm/community:ol8-java17-22.3.0-b1 bash
gu install native-image
cd bootifytwo
. ./mvnw native:compile -Pnative
Then, I was able to launch the app with:
./bootifytwo
But fails in the connection with my DB (that also was "inside" another docker/postgres container)

How to do maven build and run make command inside container

I need to run Maven build inside container and afterwards I need to run make command.
For executing Maven, I did the following inside dockerfile
FROM maven:3.8.1-adoptopenjdk-11 AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package
After maven execution, some jar files are generated, which is needed to run make command (used in make file)
Now my question is how to get docker image for make, and how do I use it in same dockerfile? I tried to find docker image for make but didn't get it directly in dockerhub.
I'm familiar with building java code but not with building C/C++ code with make. Any help would be appreciated.

Unable to run './mvnw clean install' when building docker image based on "openjdk:8-jdk-alpine" for Spring Boot app

I would like to build a Spring Boot app using the maven wrapper provided by spring.io.starter inside a docker container.
My Dockerfile is:
FROM openjdk:8-jdk-alpine
# install bash --> commented because I just need /bin/sh which is already provided by the base image
#RUN apk add --no-cache bash
ENV APP_DIR /app
ENV APP app.jar
WORKDIR ${APP_DIR}
COPY . ${APP_DIR}
# ===> HERE THE PROBLEM
RUN /bin/sh -c "./mvnw clean install"
ENTRYPOINT ["java","-jar","chicowa-app.jar"]
EXPOSE 8080
I have this error:
/bin/sh: ./mvnw: not found
After making some researches I still don't find a solution.
My docker version
Docker version 18.06.1-ce, build e68fc7a on Windows10 pro
Thanks a lot for your help.
EDIT
A solution would be to install maven directly with a RUN apk add --no-cache maven but I would like to minimize as possible the image' size.
We were experimenting this issue only on Windows 10 and were able to solve this issue on just applying dos2unix
dos2unix mvnw
I have the same issue when build Dockerfile in Window 10.
This because mvnw file contains CRLF (\r\n) at the end of line. I open it with Notepad++ and use Find \r\n and Replace with \n (with Extended (\n, \r, ...) checked). You can use dos2unix tool to convert as #Juanan said above.
After that, the build run with no problem.
Unless you are running the script in the exact location that the file mvnw is located, it won’t work.
If your PATH is set correct, and Maven was installed in a suitable location, simply removing the “./“ in front of the command will suffice.
RUN /bin/sh -c “mvnw clean install”
If mvnw is NOT in your PATH, you can specify the full path in your script (but recognize that Maven is likely to call other things that would want the same PATH changes).
if you have already maven configured then you may run the following:-
mvn clean install
It will build the docker image seamlessly.
If you are using windows git may change the line ending of the mvnw file to CRLF. If you open the file with any text editor and save it to LF it will probably work.
use
RUN chmod +x mvnw
before using
./mvnw clean install
it'll make your mvnw file executable
The problem can also be fixed without bothered with line endings or path variables even on Windows10.
Just include Maven to your Dockerfile and run the maven commands via this:
Here is an example Dockerfile:
FROM maven:3.8.1-openjdk-17-slim AS builder # <-- Include Maven
WORKDIR /app
COPY pom.xml ./
COPY src ./src
RUN mvn clean install
# Second stage: Minimal runtime environment
FROM eclipse-temurin:17-jre-jammy
WORKDIR /app
# copy jar from the first stage
COPY --from=builder /app/target/*.jar /app/app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
After using this approach, I solved this problem. Hope this helps...
This is working solution as I don't find anywhere this solution:
mvnw -version
error:
java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be
non-empty
Root Cause:
maven public certificate is missing in java trust store
Solutions:
download - repo.maven.apache.org leaf certificate from https://repo.maven.apache.org and save it as repo_maven.cer file in base64 format
Install the repo_maven.cer into to cacerts (truststore)
keytool -import -keystore "...\openjdk-8u41\jre\lib\security\cacerts" -file "repo_maven.cer" -alias maven
Output:
mvnw -version
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)

Docker - misunderstanding about the execution and volume

I have a following Dockerfile in my pet project:
FROM java:8
ADD target/sources-registry-0.0.1-SNAPSHOT.jar sources-registry.jar
RUN bash -c 'touch /sources-registry.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/sources-registry.jar"]
EXPOSE 8761
And everything perfectly works - image is created and can be run. Now a bit of description about the project: it is a maven based project and before actually executing the Dockerfile I have to manually run mvn package.
However, if I change the Dokerfile to (because I do not want manually to run mvn package and want to automate it)
FROM java:8
RUN ls target
RUN ./mvnw package
ADD target/sources-registry-0.0.1-SNAPSHOT.jar sources-registry.jar
RUN bash -c 'touch /sources-registry.jar'
#ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/sources-registry.jar"]
EXPOSE 8761
then while execution I get /bin/sh: 1: ./mvnw: not found. However, mvnw is in my project files near the pom.xml.
Even more if I just do following Dockerfile
FROM java:8
RUN ls target/
then I get ls: cannot access target/: No such file or directory.
Can someone please explain this behaviour?
I mean why I can actually do something with target folder (first
Dockerfile) even if does not exist (third Dockerfile)?
How project files (and what files) get copied into a created
container?
The main question: Why second Dockerfile is not working? And how I can make it work?
The ADD command copies over the sources-registry.jar file into the Docker image, so that the first example is able to execute it. If you want to use any other files inside the container, you need to include them in the image as well (using ADD or COPY). See the reference docs for more information

Do not download all Maven dependencies on a Docker build

I'm trying to create a Dockerfile to then build a Maven project.
I wonder how to fix the Dockerfile and what command to then execute.
I would like to know how to run the build so that it does NOT download all the Maven dependencies every time it builds when the source code, sitting in the src/ directory, has NOT changed.
Here is my Dockerfile file:
FROM maven:3.3.9-jdk-8
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
RUN cd /usr/src/app
ADD pom.xml /usr/src/app
RUN mvn dependency:resolve
ADD src /usr/src/app
RUN mvn package
ENTRYPOINT ["mvn"]
CMD ["package"]
Should I run the docker run --rm -it toolbox command or the docker build -t toolbox . command ?
Both of these above commands run fine, except that they both download all the Maven dependencies even if the source code has not been touched.
That's how Docker works. Every time you do docker run, it creates a new container which does not have any access to the files in the old container. So, it download all dependencies it requires. You can circumvent this by declaring an external volume. Looking at the Dockerfile of Maven, it declares a volume /root/.m2. So, you can use a directory in your host machine and attach it to this volume by -v option. Your Docker command would be,
`docker run -v <directory-in-your-host>:/root/.m2 <other-options-and-commands>
Every time you run a new docker run, Maven will look into your local directory before downloading the dependency.
However, my question is why don't you build your app first and use the resulting jar to create the docker images unless you have any specific reasons. You can create your own Dockerfile using java base image or simply use one of the docker-maven-plugin like spotify available out there. That makes your life a lot easier.

Resources