Search code examples
tektontekton-pipelines

How to attach a volume to docker running in tekton pipelines


I have a problem attaching a volume to the docker image running inside tekton pipelines. I have used the below task

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: distributor-base
  namespace: cicd
  labels:
    app.kubernetes.io/version: "0.1"
  annotations:
    tekton.dev/pipelines.minVersion: "0.12.1"
    tekton.dev/platforms: "linux/amd64"
spec:
  params:
  - name: builder_image
    description: The location of the docker builder image.
    default: docker:stable
  - name: dind_image
    description: The location of the docker-in-docker image.
    default: docker:dind
  - name: context
    description: Path to the directory to use as context.
    default: .
  workspaces:
  - name: source
  steps:
  - name: docker-build
    image: docker
    env:
    # Connect to the sidecar over TCP, with TLS.
    - name: DOCKER_HOST
      value: tcp://localhost:2376
    # Verify TLS.
    - name: DOCKER_TLS_VERIFY
      value: '1'
    # Use the certs generated by the sidecar daemon.
    - name: DOCKER_CERT_PATH
      value: /certs/client
    - name: DOCKER_USER
      valueFrom:
        secretKeyRef:
          key: username
          name: docker-auth
    - name: DOCKER_TOKEN
      valueFrom:
        secretKeyRef:
          key: password
          name: docker-auth
    - name: DIND_CONFIG
      valueFrom:
        configMapKeyRef:
          key: file
          name: dind-env
    workingDir: $(workspaces.source.path)
    args:
      - --storage-driver=vfs
      - --debug
    securityContext:
      privileged: true
    script: |
      #!/usr/bin/env sh
      set -e

      pwd
      ls -ltr /workspace/source
      docker run --privileged -v "/workspace/source:/workspace" busybox ls -ltr /workspace
    volumeMounts:
      - mountPath: /certs/client
        name: dind-certs
  sidecars:
  - image: $(params.dind_image)
    name: server
    args:
      - --storage-driver=vfs
      - --debug
      - --userland-proxy=false
    resources:
      requests:
        memory: "512Mi"
    securityContext:
      privileged: true
    env:
    # Write generated certs to the path shared with the client.
    - name: DOCKER_TLS_CERTDIR
      value: /certs
    volumeMounts:
    - mountPath: /certs/client
      name: dind-certs
    # Wait for the dind daemon to generate the certs it will share with the
    # client.
    readinessProbe:
      periodSeconds: 1
      exec:
        command: ['ls', '/certs/client/ca.pem']

  volumes:
  - name: dind-certs
    emptyDir: {}

in the above task workspace comes from another git-clone task

workspaces:
  - name: source

in this task, I am trying to run a docker image that has access to the workspace folder , because I have to modify some files in the workspace folder.

when we look into the script

  pwd
  ls -ltr /workspace/source
  docker run --privileged -v "/workspace/source:/workspace"

below is the console log of above 3 commands

workspace/source
total 84
-rwxr-xr-x    1 50381    50381         3206 Jun  1 10:13 README.md
-rwxr-xr-x    1 50381    50381        10751 Jun  1 10:13 Jenkinsfile.next
-rwxr-xr-x    1 50381    50381         5302 Jun  1 10:13 wait-for-it.sh
drwxr-xr-x    4 50381    50381         6144 Jun  1 10:13 overlays
-rwxr-xr-x    1 50381    50381         2750 Jun  1 10:13 example-distributor.yaml
drwxr-xr-x    5 50381    50381         6144 Jun  1 10:13 bases
-rw-r--r--    1 50381    50381            0 Jun  1 10:13 semantic.out
-rw-r--r--    1 50381    50381        44672 Jun  1 10:13 final.yaml
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
462eb288b104: Pulling fs layer
462eb288b104: Verifying Checksum
462eb288b104: Download complete
462eb288b104: Pull complete
Digest: sha256:ebadf81a7f2146e95f8c850ad7af8cf9755d31cdba380a8ffd5930fba5996095
Status: Downloaded newer image for busybox:latest
total 0

basically pwd command is giving me results and ls -ltr command also gives me the results

but when I try to attach /workspace/source folder as a volume to busybox docker, I am not able to see the content.

i mean since I have attached a volume into the directory /workspace I would expect the contents from local folder /workspace/source but I see 0 results from the above log.

basically volume is not getting attached properly.

can anyone please help me to fix this issue.

below is my pipeline run triggered by tekton-triggers

apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerTemplate
metadata:
  name: github-gitops-template
  namespace: cicd
spec:
  params:
    - name: gitRevision
      description: The git revision (SHA)
      default: master
    - name: gitRepoUrl
      description: The git repository url ("https://github.com/foo/bar.git")
    - name: gitRepoName
      description: The git repository name
    - name: branchUrl
      description: The git repository branch url
    - name: repoFullName
      description: The git repository full name
    - name: commitSha
      description: The git commit sha
  resourcetemplates:
    - apiVersion: tekton.dev/v1beta1
      kind: PipelineRun
      metadata:
        generateName: $(tt.params.gitRepoName)-
      spec:
        timeout: 0h10m
        pipelineRef:
          name: gitops-pipeline
        serviceAccountName: github-service-account
        params:
          - name: url
            value: $(tt.params.gitRepoUrl)
          - name: branch
            value: $(tt.params.gitRevision)
          - name: repoName
            value: $(tt.params.gitRepoName)
          - name: branchUrl
            value: $(tt.params.branchUrl)
          - name: repoFullName
            value: $(tt.params.repoFullName)
          - name: commitSha
            value: $(tt.params.commitSha)
        workspaces:
          - name: ws
            volumeClaimTemplate:
              spec:
                accessModes:
                  - ReadWriteOnce
                resources:
                  requests:
                    storage: 50Mi

below is my task run:

completionTime: '2022-06-01T10:13:47Z'
conditions:
  - lastTransitionTime: '2022-06-01T10:13:47Z'
    message: All Steps have completed executing
    reason: Succeeded
    status: 'True'
    type: Succeeded
podName: gitops-core-business-tzb7f-distributor-base-pod
sidecars:
  - container: sidecar-server
    imageID: 'docker-pullable://gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/nop@sha256:1d65a20cd5fbc79dc10e48ce9d2f7251736dac13b302b49a1c9a8717c5f2b5c5'
    name: server
    terminated:
      containerID: 'docker://d5e96143812bb4912c6297f7706f141b9036c6ee77efbffe2bcb7edb656755a5'
      exitCode: 0
      finishedAt: '2022-06-01T10:13:49Z'
      message: Sidecar container successfully stopped by nop image
      reason: Completed
      startedAt: '2022-06-01T10:13:37Z'
startTime: '2022-06-01T10:13:30Z'
steps:
  - container: step-docker-build
    imageID: 'docker-pullable://docker@sha256:5bc07a93c9b28e57a58d57fbcf437d1551ff80ae33b4274fb60a1ade2d6c9da4'
    name: docker-build
    terminated:
      containerID: 'docker://18aa9111f180f9cfc6b9d86d5ef1da9f8dbe83375bb282bba2776b5bbbcaabfb'
      exitCode: 0
      finishedAt: '2022-06-01T10:13:46Z'
      reason: Completed
      startedAt: '2022-06-01T10:13:42Z'
taskSpec:
  params:
    - default: 'docker:stable'
      description: The location of the docker builder image.
      name: builder_image
      type: string
    - default: 'docker:dind'
      description: The location of the docker-in-docker image.
      name: dind_image
      type: string
    - default: .
      description: Path to the directory to use as context.
      name: context
      type: string
  sidecars:
    - args:
        - '--storage-driver=vfs'
        - '--debug'
        - '--userland-proxy=false'
      env:
        - name: DOCKER_TLS_CERTDIR
          value: /certs
      image: $(params.dind_image)
      name: server
      readinessProbe:
        exec:
          command:
            - ls
            - /certs/client/ca.pem
        periodSeconds: 1
      resources:
        requests:
          memory: 512Mi
      securityContext:
        privileged: true
      volumeMounts:
        - mountPath: /certs/client
          name: dind-certs
  steps:
    - args:
        - '--storage-driver=vfs'
        - '--debug'
      env:
        - name: DOCKER_HOST
          value: 'tcp://localhost:2376'
        - name: DOCKER_TLS_VERIFY
          value: '1'
        - name: DOCKER_CERT_PATH
          value: /certs/client
        - name: DOCKER_USER
          valueFrom:
            secretKeyRef:
              key: username
              name: docker-auth
        - name: DOCKER_TOKEN
          valueFrom:
            secretKeyRef:
              key: password
              name: docker-auth
        - name: DIND_CONFIG
          valueFrom:
            configMapKeyRef:
              key: file
              name: dind-env
      image: docker
      name: docker-build
      resources: {}
      script: |
        #!/usr/bin/env sh
        set -e
        
        pwd
        ls -ltr /workspace/source
        docker run --privileged -v "/workspace/source:/workspace" busybox ls -ltr /workspace
        
        
      securityContext:
        privileged: true
      volumeMounts:
        - mountPath: /certs/client
          name: dind-certs
      workingDir: $(workspaces.source.path)
  volumes:
    - emptyDir: {}
      name: dind-certs
  workspaces:
    - name: source

Solution

  • basically we have to attach volume to sidecar, since docker run happens in side card

       volumeMounts:
        - mountPath: /certs/client
          name: dind-certs
        - name: $(workspaces.source.volume)
          mountPath: $(workspaces.source.path)