Docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

后端 未结 30 1567
孤独总比滥情好
孤独总比滥情好 2020-11-27 09:00

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.



        
相关标签:
30条回答
  • 2020-11-27 09:53

    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
    
    0 讨论(0)
  • 2020-11-27 09:54

    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.

    0 讨论(0)
  • 2020-11-27 09:55

    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.

    0 讨论(0)
  • 2020-11-27 09:56

    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:

    1. You mount the socket /var/run/docker.sock to the jenkins container in order to be able to use the docker from the host.
    2. You have to install docker inside the container in order to use it. This is a great and simple article on how to do that. Note that newer versions might already have docker installed
    3. You run 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 gid

    You 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

    0 讨论(0)
  • 2020-11-27 09:58
    sudo usermod -a -G docker jenkins
    sudo service jenkins restart
    
    0 讨论(0)
  • 2020-11-27 09:58

    If someone is still facing the issue on their local machine(Ubuntu) then try below command:

    sudo chmod 666 /var/run/docker.sock
    
    0 讨论(0)
提交回复
热议问题