Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve Docker permission error when trigger by Jenkins

Tags:

docker

jenkins

My Jenkins is not run in Docker container, just tradional install to VPS. I got the following error when executing a simple test project. I am using Ubuntu 14, java 7, and stable Jenkins. I tried all methods I can find on google, but can't get it work.

I am trying to execute this shell

docker build --pull=true -t nick/hello-jenkins:$GIT_COMMIT . 

After code change.

Here is error:

Got permission denied while trying to connect to the Docker daemon socket at unix: .... 

Started by user nicolas xu Building in workspace /var/lib/jenkins/workspace/hello-Jenkins  > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository  > git config remote.origin.url https://github.com/nicolasxu/hello-nick-jenkins.git # timeout=10 Fetching upstream changes from https://github.com/nicolasxu/hello-nick-jenkins.git  > git --version # timeout=10  > git fetch --tags --progress https://github.com/nicolasxu/hello-nick-jenkins.git +refs/heads/*:refs/remotes/origin/*  > git rev-parse refs/remotes/origin/master^{commit} # timeout=10  > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision d94ae21a8a2cf58ffc790dcad15bd851fb17df5a (refs/remotes/origin/master)  > git config core.sparsecheckout # timeout=10  > git checkout -f d94ae21a8a2cf58ffc790dcad15bd851fb17df5a  > git rev-list d94ae21a8a2cf58ffc790dcad15bd851fb17df5a # timeout=10 [hello-Jenkins] $ /bin/sh -xe /tmp/hudson5076309502904684976.sh + docker build --pull=true -t nick/hello-jenkins:d94ae21a8a2cf58ffc790dcad15bd851fb17df5a . Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.27/build?buildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&pull=1&rm=1&shmsize=0&t=nick%2Fhello-jenkins%3Ad94ae21a8a2cf58ffc790dcad15bd851fb17df5a&ulimits=null: dial unix /var/run/docker.sock: connect: permission denied Build step 'Execute shell' marked build as failure Finished: FAILURE 

I can run 'docker' in console as root no problem, why jenkins can't try a shell command which runs 'docker'? What is going on? Totally confused.......

like image 455
Nicolas S.Xu Avatar asked Jun 08 '17 19:06

Nicolas S.Xu


People also ask

How do I fix permission denied Docker?

If running elevated Docker commands does not fix the permission denied error, verify that your Docker Engine is running. Similar to running a docker command without the sudo command, a stopped Docker Engine triggers the permission denied error. How do you fix the error? By restarting your Docker engine.

How do I fix Permission denied while trying to connect to the Docker daemon socket at Unix?

Fix 1: Run all the docker commands with sudo If you have sudo access on your system, you may run each docker command with sudo and you won't see this 'Got permission denied while trying to connect to the Docker daemon socket' anymore.

How do Docker and Jenkins work together?

Jenkins builds a new docker image and pushes it to the Docker registry. Jenkins notifies Kubernetes of the new image available for deployment. Kubernetes pulls the new docker image from the docker registry. Kubernetes deploys and manages the docker instance/container.


1 Answers

In your VPS server terminal, do this to add your jenkins user to the docker group:

sudo usermod -aG docker jenkins 

Then restart your jenkins server to refresh the group.

Take into account any security issue that this could produce:

Warning: The docker group grants privileges equivalent to the root user. For details on how this impacts security in your system, see Docker Daemon Attack Surface.

Refer to the docs


Edit (mentioned by @iger): Just make sure to restart the Jenkins from command-line (i.e. sudo service jenkins restart), but not through the rest endpoint (http:///restart)

like image 116
Robert Avatar answered Oct 08 '22 17:10

Robert