且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Jenkins Docker 容器无法访问 docker.sock

更新时间:2023-01-14 23:24:25

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

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.