I am new to docker. I just tried to use docker in my local machine(Ubuntu 16.04) with Jenkins.
I configured a new job with below pipeline script.
node {
stage('Build') {
docker.image('maven:3.3.3').inside {
sh 'mvn --version'
}
}
}
But it fails with below error.
2019-02-16
Most of the steps were the same for me as the others has written. However, I was not able to add jenkins to the group docker using usermod with the mentioned solutions.
I tried the following command from the docker host, and from the running docker container:
(I entered to the running docker container with the following command from the docker host:
)
Received from docker host:
Received from docker container:
I didnt know the password.
Without the
sudo
part of the command, in the docker container I received:Solution: I entered to the running docker container from the docker host with the following command:
Now, I entered as root, and issued the following command:
Then, from the docker host, I restarted my running docker container with the following command:
After that, I started the jenkins job and it finished with success.
I only used the root user to issue the
usermod
command for the userjenkins
.My first solutions was:
But none of them work for me, I tried:
That works, but I don't know if it is the right call.
Success for me
On the server where Jenkins is running, I used
And then run each docker container with
Using setfacl seems a better option, and no "-u user" is needed. The containers then run as the same user that is running Jenkins. But I would appreciate any feedback from the security experts.
Simply adding
docker
as a supplementary group for thejenkins
useris not always enough when using a Docker image as the Jenkins Agent. That is, if your
Jenkinsfile
starts withpipeline{agent{dockerfile
orpipeline{agent{image
:This is because Jenkins performs a
docker run
command, which results in three problems.docker run
does not do a login to the container (it's more like asudo
).Installing Docker for the Agent
Making the Docker programs available within the Docker image simply requires running the Docker installation steps in your Dockerfile:
Sharing the Docker daemon socket
As has been said before, fixing the second problem means running the Jenkins Docker container so it shares the Docker daemon socket with the Docker daemon that is outside the container. So you need to tell Jenkins to run the Docker container with that sharing, thus:
Setting UIDs and GIDs
The ideal fix to the third problem would be set up supplementary groups for the Agent. That does not seem possible. The only fix I'm aware of is to run the Agent with the Jenkins UID and the Docker GID (the socket has group write permission and is owned by
root.docker
). But in general, you do not know what those IDs are (they were allocated when theuseradd ... jenkins
andgroupadd ... docker
ran when Jenkins and Docker were installed on the host). And you can not simply tell Jenkins to user userjenkins
and groupdocker
because that tells Docker to use the user and group that are named
jenkins
anddocker
within the image, and your Docker image probably does not have thejenkins
user and group, and even if it did there would be no guarantee it would have the same UID and GID as the host, and there is similarly no guarantee that thedocker
GID is the sameFortunately, Jenkins runs the
docker build
command for your Dockerfile in a script, so you can do some shell-script magic to pass through that information as Docker build arguments:That uses the
id
command to get the UID and GID of thejenkins
user and thestat
command to get information about the Docker socket.Your Dockerfile can use that information to setup a
jenkins
user anddocker
group for the Agent, usinggroupadd
,groupmod
anduseradd
: