作为Kubernetes上Jenkins Worker的一部分构建映像

6yt4nkrj  于 2023-01-20  发布在  Jenkins
关注(0)|答案(1)|浏览(143)

我在kubernetes(AWS EKS)上部署了Jenkins,并为Jenkins管道任务指定了一个节点。我有一个管道,我想构建一个Docker映像,下面是我的管道的外观:

pipeline {
    agent {
        kubernetes {
            defaultContainer 'jnlp'
            yaml """
apiVersion: v1
kind: Pod
spec:
  nodeSelector:
    illumex.ai/noderole: jenkins-worker
  containers:
  - name: docker
    image: docker:latest
    imagePullPolicy: Always
    command:
    - cat
    tty: true
    volumeMounts:
    - mountPath: /var/run/docker.sock
      name: docker-sock
  volumes:
  - name: docker-sock
    hostPath:
      path: /var/run/docker.sock
"""
        }
    }

    stages {
        stage('build') {
            steps {
                container('system') {
                        sh """
                        docker system prune -f
                        """

但是,我的工作失败的原因是:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

我认为这是权限问题。然而,由于容器是作为管道的一部分创建的,那么我应该给予哪个用户权限呢?

jecbmhm3

jecbmhm31#

在Jenkins计算机上,确保安装了docker,并且用户jenkins位于docker组中。
安装Docker插件是不够的,必须在Jenkins机器上安装Kubectl插件。

FROM jenkins/jenkins 

ARG HOST_UID=1004
ARG HOST_GID=999

USER root
RUN apt-get -y update && \
    apt-get -y install apt-transport-https ca-certificates curl gnupg-agent software-properties-common && \
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - && \
    add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") $(lsb_release -cs) stable" && \
    apt-get update && \
    apt-get -y install docker-ce docker-ce-cli containerd.io
RUN curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose  \
    && chmod +x /usr/local/bin/docker-compose  \
    && ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose \
    && curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - 
RUN echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | tee /etc/apt/sources.list.d/kubernetes.list \
    && apt-get -y update \
    && apt install -y kubectl

RUN usermod -u $HOST_UID jenkins
RUN groupmod -g $HOST_GID docker
RUN usermod -aG docker jenkins
FROM base
RUN kubectl version
USER jenkins

相关问题