Mount kubernetes volume in docker container without erasing the container's data

10/18/2019

I'm using the kubernetes plugin to setup a pipeline on jenkins to compile some code.

MY GOAL:
In this pipeline, I'm trying to access some data from a docker container to use it as a cache in a second on (as shown below).

apiVersion: v1
kind: Pod
metadata:
  name: cache-test
spec:
  restartPolicy: Never
  volumes:
  - name: shared-data
    emptyDir: {}

  containers:

  - name: cache-container
    image: cache:latest
    volumeMounts:
    - name: shared-data
      mountPath: /cache
    command: 
      - cat


  - name: debian-container
    image: debian
    volumeMounts:
    - name: shared-data
      mountPath: /pod-data
    command: 
      - cat

PROBLEM:
My issue is that when I mount my shared-folder in /cache directly, all my data get erased (overwritten).

WORK AROUND:
One work around would be to to create an intermediate directory where I can copy my data:

apiVersion: v1
kind: Pod
metadata:
  name: cache-test
spec:
  restartPolicy: Never
  volumes:
  - name: shared-data
    emptyDir: {}

  containers:

  - name: cache-container
    image: cache:latest
    volumeMounts:
    - name: shared-data
      mountPath: /shared-folder
    command: 
      - cat


  - name: debian-container
    image: debian
    volumeMounts:
    - name: shared-data
      mountPath: /pod-data
    command: 
      - cat

And the in my Jenkins pipeline add this step:

          container('cache-container') {
                sh """#!/usr/bin/env bash
                set -exu
                cp -r /cache/* /shared-folder
                """
              } // container

QUESTION:
Is there a way to avoid this copy step? Maybe a kubernetes volume setup that doesn't overwrite what's in the container? I went through the documentation couple times without finding anything..

-- ibrahim elouard
docker
jenkins
kubernetes
mounted-volumes

0 Answers