【问题标题】:Building Docker images with Jenkins Pipeline使用 Jenkins Pipeline 构建 Docker 镜像
【发布时间】:2018-06-19 22:06:30
【问题描述】:

我在容器内运行 Jenkins;但是,我的 Jenkinsfile 无法构建 Docker 映像并引发此错误:

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

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

绑定安装 docker.sock 文件似乎不起作用

有什么想法吗?

这是我的 docker 文件

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 the missing library

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

我用我的 docker-compose.yml 调用这个 Dockerfile,如下所示:

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,直到找到更好的方法。

【问题讨论】:

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


    【解决方案1】:

    使用Jenkins创建docker镜像挂载docker socket的标准解决方案。

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

    【讨论】:

    • 我正在通过 docker-compose 运行 docker,并且我已经将这些卷包含在我的 compose 文件中,但它仍然无法正常工作。我更新了我的 dockerfile 以包含缺少的库,请参阅上面更新的问题,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-11-04
    • 2017-07-16
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-27
    • 2020-10-18
    相关资源
    最近更新 更多