Home > Mobile >  Jenkins with docker agent can't find JAVA_HOME when installing Maven
Jenkins with docker agent can't find JAVA_HOME when installing Maven

Time:10-08

I have a declarative pipeline:

agent {
    docker {
        image 'myimage:latest'
        reuseNode true
    }
    steps {
        sh "mvn clean install"
    }
}

It errors with:

The JAVA_HOME environment variable is not defined correctly

This environment variable is needed to run this program

NB: JAVA_HOME should point to a JDK not a JRE

This is a problem with Jenkins and how it treats docker containers, cause the same image outside of the Jenkins environment effectively does the maven cleanup and install command without problems.

I prefer not using external plugins, I would like to adopt a vanilla solution. Has anyone faced something similar?

Many thanks.

Java version used:

$ java -version

openjdk version "1.8.0_302"
OpenJDK Runtime Environment (IcedTea 3.20.0) (Alpine 8.302.08-r1)
OpenJDK 64-Bit Server VM (build 25.302-b08, mixed mode)

Maven version used (output from outside of the Jenkins environment):

$ mvn -v

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/share/java/maven-3
Java version: 1.8.0_302, vendor: IcedTea, runtime: /usr/lib/jvm/java-1.8-openjdk/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.10.104-linuxkit", arch: "amd64", family: "unix"

My dockerfile which builds the image myimage:

FROM amd64/alpine:3.14
RUN apk update
RUN apk add --no-cache openjdk8
RUN apk add --no-cache maven

CodePudding user response:

What worked in my case was that Jenkins controller (built-in node, master) passes the environment variables to the container... So the container inherits the whole env from the controller.

Hence the container is trying to use a JAVA_HOME that is not appropiate for the container.

Just unsetting the JAVA_HOME variable when building the container fixed the error.

agent {
    docker {
        image 'myimage:latest'
        reuseNode true
    }
    steps {
        sh "unset JAVA_HOME && mvn clean install"
    }
}
  • Related