使用Jenkins Pipeline构建Docker镜像

时间:2018-01-10 16:58:35

标签: docker jenkins docker-compose dockerfile jenkins-pipeline

我在容器里面运行Jenkins;但是,我的Jenkinsfile无法构建Docker镜像并抛出此错误:

  

运行shell脚本   + docker build -t 11207b4dde319c028c35cc11eff8216939cf96f5 -f Dockerfile。 docker:加载共享库时出错:   libltdl.so.7:无法打开共享对象文件:没有这样的文件或   目录

我不想通过在我的jenkins图像中安装docker客户端来遵循在docker中运行docker的解决方案。

绑定挂载docker.sock文件似乎无法正常工作

有什么想法吗?

这是我的泊坞文件

FROM microsoft/aspnetcore-build:2 AS build-env
RUN dotnet --version

Jenkinsfile(https://jenkins.io/doc/book/pipeline/docker/

pipeline {
    agent { dockerfile true }
    stages {
        stage('Test') {
            steps {
                sh 'dotnet --version'
                sh 'docker --version'
            }
        }
    }
}

更新 为了解决这个问题,我最终将我的Dockerfile更新为apt-get install缺少的库

FROM jenkins/jenkins:lts
COPY executors.groovy /usr/share/jenkins/ref/init.groovy.d/executors.groovy
#Installing missing libs required for jenkins to run on docker 
USER root
RUN apt-get update \
      && apt-get upgrade -y \
      && apt-get install -y sudo libltdl-dev \
      && apt-get install apt-utils -y \
      && rm -rf /var/lib/apt/lists/*
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers

我将这个Dockerfile称为docker-compose.yml,如下所示:

version: '3.1'

#Repo: https://hub.docker.com/r/jenkins/jenkins/
#Doc: https://github.com/jenkinsci/docker/blob/master/README.md

services:
  jenkins_server:
    build: .
    image: joel/jenkins:manager #gives a name to our image that we just built
    ports:
      - "8080:8080"  #master port on Docker host mapping to 8080 inside the container
      - "50000:50000"  #used for slave agents - not needed to ssh slaves

    volumes:
      - $HOME/jenkins_home:/var/jenkins_home #let docker manage the volume on the host to avoid file permission issues when the jenkins user doesn't have enough perm to access this location on a different machine
      - $HOME/jenkins_home/logs:/var/log/jenkins/ #https://wiki.jenkins.io/display/JENKINS/Logging
      - /var/run/docker.sock:/var/run/docker.sock #Giving the agent the capacity to run docker containers
      - /usr/bin/docker:/usr/bin/docker

我现在唯一的问题是jenkins以“root”身份运行,以避免我一直遇到的权限问题;即使我将jenkins用户添加到docker组,它仍然无效,所以我现在只是以root身份运行jenkins,直到找到更好的方法。

1 个答案:

答案 0 :(得分:0)

使用Jenkins安装docker socket的标准解决方案。

docker run -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/bin/docker ...