Answers: <p>To solve this, you need to include the volume mounts in the containers <a href="https://cloud.google.com/kubernetes-engine/docs/concepts/volumes#using_volumes" rel="nofollow noreferrer">described here</a>:</p>
  
  <p><strong>Sample Jenkinsfile</strong>:</p>
  
  <pre><code>pipeline {
      agent {
      kubernetes {
        label 'test-pod'
        defaultContainer 'jnlp'
        yaml """
  apiVersion: v1
  kind: Pod
  spec:
    containers:
    - name: frontend-test
      image: centos:7
      command:
      - cat
      tty: true
      volumeMounts:
      - mountPath: '/opt/app/shared'
        name: sharedvolume
    - name: backend-test
      image: centos:7
      command:
      - cat
      tty: true   
      volumeMounts:
      - mountPath: '/opt/app/shared'
        name: sharedvolume
    volumes:
    - name: sharedvolume
      emptyDir: {}
  """
          }
      }
      stages {
          stage('Test Pipeline Configuration') {
              steps {
                  container('frontend-test') {
                      sh 'touch /opt/app/shared/test_file'
                  }
                  container('backend-test') {
                      sh 'ls /opt/app/shared/test_file ; echo $?'
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Result</strong>:</p>
  
  <p>This solved the problem.</p>
  
  <pre><code>[Pipeline] podTemplate
  [Pipeline] {
  [Pipeline] node
  Still waiting to schedule task
  All nodes of label ‘test-pod’ are offline
  Agent test-pod-wv12q-brfxj is provisioned from template Kubernetes Pod Template
  Agent specification [Kubernetes Pod Template] (test-pod): 
  
  Running on test-pod-wv12q-brfxj in /home/jenkins/workspace/pipeline-test
  [Pipeline] {
  [Pipeline] container
  [Pipeline] {
  [Pipeline] stage
  [Pipeline] { (Test Pipeline Configuration)
  [Pipeline] container
  [Pipeline] {
  [Pipeline] sh
  [pipeline-test] Running shell script
  + touch /opt/app/shared/test_file
  [Pipeline] }
  [Pipeline] // container
  [Pipeline] container
  [Pipeline] {
  [Pipeline] sh
  [pipeline-test] Running shell script
  + ls /opt/app/shared/test_file
  /opt/app/shared/test_file
  + echo 0
  0
  [Pipeline] }
  [Pipeline] // container
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] }
  [Pipeline] // container
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] }
  [Pipeline] // podTemplate
  [Pipeline] End of Pipeline
  Finished: SUCCESS
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-09-07T22:06:06.607Z
Answers: quick question : may i know why u use "test-pod" ? did u configure it  in  /configure page? if so, please share with me your plugins conf
Answers: Abdennour TOUMI
Answers: CC BY-SA 4.0
Answers: 2018-08-05T13:57:36.347Z
Answers: 2018-08-05T13:57:36.347Z
Answers: Inbar Rose
Body: <p>I have a Jenkins Pipeline using Kubernetes Agent.</p>
  
  <p>I schedule multiple containers in my pod template.</p>
  
  <p>I recently wanted to add volumes that would be shared some of the containers and am having difficulties.</p>
  
  <p>I need to know how to create a volume that will be shared between 2 or more containers in a pod. That volume should mount to a specific path on each container.</p>
  
  <p><strong>Specifics</strong>:</p>
  
  <ul>
  <li>Using GCE (Google Cloud Engine) with Kubernetes Jenkins</li>
  <li>Using emptyDir volumes</li>
  </ul>
  
  <p>There also seems to be a <em>severe lack of documentation</em> for this. So any direction on that would also be appreciated. The <a href="https://github.com/jenkinsci/kubernetes-plugin#container-configuration" rel="nofollow noreferrer">documentation I did find</a> says that the way to do this is to define <a href="https://github.com/jenkinsci/kubernetes-plugin#pod-and-container-template-configuration" rel="nofollow noreferrer">volumes in the pipeline that will be shared by all containers.</a></p>
  
  <p><strong>Sample Jenkinsfile</strong>:</p>
  
  <p>Below is not my actual pipeline, but it recreates the issue.</p>
  
  <pre><code>pipeline {
      agent {
      kubernetes {
        label 'test-pod'
        defaultContainer 'jnlp'
        yaml """
  apiVersion: v1
  kind: Pod
  spec:
    containers:
    - name: frontend-test
      image: centos:7
      command:
      - cat
      tty: true
    - name: backend-test
      image: centos:7
      command:
      - cat
      tty: true   
    volumes:
    - name: sharedvolume
      emptyDir: {}
      mountPath: '/opt/app/shared'
  """
          }
      }
      stages {
          stage('Test Pipeline Configuration') {
              steps {
                  container('frontend-test') {
                      sh 'touch /opt/app/shared/test_file'
                  }
                  container('backend-test') {
                      sh 'ls /opt/app/shared/test_file ; echo $?'
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Result</strong>:</p>
  
  <pre><code>[Pipeline] podTemplate
  [Pipeline] {
  [Pipeline] node
  Still waiting to schedule task
  All nodes of label ‘test-pod’ are offline
  Agent test-pod-5t0gj-wln8d is provisioned from template Kubernetes Pod Template
  Agent specification [Kubernetes Pod Template] (test-pod): 
  
  Running on test-pod-5t0gj-wln8d in /home/jenkins/workspace/pipeline-test
  [Pipeline] {
  [Pipeline] container
  [Pipeline] {
  [Pipeline] stage
  [Pipeline] { (Test Pipeline Configuration)
  [Pipeline] container
  [Pipeline] {
  [Pipeline] sh
  [pipeline-test] Running shell script
  + touch /opt/app/shared/test_file
  touch: cannot touch '/opt/app/shared/test_file': No such file or directory
  [Pipeline] }
  [Pipeline] // container
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] }
  [Pipeline] // container
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] }
  [Pipeline] // podTemplate
  [Pipeline] End of Pipeline
  ERROR: script returned exit code 1
  Finished: FAILURE
  </code></pre>
  
Tags: <kubernetes><jenkins-pipeline><jenkinsfile>
Title: Jenkins Pipeline Kubernetes Agent shared Volumes
UserDisplayName: Inbar Rose
ThreadText: # Jenkins Pipeline Kubernetes Agent shared Volumes
  
  (Asked by: username_0 on 2018-08-05)
  
  I have a Jenkins Pipeline using Kubernetes Agent.
  
  I schedule multiple containers in my pod template.
  
  I recently wanted to add volumes that would be shared some of the containers and am having difficulties.
  
  I need to know how to create a volume that will be shared between 2 or more containers in a pod. That volume should mount to a specific path on each container.
  
  **Specifics**:
  
  * Using GCE (Google Cloud Engine) with Kubernetes Jenkins
  * Using emptyDir volumes
  
  There also seems to be a *severe lack of documentation* for this. So any direction on that would also be appreciated. The [documentation I did find](https://github.com/jenkinsci/kubernetes-plugin#container-configuration) says that the way to do this is to define [volumes in the pipeline that will be shared by all containers.](https://github.com/jenkinsci/kubernetes-plugin#pod-and-container-template-configuration)
  
  **Sample Jenkinsfile**:
  
  Below is not my actual pipeline, but it recreates the issue.
  
  ```
  pipeline {
      agent {
      kubernetes {
        label 'test-pod'
        defaultContainer 'jnlp'
        yaml """
  apiVersion: v1
  kind: Pod
  spec:
    containers:
    - name: frontend-test
      image: centos:7
      command:
      - cat
      tty: true
    - name: backend-test
      image: centos:7
      command:
      - cat
      tty: true   
    volumes:
    - name: sharedvolume
      emptyDir: {}
      mountPath: '/opt/app/shared'
  """
          }
      }
      stages {
          stage('Test Pipeline Configuration') {
              steps {
                  container('frontend-test') {
                      sh 'touch /opt/app/shared/test_file'
                  }
                  container('backend-test') {
                      sh 'ls /opt/app/shared/test_file ; echo $?'
                  }
              }
          }
      }
  }
  
  ```
  
  **Result**:
  
  ```
  [Pipeline] podTemplate
  [Pipeline] {
  [Pipeline] node
  Still waiting to schedule task
  All nodes of label ‘test-pod’ are offline
  Agent test-pod-5t0gj-wln8d is provisioned from template Kubernetes Pod Template
  Agent specification [Kubernetes Pod Template] (test-pod): 
  
  Running on test-pod-5t0gj-wln8d in /home/jenkins/workspace/pipeline-test
  [Pipeline] {
  [Pipeline] container
  [Pipeline] {
  [Pipeline] stage
  [Pipeline] { (Test Pipeline Configuration)
  [Pipeline] container
  [Pipeline] {
  [Pipeline] sh
  [pipeline-test] Running shell script
  + touch /opt/app/shared/test_file
  touch: cannot touch '/opt/app/shared/test_file': No such file or directory
  [Pipeline] }
  [Pipeline] // container
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] }
  [Pipeline] // container
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] }
  [Pipeline] // podTemplate
  [Pipeline] End of Pipeline
  ERROR: script returned exit code 1
  Finished: FAILURE
  
  ```
  
  **Tags:** kubernetes, jenkins-pipeline, jenkinsfile
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-08-05. Score: 4
  
  To solve this, you need to include the volume mounts in the containers [described here](https://cloud.google.com/kubernetes-engine/docs/concepts/volumes#using_volumes):
  
  **Sample Jenkinsfile**:
  
  ```
  pipeline {
      agent {
      kubernetes {
        label 'test-pod'
        defaultContainer 'jnlp'
        yaml """
  apiVersion: v1
  kind: Pod
  spec:
    containers:
    - name: frontend-test
      image: centos:7
      command:
      - cat
      tty: true
      volumeMounts:
      - mountPath: '/opt/app/shared'
        name: sharedvolume
    - name: backend-test
      image: centos:7
      command:
      - cat
      tty: true   
      volumeMounts:
      - mountPath: '/opt/app/shared'
        name: sharedvolume
    volumes:
    - name: sharedvolume
      emptyDir: {}
  """
          }
      }
      stages {
          stage('Test Pipeline Configuration') {
              steps {
                  container('frontend-test') {
                      sh 'touch /opt/app/shared/test_file'
                  }
                  container('backend-test') {
                      sh 'ls /opt/app/shared/test_file ; echo $?'
                  }
              }
          }
      }
  }
  
  ```
  
  **Result**:
  
  This solved the problem.
  
  ```
  [Pipeline] podTemplate
  [Pipeline] {
  [Pipeline] node
  Still waiting to schedule task
  All nodes of label ‘test-pod’ are offline
  Agent test-pod-wv12q-brfxj is provisioned from template Kubernetes Pod Template
  Agent specification [Kubernetes Pod Template] (test-pod): 
  
  Running on test-pod-wv12q-brfxj in /home/jenkins/workspace/pipeline-test
  [Pipeline] {
  [Pipeline] container
  [Pipeline] {
  [Pipeline] stage
  [Pipeline] { (Test Pipeline Configuration)
  [Pipeline] container
  [Pipeline] {
  [Pipeline] sh
  [pipeline-test] Running shell script
  + touch /opt/app/shared/test_file
  [Pipeline] }
  [Pipeline] // container
  [Pipeline] container
  [Pipeline] {
  [Pipeline] sh
  [pipeline-test] Running shell script
  + ls /opt/app/shared/test_file
  /opt/app/shared/test_file
  + echo 0
  0
  [Pipeline] }
  [Pipeline] // container
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] }
  [Pipeline] // container
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] }
  [Pipeline] // podTemplate
  [Pipeline] End of Pipeline
  Finished: SUCCESS
  
  ```
  
  > **Comments:**
  > **username_1**: quick question : may i know why u use "test-pod" ? did u configure it  in  /configure page? if so, please share with me your plugins conf  
  
  ---
  
  