Issue
I am hoping someone can provide me some guidance here:
I'm using jenkins (2.289.3) as a docker container to run some CI jobs in my macbook Monterey. Here is my docker command
docker run -d \
-u root \
--name jenkins \
-p 9080:8080 \
-v ./jenkins_home:/var/jenkins_home \
-v "$HOME":/home \
-v /var/run/docker.sock:/var/run/docker.sock \
jenkins
I'm using Jenkinsfile to build a project. Below is the code:
#!/usr/bin/env groovy
node {
def workspace = pwd()
stage('checkout') {
checkout scm
sh "echo checkout" ## this works fine!
}
docker.image('openjdk:17-jdk-alpine').inside('-u root -e MAVEN_OPTS="-Duser.home=./my-service/"') {
stage('check java') {
sh "java -version" ## this fails!
}
stage('artifact build and deploy') {
configFileProvider([configFile(fileId: 'gcRepositoryCred', variable: 'GC_MVN_REPOSITORY_CREDENTIAL'),configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
sh "chmod +x my-service/mvnw"
sh "./my-service/mvnw -f my-service/pom.xml clean deploy -DskipTests -s $MAVEN_SETTINGS"
}
}
}
}
It fails at line sh "java -version"
inside the open jdk container. Basically it hangs for a while and I get the below error code:
[Pipeline] sh
+ echo checkout
checkout
[Pipeline] }
[Pipeline] // stage
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . openjdk:17-jdk-alpine
.
[Pipeline] withDockerContainer
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 0:0 -u root -e MAVEN_OPTS=-Duser.home=./my-service/ -w /var/jenkins_home/workspace/my-service -v /var/jenkins_home/workspace/my-service:/var/jenkins_home/workspace/my-service:rw,z -v /var/jenkins_home/workspace/my-service@tmp:/var/jenkins_home/workspace/my-service@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** openjdk:17-jdk-alpine cat
$ docker top 7413692ee7a423413e80909000e1e94adc2c880016f32071c6ab9d47645092cf -eo pid,comm
[Pipeline] {
[Pipeline] stage
[Pipeline] { (check java)
[Pipeline] sh
process apparently never started in /var/jenkins_home/workspace/my-service@tmp/durable-25c4f40f
(running Jenkins temporarily with -Dorg.jenkinsci.plugins.durabletask.BourneShellScript.LAUNCH_DIAGNOSTICS=true might make the problem clearer)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
$ docker stop --time=1 7413692ee7a423413e80909000e1e94adc2c880016f32071c6ab9d47645092cf
$ docker rm -f 7413692ee7a423413e80909000e1e94adc2c880016f32071c6ab9d47645092cf
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code -2
Finished: FAILURE
Update The same code works in my another laptop (Mac running Big Sur). The main difference I see is the docker version. Big Sur mac has version 20.10.8 and the machine where it doesn't work has the docker version of 20.10.12.
The another key difference is the log from the working mac. Instead of logging Jenkins does not seem to be running inside a container
it says Jenkins is running inside a container
. Not sure if they both are related in some way.
Solution
I got it working with the following instructions: https://www.jenkins.io/doc/book/installing/docker/
Looks like with the new updates, I need to use docker:dind container to run docker commands and create my own Dockerfile with the provided instructions in the link above.
Answered By - Sameer Malhotra
Answer Checked By - Robin (JavaFixing Admin)