Here's the situation:
Have a docker container (jenkins). I've mounted the sockets to my container so that I can perform docker commands inside my jenkins container. Everything is setup correctly, and I have a Jenkins slave that is itself an Ubuntu docker container.
I'd like to run a command inside a separate docker container created by the Jenkinsfile. The Docker Pipeline plugin promises to offer a solution to this common requirement.
Here's my Jenkinsfile:
pipeline {
agent any
stages {
stage('Test') {
agent {
docker { image 'maven:3-alpine' }
}
steps {
sh 'mvn --version'
}
}
}
}
Running the above, where the slave jenkins node is not inside in docker container works as expected. It prints:
- mvn --version Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T19:39:06Z) Maven home: /usr/share/maven Java version: 1.8.0_131, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-1.8-openjdk/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "5.4.8-gentoo", arch: "amd64", family: "unix"
It also works when I run the above on the Jenkins Master (which is itself a docker container).
However, when the slave is running (as an ubuntu docker container), I get the following error. Fix-Jenkins-Local
is the name of the github repo (branch) containing the Jenkinsfile:
Slave 1 seems to be running inside container 5cf4397c3abff105818b2ab92e275fa8265bd491170f47723a8e2cef9b308a3b but /tmp/jenkins_workspace/workspace/Fix-Jenkins-Local@2 could not be found among [] but /tmp/jenkins_workspace/workspace/Fix-Jenkins-Local@2@tmp could not be found among [] $ docker run -t -d -u 1000:1000 -w /tmp/jenkins_workspace/workspace/Fix-Jenkins-Local@2 -v /tmp/jenkins_workspace/workspace/Fix-Jenkins-Local@2:/tmp/jenkins_workspace/workspace/Fix-Jenkins-Local@2:rw,z -v /tmp/jenkins_workspace/workspace/Fix-Jenkins-Local@2@tmp:/tmp/jenkins_workspace/workspace/Fix-Jenkins-Local@2@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 ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** maven:3-alpine cat $ docker top 977b4035f431b617eab29e6b8292bc66fc71e3fed82af4788d89d6f9b803c4c7 -eo pid,comm [Pipeline] { [Pipeline] sh process apparently never started in /tmp/jenkins_workspace/workspace/Fix-Jenkins-Local@2@tmp/durable-63950950 (running Jenkins temporarily with -Dorg.jenkinsci.plugins.durabletask.BourneShellScript.LAUNCH_DIAGNOSTICS=true might make the problem clearer)
Anyone have any experience attempting to run docker pipeline plugin inside a jenkins docker container? Any thoughts much appreciated.
Another approach, I've tried without using Docker Pipeline plugin here
Reading another SOF question here, it appears you have to to be mindful of the following:
For inside to work, the Docker server and the Jenkins agent must use the same filesystem, so that the workspace can be mounted. The easiest way to ensure this is for the Docker server to be running on localhost (the same computer as the agent). Currently neither the Jenkins plugin nor the Docker CLI will automatically detect the case that the server is running remotely; When Jenkins can detect that the agent is itself running inside a Docker container, it will automatically pass the --volumes-from argument to the inside container, ensuring that it can share a workspace with the agent.
Here are two screenshots, when I run the job successfully on the Jenkins master (a docker container), and unsuccessfully on the slave (also, a docker container):
Master:
Slave: