Jenkins Docker容器无法访问docker.sock [英] Jenkins Docker Container can't access docker.sock

查看:189
本文介绍了Jenkins Docker容器无法访问docker.sock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用docker-compose和以下配置部署了标准的Jenkins Docker映像:

I deployed the standard Jenkins Docker image with docker-compose and this configuration:

deployer:
  image: jenkins
  volumes:
    - "/mnt/jenkins:/var/jenkins_home"
    - "/var/run/docker.sock:/var/run/docker.sock"
  ports:
    - "2375:2375"
    - "8080:8080"
    - "50000:50000"

在阅读了许多SO问题之后,我进行了测试,并使用 gpasswd -a $ {USER} Docker 将Root添加到了docker用户组中并进行了验证容器中的用户是具有 docker exec jenkins_deployer echo $ {USER} 的Root。

After reading numerous SO questions I tested added Root to the docker user group with gpasswd -a ${USER} docker and verified that the user inside the Container is Root with docker exec jenkins_deployer echo ${USER}.

使用 Docker URL = unix:///var/run/docker.sock在Jenkins UI中添加Docker访问权限,我收到错误消息
org.newsclub.net.unix.AFUNIXSocketException:权限被拒绝 (套接字:/run/docker.sock)

When I try to add Docker access inside the Jenkins UI with "Docker URL = unix:///var/run/docker.sock" I get the error message "org.newsclub.net.unix.AFUNIXSocketException: Permission denied (socket: /run/docker.sock)"

我如何让Jenkins访问docker.sock来自动部署Docker容器?

How can I give Jenkins access to docker.sock to automatically deploy Docker Containers?

推荐答案

我知道我迟到了两年,但遇到了同样的问题,有了这种解决方案可以节省我几个小时的工作。

I know I'm two years late, but I ran into the same issue and having this solution would've save me several hours of work.

所以我需要部署一个Jenkins容器,该容器会自动部署Docker容器。这是我用来构建和运行的文件:

So I needed to deploy a Jenkins Container that automatically deploys Docker Containers. Here are the files I used to build and run :

Dockerfile

Dockerfile

FROM jenkins/jenkins:latest

USER root
RUN apt-get update -qq \
    && apt-get install -qqy apt-transport-https ca-certificates curl gnupg2 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 update  -qq \
    && apt-get install docker-ce=17.12.1~ce-0~debian -y

RUN usermod -aG docker jenkins

docker-compose.yml

docker-compose.yml

version: '3'

services:
  jenkins:
    container_name: 'jenkins-container'
    privileged: true
    build: .
    ports:
      - '8080:8080'
      - '50000:50000'
    volumes:
      - jenkins-data:/var/jenkins_home
    restart: unless-stopped

volumes:
  jenkins-data:

然后在这些文件所在的文件夹中,运行以下命令:

Then, in the folder these files are, run the following command :

docker-compose up

在容器启动后,使用它在内部启动Docker:

When the container is up, use this to start Docker inside :

docker exec -it --user root <CONTAINER_ID>

service docker start

然后瞧瞧!可能会有一些更优化的解决方案,但这对我来说现在非常有用。

And voilà ! There might be some more optimized solutions, but this works great for me right now.

您现在可以访问< YOUR_IP>:8080 在浏览器中可以访问可以运行Docker容器的全新Jenkins。

You can now visit <YOUR_IP>:8080 in a browser to have access to your brand new Jenkins that can run Docker Containers.

这篇关于Jenkins Docker容器无法访问docker.sock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆