0

I have the following jenkinsfile which is giving the error below.

node {
    docker.withRegistry("https://001.dkr.ecr.eu-west-1.amazonaws.com/my-tooling") {
        docker.image('my-tooling:deploy-agent_0.0.3')
                .inside("-v /var/lib/jenkins/.ivy2:/home/jenkins/.ivy2", {
...
}

Error

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/my-tooling:deploy-agent_0.0.3/json: dial unix /var/run/docker.sock: connect: permission denied

I see lots of answers suggesting to run sudo usermod -aG docker $USER but as I'm not running this locally and don't have access to the vm, because it's all infra-as-code how can I add this to the jenkinsfile?

I've tried to add it like this,

node {
    try {
        stage('set permissions') {
            sh "sudo usermod -aG docker jenkins"
            //also tried "sudo chmod 777 /var/run/docker.sock"
        }
    } finally {
        echo 'Unable to set permissions'
    }
    ...
}

But I'm not able to run sudo as I get the error..

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things: 
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
sudo: no tty present and no askpass program specified
script returned exit code 1
Kaigo
  • 1,267
  • 2
  • 14
  • 33
  • Is docker installed on the machine ? And the docker plugin in jenkins ? How is installed jenkins (container or on VM) ? Because if you can't add the user in the docker group I don't know if its possible. Maybe try with `--privileged` or `-u root` in your`.inside()` – fmdaboville Sep 02 '21 at 14:47
  • Just tried `.inside("-v /var/lib/jenkins/.ivy2:/home/jenkins/.ivy2 --privileged")` and `.inside("-u root -v /var/lib/jenkins/.ivy2:/home/jenkins/.ivy2")` both gave same error. Thanks – Kaigo Sep 02 '21 at 14:57
  • And with both ? – fmdaboville Sep 02 '21 at 14:58
  • yep, same error. – Kaigo Sep 02 '21 at 15:17

1 Answers1

0

If you arrive to this question of stack overflow because you receive this message from docker, but you don't use jenkins, most probably the error is the same: your unprivileged user does not belong to the docker group.

You can do:

sudo usermod -a -G docker [user] Insert your user name where [user] is.

You can check it was successful by doing grep docker /etc/group and see something like this:

docker:x:998:[user] in one of the lines.

Then change your users group ID to docker:

newgrp docker Finally, log out and log in again

  • Where do I run `sudo usermod -aG docker jenkins`? You've just given me what I see in the other answers. – Kaigo Sep 02 '21 at 13:51
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 02 '21 at 19:30