Jenkins Docker 容器无法访问 docker.sock [英] Jenkins Docker Container can't access 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 验证容器内的用户是 Root回声 ${USER}
.
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: Permission denied (socket:/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.
所以我需要部署一个自动部署 Docker 容器的 Jenkins 容器.以下是我用来构建和运行的文件:
So I needed to deploy a Jenkins Container that automatically deploys Docker Containers. Here are the files I used to build and run :
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屋!