Rudziankoŭ
Rudziankoŭ

Reputation: 11251

Make Jenkins run docker without sudo

I would like to run Docker shell commands on Jenkins like:

docker ps

Is it possible to do it with out using any plugins? Since Jenkins isn't a user, but a service account how can I add to docker group?

Upvotes: 13

Views: 31650

Answers (4)

Sudhakar MNSR
Sudhakar MNSR

Reputation: 764

I had the issue when I was running from jenkins pipeline. I added jenkins user to docker group, restarted the docker engine and rebooted the machine as well. However I still get the same error dial unix /var/run/docker.sock: connect: permission denied.

Finally I added jenkins to root group and it resolved my issue (ubuntu 18.04) (VM on Azure)

sudo gpasswd -a jenkins root
sudo service docker restart

Upvotes: 3

Javier Rodriguez
Javier Rodriguez

Reputation: 986

First execute

sudo groupadd docker

Then execute

sudo usermod -aG docker $USER

Then logout its important to logout because your group membership is re-evaluated

Login and try again

docker ps

It works!

Upvotes: 20

vishnu
vishnu

Reputation: 2830

Following approach worked for me to run docker commands without any plugins

Rather than adding jenkins user to docker group, allowed jenkins user to run sudo commands with out prompting for password and then created an alias to avoid sudo in Dockerfile for jenkins slave. I had to install docker client in the container which connects to daemon running in the host machine.


 ## allowing jenkins user to run sudo commands
RUN echo "jenkins ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
 ## avoid typing sudo in command line
RUN echo "alias docker='sudo docker '" >> /home/jenkins/.bashrc

Upvotes: 5

veuncent
veuncent

Reputation: 1712

(Taken from this answer: https://askubuntu.com/a/477554)

If you run on Ubuntu and Jenkins runs directly on the host machine (i.e. not inside a Docker container):

Add the docker group if it doesn't already exist:

sudo groupadd docker

Add the user "jenkins" to the docker group:

sudo gpasswd -a jenkins docker

Restart the Docker daemon:

sudo service docker restart

Either do a newgrp docker or log out/in to activate the changes to groups.

Upvotes: 4

Related Questions