Programming

unix : ///var/run/docker.sock에서 Docker 데몬 소켓에 연결하는 동안 권한이 거부되었습니다.

procodes 2020. 8. 22. 13:04
반응형

unix : ///var/run/docker.sock에서 Docker 데몬 소켓에 연결하는 동안 권한이 거부되었습니다.


나는 도커를 처음 사용합니다. Jenkins와 함께 로컬 컴퓨터 (Ubuntu 16.04)에서 도커를 사용하려고했습니다.

아래 파이프 라인 스크립트로 새 작업을 구성했습니다.

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

그러나 아래 오류로 실패합니다.

여기에 이미지 설명 입력


사용자 jenkins를 그룹에 추가해야합니다 docker.

sudo usermod -a -G docker jenkins

그런 다음 Jenkins를 다시 시작하십시오.

편집하다

docker 에서이 메시지를 수신했지만 jenkins를 사용하지 않아 스택 오버플로에 대한이 질문에 도달하면 대부분의 오류는 동일합니다. 권한이없는 사용자는 docker 그룹에 속하지 않습니다.

넌 할 수있어:

sudo usermod -a -G docker alice

또는 사용자 이름이 무엇이든.

마지막에 확인하고 다음 cat /etc/group과 같이 볼 수 있습니다 .

docker:x:998:alice

라인 중 하나에서.

Ilya Kolesnikov가 댓글에서 말했듯이 다시 로그인하십시오!


내 첫 번째 해결책은 다음과 같습니다.

usermod -aG docker jenkins
usermod -aG root jenkins
chmod 664 /var/run/docker.sock

그러나 그들 중 누구도 나를 위해 일하지 않았습니다.

chmod 777 /var/run/docker.sock

작동하지만 올바른 전화인지 모르겠습니다.


나를위한 성공

sudo usermod -a -G docker $USER
reboot

젠킨스 사용자를 루트 그룹에 추가하고 젠킨스를 다시 시작하면 작동하기 시작했습니다.

sudo usermod -a -G root jenkins
sudo service jenkins restart

2018-08-19

나는이 일에 며칠 동안 붙어 있었고 이유와 방법에 대한 완전한 답변을 찾지 못했기 때문에 동일한 문제에 우연히 걸려 위의 답변이 작동하지 않는 다른 사람들을 위해 하나를 게시 할 것입니다.

다음은 Docker 내에서 Jenkins를 실행할 때 3 가지 중요한 단계입니다.

  1. /var/run/docker.sock호스트에서 도커를 사용할 수 있도록 소켓 을 jenkins 컨테이너에 마운트합니다 .
  2. 사용하기 위해서는 컨테이너 내부에 도커를 설치해야합니다. 이것은 그것을 수행하는 방법에 대한 위대하고 간단한 기사입니다. 최신 버전에는 이미 Docker가 설치되어있을 수 있습니다.
  3. sudo usermod -a -G docker jenkins도커 그룹에 젠킨스를 추가하기 위해 실행 합니다. 그러나 여기서 호스트 도커와 컨테이너 도커의 그룹 ID가 같지 않으면 권한 문제가 발생할 수 있으므로 컨테이너 도커의 gid를 호스트 도커 gid와 동일하게 조정하는 것이 매우 중요합니다.

실행 스크립트의 일부로 또는 단순히 사용 exec하고 수동으로 수행 하여 이를 수행 할 수 있습니다 groupmod -g <YOUR_HOST_DOCKER_GID> docker..

또한 /var/run/docker.sock큰 보안 위험이 있으므로 권한 을 777 또는 이와 유사한 것으로 변경하지 마십시오 .

도움이 되었기를 바랍니다


Docker에서 Jenkins를 실행 중이고 Jenkins는 호스트 시스템 Ubuntu 16.04에서 볼륨을 통해 /var/run/docker.sock으로 Docker 소켓을 사용하고 있습니다.

저에게 해결책은 다음과 같습니다.

1) Jenkins의 Docker 컨테이너 내부 ( docker exec -it jenkins bash호스트 머신)

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

2) 호스트 컴퓨터에서 :

sudo service docker restart

664 의미-그룹의 소유자 및 사용자에 대해 읽고 쓰기 (실행은 아님)합니다.


사용자를 위한 보조 그룹docker 으로 간단히 추가jenkins

sudo usermod -a -G docker jenkins

Docker 이미지를 Jenkins Agent 로 사용할 때 항상 충분하지는 않습니다 . 즉, 또는로 Jenkinsfile시작하는 pipeline{agent{dockerfile경우 pipeline{agent{image:

pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile.jenkinsAgent'
        }
    }
    stages {

Jenkins가 docker run명령을 수행하기 때문에 세 가지 문제가 발생합니다.

  • 에이전트에는 (아마도) Docker 프로그램이 설치되어 있지 않습니다.
  • 에이전트는 Docker 데몬 소켓에 액세스 할 수 없으므로 권장되지 않는 Docker-in-Docker 실행을 시도 합니다 .
  • Jenkins는 에이전트가 사용해야하는 숫자 사용자 ID와 숫자 그룹 ID를 제공합니다. 에이전트는 docker run컨테이너에 로그인하지 않기 때문에 보충 그룹 이 없습니다 ( sudo).

에이전트 용 Docker 설치

Docker 이미지 내에서 Docker 프로그램을 사용할 수 있도록하려면 Dockerfile에서 Docker 설치 단계실행 하기 만하면됩니다.

# Dockerfile.jenkinsAgent
FROM debian:stretch-backports
# Install Docker in the image, which adds a docker group
RUN apt-get -y update && \
 apt-get -y install \
   apt-transport-https \
   ca-certificates \
   curl \
   gnupg \
   lsb-release \
   software-properties-common

RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"

RUN apt-get -y update && \
 apt-get -y install \
   docker-ce \
   docker-ce-cli \
   containerd.io

...

Docker 데몬 소켓 공유

앞서 말했듯 두 번째 문제를 해결한다는 것은 Jenkins Docker 컨테이너를 실행하여 컨테이너 외부에 있는 Docker 데몬과 Docker 데몬 소켓을 공유하는 것을 의미합니다 . 따라서 Jenkins에게 해당 공유로 Docker 컨테이너를 실행하도록 지시해야합니다.

pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile.jenkinsAgent'
            args '-v /var/run/docker.sock:/var/run/docker.sock'
        }
    }

UID 및 GID 설정

The ideal fix to the third problem would be set up supplementary groups for the Agent. That does not seem possible. The only fix I'm aware of is to run the Agent with the Jenkins UID and the Docker GID (the socket has group write permission and is owned by root.docker). But in general, you do not know what those IDs are (they were allocated when the useradd ... jenkins and groupadd ... docker ran when Jenkins and Docker were installed on the host). And you can not simply tell Jenkins to user user jenkins and group docker

args '-v /var/run/docker.sock:/var/run/docker.sock -u jenkins:docker'

because that tells Docker to use the user and group that are named jenkins and docker within the image, and your Docker image probably does not have the jenkins user and group, and even if it did there would be no guarantee it would have the same UID and GID as the host, and there is similarly no guarantee that the docker GID is the same

Fortunately, Jenkins runs the docker build command for your Dockerfile in a script, so you can do some shell-script magic to pass through that information as Docker build arguments:

pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile.jenkinsAgent'
            additionalBuildArgs  '--build-arg JENKINSUID=`id -u jenkins` --build-arg JENKINSGID=`id -g jenkins` --build-arg DOCKERGID=`stat -c %g /var/run/docker.sock`'
            args '-v /var/run/docker.sock:/var/run/docker.sock -u jenkins:docker'
        }
    }

That uses the id command to get the UID and GID of the jenkins user and the stat command to get information about the Docker socket.

Your Dockerfile can use that information to setup a jenkins user and docker group for the Agent, using groupadd, groupmod and useradd:

# Dockerfile.jenkinsAgent
FROM debian:stretch-backports
ARG JENKINSUID
ARG JENKINSGID
ARG DOCKERGID
...
# Install Docker in the image, which adds a docker group
RUN apt-get -y update && \
 apt-get -y install \
   apt-transport-https \
   ca-certificates \
   curl \
   gnupg \
   lsb-release \
   software-properties-common

RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"

RUN apt-get -y update && \
 apt-get -y install \
   docker-ce \
   docker-ce-cli \
   containerd.io

...
# Setup users and groups
RUN groupadd -g ${JENKINSGID} jenkins
RUN groupmod -g ${DOCKERGID} docker
RUN useradd -c "Jenkins user" -g ${JENKINSGID} -G ${DOCKERGID} -M -N -u ${JENKINSUID} jenkins

2019-02-16

Most of the steps were the same for me as the others has written. However, I was not able to add jenkins to the group docker using usermod with the mentioned solutions.

I tried the following command from the docker host, and from the running docker container:

sudo usermod -a -G docker jenkins

(I entered to the running docker container with the following command from the docker host:

docker exec -t -i my_container_id_or_name /bin/bash

)

Received from docker host:

usermod: user 'jenkins' does not exist

Received from docker container:

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] password for jenkins:

I didnt know the password.

Without the sudo part of the command, in the docker container I received:

usermod: Permission denied. usermod: cannot lock /etc/passwd; try again later.

Solution: I entered to the running docker container from the docker host with the following command:

docker exec -t -i -u root my_container_id_or_name /bin/bash

Now, I entered as root, and issued the following command:

usermod -a -G docker jenkins

Then, from the docker host, I restarted my running docker container with the following command:

docker restart my_container_id_or_name

After that, I started the jenkins job and it finished with success.

I only used the root user to issue the usermod command for the user jenkins.


2019-05-26

This worked for me !

Example docker-compose:

version: "3"
services:
  jenkins:
    image: jenkinsci/blueocean
    privileged: true
    ports:
      - "8080:8080"
    volumes:
      - $HOME/learning/jenkins/jenkins_home:/var/jenkins_home
    environment:
      - DOCKER_HOST=tcp://socat:2375
    links:
      - socat

  socat:
     image: bpack/socat
     command: TCP4-LISTEN:2375,fork,reuseaddr UNIX-CONNECT:/var/run/docker.sock
     volumes:
        - /var/run/docker.sock:/var/run/docker.sock
     expose:
        - "2375"

In my case, it was not only necessary add jenkins user to docker group, but make that group the primary group of the jenkins user.

# usermod -g docker jenkins
# usermod -a -G jenkins jenkins

Don't forget to reconnect the jenkins slave node or restart the jenkins server, depend on your case.


sudo usermod -a -G docker jenkins
sudo service jenkins restart

On the server where Jenkins is running, I used

sudo setfacl -m user:tomcat:rw /var/run/docker.sock

And then run each docker container with

-v /var/run/docker.sock:/var/run/docker.sock

Using setfacl seems a better option, and no "-u user" is needed. The containers then run as the same user that is running Jenkins. But I would appreciate any feedback from the security experts.


도커 컨테이너 내에서 Jenkins를 실행하고 있습니다. 저에게 가장 간단한 해결책은 다음과 같이 GID를 동적으로 설정하는 커스텀 이미지를 만드는 것이 었습니다.

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

참조 : https://github.com/jenkinsci/docker/issues/263

또는 다음 옵션을 사용하여 jenkins를 시작할 수 있습니다.

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

이것은 젠킨스 이미지에 도커 클라이언트가 설치되어 있다고 가정합니다. 참조 : https://getintodevops.com/blog/the-simple-way-to-run-docker-in-docker-for-ci


처음부터 "-u root"옵션을 사용하여 도커를 실행해야 할 수도 있습니다.

적어도 내 문제는 해결 됐어

참고 URL : https://stackoverflow.com/questions/47854463/got-permission-denied-while-trying-to-connect-to-the-docker-daemon-socket-at-uni

반응형