Reputation: 3951
I want to run the Jenkins pipeline inside a docker container, like running all the tests, doing sonar scanning, etc.
This is what my pipeline looks like.
pipeline {
agent {
docker {
image 'node:10-alpine'
args '-u root:root'
}
}
stages {
stage('Checkout') {
steps {
sh 'node --version; cat /proc/1/cgroup; touch /root/my_file'
checkout(...)
}
}
stage('Run Unit Tests') {
steps {
sh '''
npm install --unsafe-perm
'''
}
}
}
}
Now as an output of this, I see following logs in the console
+ cat /proc/1/cgroup
...
2:freezer:/docker/eeea802143ce0f1b4047650956e58624a0c38a5fbc601f189755a381dcf9c271
1:name=systemd:/docker/eeea802143ce0f1b4047650956e58624a0c38a5fbc601f189755a381dcf9c271
Which means the command runs in the docker container. Also, I can see /root/my_file
does not exist on the host.
Now, further, I see following output
+ npm install --unsafe-perm
> [email protected] install /home/jenkins/mount-dir/workspace/test-docker-pipeline/api/node_modules/libpq
> node-gyp rebuild
Now, here I see that the log mentions a directory that is present on the host.
I'm now confused, where my pipeline is executing, inside container or insider Jenkins? If either of the case, why I am seeing the contradictory log statement?
Upvotes: 1
Views: 1278
Reputation: 989
All your commands are run inside the container as you expect. So everything is fine.
What Jenkins is doing is to bind-mount the Workspace folder at the same location inside the Container. If you check your jobs console Output you should find something like:
Docker 1 does not seem to be running inside a container
$ docker run -t -d -u 1002:1002 -w /home/jenkins/build/workspace/<my-project> -v /home/jenkins/build/workspace/<my-project>:/home/jenkins/build/workspace/<my-project>:rw,z -v /home/jenkins/build/workspace/<my-project>@tmp:/home/jenkins/build/workspace/<my-project>@tmp:rw,z $ docker top 8948be0b380b033f20cd8c27647b88719b47a237f839513ebf66a9693b4c2975 -eo pid,comm
Running in /home/jenkins/build/workspace/<my-project>
The -v
option is doing the bind-mount of your Workspace Folder from your host into the same location inside the running container. For more information on Docker's bind-mlunt capability please see the official documentation at: https://docs.docker.com/storage/bind-mounts/
Edit: Since Pipeline 2.5 Jenkins Pipeline has native support for Docker Images. Therefor no Docker Pipeline Plugin needed anymore.
Upvotes: 1
Reputation: 3951
After carefully looking at the logs of the pipeline job I saw this line
$ docker run -t -d -u 1001:1001 -u root:root -w /home/jenkins/mount-dir/workspace/test-docker-pipeline -v /home/jenkins/mount-dir/workspace/test-docker-pipeline:/home/jenkins/mount-dir/workspace/test-docker-pipeline:rw,z -v /home/jenkins/mount-dir/workspace/test-docker-pipeline@tmp:/home/jenkins/mount-dir/workspace/test-docker-pipeline@tmp:rw,
Which is a docker run
command used by Jenkins. Here Jenkins mounts the directory in the exactly same path. So when we see reference /home/jenkins/mount-dir/...
in logs it's indeed run in Docker container but actual storage is mounted on the Jenkins host.
Upvotes: 0