Got permission denied while trying to connect to t

2020-01-27 00:01发布

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.

node {
    stage('Build') {
      docker.image('maven:3.3.3').inside {
        sh 'mvn --version'
      }
    }
}

But it fails with below error.

enter image description here

20条回答
相关推荐>>
2楼-- · 2020-01-27 00:24

I`m using the official jenkins docker image (https://hub.docker.com/r/jenkins/jenkins) but I think this solution is applicable to most use cases where we want to run Docker inside a Docker container.

The recommended way for using Docker inside a Docker container, is to use the Docker deamon of the host system. Good article regarding that: https://itnext.io/docker-in-docker-521958d34efd.

The secret to handle the permission issue, which this question is about, is to add permissions for the user of the container inside the container, not the host system. Only root user has permissions to do that by default, so

docker exec -it -u root <container-name> bash
usermod -a -G docker <username>

will do it. Remember to restart the container.

I guess the simpliest way to achive this is to create a customised Dockerfile:

# Official jenkins image
FROM jenkins/jenkins:lts
# Swith to root to be able to install Docker and modify permissions
USER root
RUN apt-get update
# Install docker
RUN curl -sSL https://get.docker.com/ | sh
# Add jenkins user to docker group
RUN usermod -a -G docker jenkins
# Switch back to default user
USER jenkins

# Bild the image:
# sudo docker build -t yourusername/imagename .
# Run the image and mount with the followin bind mount option:
# sudo docker run --name imagename -d -p8080:8080 -v /var/run/docker.sock:/var/run/docker.sock yourusername/imagename
查看更多
Explosion°爆炸
3楼-- · 2020-01-27 00:25

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

查看更多
劫难
4楼-- · 2020-01-27 00:26

I added the jenkins user to root group and restarted the jenkins and it started working.

sudo usermod -a -G root jenkins
sudo service jenkins restart
查看更多
相关推荐>>
5楼-- · 2020-01-27 00:26

I am running Jenkins inside a docker container. The simplest solution for me was to make a custom image that dynamically sets the GID, like:

FROM jenkins/jenkins:lts
...
CMD DOCKER_GID=$(stat -c '%g' /var/run/docker.sock) && \
    groupadd -for -g ${DOCKER_GID} docker && \
    usermod -aG docker jenkins && \
    sudo -E -H -u jenkins bash -c /usr/local/bin/jenkins.sh

See: https://github.com/jenkinsci/docker/issues/263

Alternatively you could launch jenkins with the following options:

-v /var/run/docker.sock:/var/run/docker.sock \
-u jenkins:$(getent group docker | cut -d: -f3)

This assumes your jenkins image has docker client installed. See: https://getintodevops.com/blog/the-simple-way-to-run-docker-in-docker-for-ci

查看更多
等我变得足够好
6楼-- · 2020-01-27 00:27

The user jenkins needs to be added to the group docker:

sudo usermod -a -G docker jenkins

Then restart Jenkins.

Edit

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 alice

or whatever your username is.

You can check it at the end doing cat /etc/group | grep 'docker' and see something like this:

docker:x:998:alice

in one of the lines.

As Ilya Kolesnikov says in the comment, re-login!

查看更多
Juvenile、少年°
7楼-- · 2020-01-27 00:27

I have Jenkins running in Docker and connected Jenkins is using Docker socket from host machine Ubuntu 16.04 via volume to /var/run/docker.sock.

For me solution was:

1) Inside Docker container of Jenkins (docker exec -it jenkins bash on host machine)

usermod -a -G docker jenkins
chmod 664 /var/run/docker.sock
service jenkins restart (or systemctl restart jenkins.service)
su jenkins

2) On host machine:

sudo service docker restart

664 means - read and write(but not execute) for owner and users from group.

查看更多
登录 后发表回答