I am trying to perform mvn install from Dockerfile but it is not working says mvn not found
Solution 1
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.
Solution 2
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
![Dhaval Bhoot](https://lh5.googleusercontent.com/-G0lIsfJhKII/AAAAAAAAAAI/AAAAAAAAAFY/j83Oj0V6sYw/photo.jpg?sz=256)
Dhaval Bhoot
Updated on June 16, 2022Comments
-
Dhaval Bhoot about 2 years
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