【问题标题】:Running a command inside a docker image in Jenkins Pipeline Stage在 Jenkins Pipeline Stage 的 docker 镜像中运行命令
【发布时间】:2020-11-24 16:36:06
【问题描述】:

我有一个简单的 Jenkins 管道,它创建一个带有 3 个容器的 pod - jnlp、dind、example-test

这看起来如下 -

    agent {
        kubernetes {
      yaml """
apiVersion: v1
kind: Pod
metadata:
  name: example-pb
  annotations:
    container.apparmor.security.beta.kubernetes.io/dind: unconfined
    container.seccomp.security.alpha.kubernetes.io/dind: unconfined
  labels:
    some-label: label1
spec:
  serviceAccountName: example
  securityContext:
    runAsUser: 10000
    runAsGroup: 10000
  containers: 
    - name: jnlp
      image: 'jenkins/jnlp-slave:4.3-4-alpine'
      args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)']
    - name: dind
      image: docker:dind
      securityContext:
          runAsUser: 0
          runAsGroup: 0
          fsGroup: 0        
          privileged: true
      tty: true
      volumeMounts:
      - name: var-run
        mountPath: /var/run
    - name: example-test
      image: pranavbhatia/example-test:0.1
      securityContext:
          runAsUser: 0
          runAsGroup: 0
          fsGroup: 0
      volumeMounts:
      - name: var-run
        mountPath: /var/run  
  volumes:
  - emptyDir: {}
    name: var-run
"""
        }
    }

还定义了几个阶段-

    stages {
        stage ('DIND') {
            steps {
                container('dind') {
                    sh 'pwd && echo "Pulling image" && docker pull ubuntu:18.04'
                }
            }
        }
        stage ('EXAMPLE') {
            steps {
                container('example-test') {
                    sh './example'
                }
            }
        }

所以现在我的根文件夹中有这个脚本“示例”,我想运行它,但不知何故找不到它。

Dockerfile 看起来像这样 -

FROM ubuntu:18.04

COPY ./example ./example

#make it executable
RUN chmod +x ./example

#command to keep container running in detached mode
CMD tail -f /dev/null

pwd 返回“/home/jenkins/agent/workspace/test-pipeline”,而不是 docker 容器路径。

输出如下-

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Created Pod: test-pipeline-14-s7167-4zcg5-s68gw in namespace dc-pipeline
Still waiting to schedule task
‘test-pipeline-14-s7167-4zcg5-s68gw’ is offline
Agent test-pipeline-14-s7167-4zcg5-s68gw is provisioned from template test-pipeline_14-s7167-4zcg5
---
apiVersion: "v1"
kind: "Pod"
metadata:
  annotations:
    container.apparmor.security.beta.kubernetes.io/dind: "unconfined"
    container.seccomp.security.alpha.kubernetes.io/dind: "unconfined"
    buildUrl: "http://jenkins-164-229:8080/job/test-pipeline/14/"
    runUrl: "job/test-pipeline/14/"
  labels:
    some-label: "label1"
    jenkins: "slave"
    jenkins/label: "test-pipeline_14-s7167"
  name: "test-pipeline-14-s7167-4zcg5-s68gw"
spec:
  containers:
  - args:
    - "$(JENKINS_SECRET)"
    - "$(JENKINS_NAME)"
    env:
    - name: "JENKINS_SECRET"
      value: "********"
    - name: "JENKINS_TUNNEL"
      value: "jenkins-164-229-agent:50000"
    - name: "JENKINS_AGENT_NAME"
      value: "test-pipeline-14-s7167-4zcg5-s68gw"
    - name: "JENKINS_NAME"
      value: "test-pipeline-14-s7167-4zcg5-s68gw"
    - name: "JENKINS_AGENT_WORKDIR"
      value: "/home/jenkins/agent"
    - name: "JENKINS_URL"
      value: "http://jenkins-164-229:8080/"
    - name: "HOME"
      value: "/home/jenkins"
    image: "jenkins/jnlp-slave:4.3-4-alpine"
    name: "jnlp"
    volumeMounts:
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  - image: "pranavbhatia/example-test:0.1"
    name: "example-test"
    securityContext:
      runAsGroup: 0
      runAsUser: 0
    volumeMounts:
    - mountPath: "/var/run"
      name: "var-run"
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  - image: "docker:dind"
    name: "dind"
    securityContext:
      privileged: true
      runAsGroup: 0
      runAsUser: 0
    tty: true
    volumeMounts:
    - mountPath: "/var/run"
      name: "var-run"
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  nodeSelector:
    beta.kubernetes.io/os: "linux"
  restartPolicy: "Never"
  securityContext:
    runAsGroup: 10000
    runAsUser: 10000
  serviceAccountName: "example"
  volumes:
  - emptyDir: {}
    name: "var-run"
  - emptyDir:
      medium: ""
    name: "workspace-volume"

Running on test-pipeline-14-s7167-4zcg5-s68gw in /home/jenkins/agent/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (DIND)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ pwd
/home/jenkins/agent/workspace/test-pipeline
+ echo 'Pulling image'
Pulling image
+ docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
7595c8c21622: Pulling fs layer
d13af8ca898f: Pulling fs layer
70799171ddba: Pulling fs layer
b6c12202c5ef: Pulling fs layer
b6c12202c5ef: Waiting
d13af8ca898f: Verifying Checksum
d13af8ca898f: Download complete
70799171ddba: Verifying Checksum
70799171ddba: Download complete
b6c12202c5ef: Verifying Checksum
b6c12202c5ef: Download complete
7595c8c21622: Verifying Checksum
7595c8c21622: Download complete
7595c8c21622: Pull complete
d13af8ca898f: Pull complete
70799171ddba: Pull complete
b6c12202c5ef: Pull complete
Digest: sha256:a61728f6128fb4a7a20efaa7597607ed6e69973ee9b9123e3b4fd28b7bba100b
Status: Downloaded newer image for ubuntu:18.04
docker.io/library/ubuntu:18.04
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (EXAMPLE)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ pwd
/home/jenkins/agent/workspace/test-pipeline
+ ./example
/home/jenkins/agent/workspace/test-pipeline@tmp/durable-26584660/script.sh: 1: /home/jenkins/agent/workspace/test-pipeline@tmp/durable-26584660/script.sh: ./example: not found
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

知道如何解决这个问题吗?

【问题讨论】:

    标签: docker jenkins kubernetes jenkins-pipeline jenkins-kubernetes


    【解决方案1】:

    使用 sh '/example' (不带点)执行它可能会起作用。您将它安装在根文件系统中,但是当容器中的命令从 Jenkins 运行时,PWD 将成为工作区。

    【讨论】:

    • 运行“ls”命令返回给我 - 所以看起来它没有在容器内运行。甚至“pwd”似乎是 /home/jenkins/agent/workspace/test-pipeline 的输出 ls -> ls -la ./ 总共 8 个 drwxr-xr-x。 2 10000 10000 4096 8 月 4 日 13:07。 drwxr-xr-x。 4 10000 10000 4096 8 月 4 日 13:07 ..
    • ls -la / 有什么用?它是否显示example 文件?
    • 我的错,它在没有点的情况下工作。但是我仍然对差异感到困惑。 “从 Jenkins 运行 PWD 将成为工作区”——这是什么意思?
    • Jenkins 在容器内执行命令,但在执行此操作之前将工作区设置为当前目录。在 Dockerfile 中,当前目录取决于基础镜像(如果您没有更改它)。 Ubuntu 基础映像将 / 作为当前目录。
    猜你喜欢
    • 1970-01-01
    • 2019-10-09
    • 2018-06-19
    • 2016-11-25
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    相关资源
    最近更新 更多