Pod Template and Custom Agent Images

Jenkins setup:

Version 2.464

I’ve created a custom jenkins docker image and when I build it, I add openjdk8 so my old jobs can work.

In my dockerfile:
FROM jenkins/inbound-agent:latest-alpine3.20-jdk17
RUN apk add openjdk8

When I build and exec into the container on my local build machine:

/home/jenkins # java -version
openjdk version "17.0.11" 2024-04-16
OpenJDK Runtime Environment Temurin-17.0.11+9 (build 17.0.11+9)
OpenJDK 64-Bit Server VM Temurin-17.0.11+9 (build 17.0.11+9, mixed mode)
/home/jenkins # /usr/lib/jvm/java-1.8-openjdk/bin/java -version
openjdk version "1.8.0_402"
OpenJDK Runtime Environment (IcedTea 3.30.0) (Alpine 8.402.06-r0)
OpenJDK 64-Bit Server VM (build 25.402-b06, mixed mode)

When I call that image from my pod template in a simple script:

podTemplate {
    node('jdk8') {
        stage('Run shell') {
            sh 'cd /usr/lib/jvm/java-1.8-openjdk/bin/ && ./java -version'
            sh 'sleep 1000'
        }
    }
}

+ cd /usr/lib/jvm/java-1.8-openjdk/bin/
+ ./java -version
openjdk version "17.0.11" 2024-04-16
OpenJDK Runtime Environment Temurin-17.0.11+9 (build 17.0.11+9)
OpenJDK 64-Bit Server VM Temurin-17.0.11+9 (build 17.0.11+9, mixed mode)

So…WHY? How would that happen? Why isn’t that behaving the same as the docker run?

I know I can use pod templates and use dedicated containers to run the job with the container command, but the jobs I’m transferring from an old jenkins version only have GUI build params, not groovy, so i need to accomodate these folks. I thought I could simply use the jnlp container name trick and it works, but for some reason, it isn’t recongizing JDK8, only version 17.

Hello and welcome to this community, @goobysnack. :wave:

That behaviour looks strange to me, but just to be sure, shouldn’t you set these two environment variables?

podTemplate(
    containers: [
        containerTemplate(
            name: 'jnlp',
            image: 'your-custom-jenkins-agent-image:latest',
            envVars: [
                envVar(key: 'JAVA_HOME', value: '/usr/lib/jvm/java-1.8-openjdk'),
                envVar(key: 'PATH', value: '/usr/lib/jvm/java-1.8-openjdk/bin:$PATH')
            ]
        )
    ],
    // Define any other necessary pod template configuration here
) {
    node('jdk8') {
        stage('Run shell') {
            sh 'java -version'
            sh 'sleep 1000'
        }
    }
}

This is a problem you see with alpine images and java 17 or java 21. See Remoting Agent with Java 17 & Alpine causing incorrect Java version to be picked up - #2 by mawinter69 how to resolve the problem

This happens regardless of the container OS. I can replicate this using debian as well. Looking at that solve though to see if works. Thank YOU!

The problem is that you use an alpine image with musl.
Please try the following:

podTemplate {
    node('jdk8') {
        stage('Run shell') {
            sh '''
unset LD_LIBRARY_PATH
cd /usr/lib/jvm/java-1.8-openjdk/bin/ && ./java -version
'''
            sh 'sleep 1000'
        }
    }
}

That worked! I wish I could do that in the podtemplate itself. Thanks!