Dhaval Bhoot
Dhaval Bhoot

Reputation: 251

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

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

Upvotes: 5

Views: 23693

Answers (4)

Mahmoud
Mahmoud

Reputation: 11459

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

Upvotes: 0

Anoop
Anoop

Reputation: 1495

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

Upvotes: 1

Steve C
Steve C

Reputation: 19445

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.

Upvotes: 2

Wee
Wee

Reputation: 483

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

Upvotes: 0

Related Questions