How to mount entire directory in Kubernetes using configmap?

You can mount the ConfigMap as a special volume into your container.

In this case, the mount folder will show each of the keys as a file in the mount folder and the files will have the map values as content.

From the Kubernetes documentation:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
...
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config

I'm feeling really stupid now.

Sorry, My fault.

The Docker container did not start so I was manually staring it using docker run -it --entrypoint='/bin/bash' and I could not see any files from the configMap.

This does not work since docker don't know anything about my deployment until Kubernetes starts it.

The docker image was failing and the Kubernetes config was correct all the time.

I was debugging it wrong.


With your config, you're going to mount each file listed in your configmap.

If you need to mount all file in a folder, you shouldn't use configmap, but a persistenceVolume and persistenceVolumeClaims:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume-jenkins
spec:
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/pv-jenkins"

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-jenkins
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: ""
  resources:
    requests:
      storage: 50Gi

In your deployment.yml:

 volumeMounts:
        - name: jenkins-persistent-storage
          mountPath: /data

  volumes:
        - name: jenkins-persistent-storage
          persistentVolumeClaim:
            claimName: pv-claim-jenkins

You can also use the following:

kubectl create configmap my-config --from-file=/etc/configs

to create the config map with all files in that folder.

Hope this helps.