gpt4 book ai didi

Docker 挂载卷 GID 不匹配导致错误 : dial unix/var/run/docker. sock: connect: permission denied

转载 作者:行者123 更新时间:2023-12-02 04:33:07 26 4
gpt4 key购买 nike

使用jenkins官方镜像,我已经安装了docker和docker-compose,并将jenkins添加到docker组(容器中GID 999)。

在那之后,我共享了主机的/var/run/docker.sock 以启用 jenkins 创建“兄弟”容器。碰巧原始文件的 GID 为 134,并且挂载了这个 GID。我收到以下错误:

demo_1 | docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.32/containers/create: dial unix /var/run/docker.sock: connect: permission denied. demo_1 | See 'docker run --help'.

关于如何解决这个问题有什么想法吗?

我的最小(尚未优化)Dockerfile 是:

FROM jenkins/jenkins:lts

USER root

RUN apt-get update && apt-get install -y apt-transport-https \
ca-certificates \
curl \
gnupg2 \
software-properties-common

RUN curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg | apt-key add -
RUN apt-key fingerprint 0EBFCD88
RUN add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
$(lsb_release -cs) \
stable"

RUN apt-get update
RUN apt-get install -y docker-ce docker-compose
RUN usermod -aG docker jenkins

USER jenkins
RUN newgrp docker

我还创建了一个 docker-compose 来测试它:

version: '2'
services:
demo:
build: .
ports:
- 8080:8080
- 50000:50000
volumes:
- /var/run/docker.sock:/var/run/docker.sock
command: >
/bin/sh -c "
set -e
groups
docker -v
docker-compose -v
ls -ln /var/run/docker.sock
id jenkins
docker run hello-world
"

输出是:

demo_1  | jenkins staff docker
demo_1 | Docker version 17.09.0-ce, build afdb6d4
demo_1 | docker-compose version 1.8.0, build unknown
demo_1 | srw-rw---- 1 0 134 0 Sep 30 07:36 /var/run/docker.sock
demo_1 | uid=1000(jenkins) gid=1000(jenkins) groups=1000(jenkins),50(staff),999(docker)
demo_1 | docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.32/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
demo_1 | See 'docker run --help'.

最佳答案

我给了这个问题一个肮脏的修复,所以我打开这个问题看看是否会出现更好的问题。

由于/var/run/docker.sock 文件由 root 所有,它们具有相同的 UID,因此我将 jenkins 添加到 sudoers 列表中而无需输入密码:

RUN adduser jenkins sudo
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers

这解决了问题。我不喜欢它,但它有效。

关于Docker 挂载卷 GID 不匹配导致错误 : dial unix/var/run/docker. sock: connect: permission denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46632829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com