<p>I used a configMap to store the mount path value, but when i call in my pod it doesn't work it seems that the mountPath property can not be assigned to an env variable.</p>

<p>here is my code :</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
        env:
        - name: MOUNT_PATH
          valueFrom:
           configMapKeyRef:
            name: my-configMap
            key: MOUNT_PATH
        volumeMounts:
        - name: nginx-volume
          mountPath: $(MOUNT_PATH)
      volumes:
      - name: nginx-volume
        persistentVolumeClaim:
         claimName: my-pvc

     ---


apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configMap
  namespace: default
data:
  MOUNT_PATH: "/opt/somepath"
</code></pre>

<p>You can achieve what you want using <a href="https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath-expanded-environment" rel="noreferrer">subPath</a> if you are using k8s 1.17 or higher.</p>
<pre><code>    env:
    - name: MOUNT_PATH
      valueFrom:
        configMapKeyRef:
          name: myconfigmap
          key: MOUNT_PATH
    volumeMounts:
    - name: nginx-volume
      mountPath: /opt
      subPathExpr: $(MOUNT_PATH)
  volumes:
  - name: nginx-volume
    persistentVolumeClaim:
      claimName: my-pvc

  ---

  apiVersion: v1
  kind: ConfigMap
  metadata:
    name: myconfigmap
    namespace: default
  data:
    MOUNT_PATH: &quot;somepath&quot;
</code></pre>
<p>Result within the container:</p>
<pre><code># ls -l /opt
total 4
drwxrwxrwx    2 root     root          4096 Oct 29 14:58 somepath
</code></pre>
