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.
If you may get errors like below,
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
or
level=error msg="failed to dial gRPC: cannot connect to the Docker daemon. Is 'docker daemon' running on this host?: dial unix /var/run/docker.sock: connect: permission denied"
Just try to execute the following commands,
$ sudo su - jenkins
$ sudo usermod -a -G docker $USER
$ sudo chown jenkins:docker /var/run/docker.sock
In addition to adding the user to the docker group and trying everything mentioned in this thread, it took me a while to realize that I had to restart my terminal and then log back into the ec2 instance. It worked after that.
sudo setfacl --modify user:(user name or ID):rw /var/run/docker.sock
Several times I tried to execute the command
sudo chmod 777 /var/run/docker.sock
but unfortunately, I have to do this every time when I'm logging in to ubuntu system. It doesn't require a restart and is more secure than usermod or chown. user ID is required when the user name only exists inside the container, but not on the host.
I hope that it will help you solve the problem.
2018-08-19
I have been stuck for days on this one and as I haven't found a complete answer with the why and how, I will post one for other people that stumble on the same problem and answers from above do not work.
These are the 3 crucial steps when running Jenkins inside docker:
/var/run/docker.sock
to the jenkins container in order to be able to use the docker from the host.sudo usermod -a -G docker jenkins
in order to add jenkins to the docker group. However, here you might run into a permission problem if the host docker and the container docker don't have the same group id so it is very important to adjust the container docker's gid to be the same as the host docker gidYou can do this as a part of a launch script or simply by using exec
and doing it manually: groupmod -g <YOUR_HOST_DOCKER_GID> docker
.
Also, do not change permissions of the /var/run/docker.sock
to 777 or stuff like that because that is a big security risk, you are basically giving everyone permission to use docker on your machine
Hope this helps
sudo usermod -a -G docker jenkins
sudo service jenkins restart
If someone is still facing the issue on their local machine(Ubuntu) then try below command:
sudo chmod 666 /var/run/docker.sock