Answers: <p><code>volumeMount</code> based on <code>configMap</code> actually creates the files for the data keys. You don't need the filename in the <code>mountPath</code> or the <code>subPath</code></p>
  <pre class="lang-bash prettyprint-override"><code>$ cat &lt;&lt;EOF | kubectl apply -f -
  apiVersion: v1
  data:
    settings.conf: |
      test: config
      multi: line
  kind: ConfigMap
  metadata:
    name: test-config
  ---
  apiVersion: v1
  kind: Pod
  metadata:
    name: dapi-test-pod
  spec:
    containers:
      - name: test-container
        image: k8s.gcr.io/busybox
        command: [ &quot;/bin/sh&quot;, &quot;-c&quot;, &quot;ls /etc/config/&quot; ]
        volumeMounts:
        - name: config-volume
          mountPath: /etc/config
    restartPolicy: Never
    volumes:
      - name: config-volume
        configMap:
          name: test-config
  EOF
  
  $ kubectl logs dapi-test-pod
  settings.conf
  </code></pre>
  <p>Ref:</p>
  <ul>
  <li><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/</a></li>
  </ul>
  
Answers: CC BY-SA 4.0
Answers: 2021-05-23T18:38:06.587Z
Answers: I have tried this already but it didn't help.
Answers: Chang Zhao
Answers: CC BY-SA 4.0
Answers: 2021-05-24T02:08:24.313Z
Answers: I have updated my answer with the correct example and reference.
Answers: Faheem
Answers: CC BY-SA 4.0
Answers: 2021-05-23T18:24:07.637Z
Answers: 2021-05-24T02:06:40.983Z
Answers: 2021-05-24T02:06:40.983Z
Answers: Faheem
Body: <p>I have this prometheus deployment manifest file: to create prometheus deployment</p>
  <pre><code>---
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: prometheus
    namespace: monitoring
    labels:
      app: prometheus
  spec:
    replicas: 1
    strategy:
      rollingUpdate:
        maxSurge: 1
        maxUnavailable: 1
      type: RollingUpdate
    selector:
      matchLabels:
        app: prometheus
    template:
      metadata:
        labels:
          app: prometheus
        annotations:
          prometheus.io/scrape: &quot;true&quot;
          prometheus.io/port: &quot;9090&quot;
      spec:
        nodeSelector:
          boardType: aarch64
        initContainers:
        - name: prometheus-data-permission-setup
          image: busybox
          command: [&quot;/bin/chmod&quot;,&quot;-R&quot;,&quot;777&quot;, &quot;/data&quot;]
          volumeMounts:
          - name: prometheus-data
            mountPath: /data
        containers:
        - name: prometheus
          image: rycus86/prometheus:latest
          imagePullPolicy: IfNotPresent
          args:
            - '--storage.tsdb.path=/data'
            - '--config.file=/etc/prometheus/prometheus.yml'
          command:
          - /bin/prometheus
          ports:
          - name: web
            containerPort: 9090
          volumeMounts:
          - name: config-volume
            mountPath: /etc/prometheus/prometheus.yml
            subPath: prometheus.yml
          - name: prometheus-data
            mountPath: /data
            readOnly: false
          resources:
            requests:
              cpu: &quot;250m&quot;
              ephemeral-storage: &quot;4Gi&quot;
            limits:
              memory: &quot;200Mi&quot;
              cpu: &quot;500m&quot;
              ephemeral-storage: &quot;20Gi&quot;
        restartPolicy: Always
        securityContext: {}
        terminationGracePeriodSeconds: 30
        volumes:
        - name: config-volume
          configMap:
            name: prometheus-config
        - name: prometheus-data
          hostPath:
            path: /data
            type: Directory
        serviceAccountName: prometheus
  </code></pre>
  <p>but this error that &quot;trying to mount a directory onto a file &quot;</p>
  <pre><code>Events:
    Type     Reason     Age               From               Message
    ----     ------     ----              ----               -------
    Normal   Scheduled  30s               default-scheduler  Successfully assigned monitoring/prometheus-b568c9967-tmdq7 to rockpro64
    Normal   Pulling    28s               kubelet            Pulling image &quot;busybox&quot;
    Normal   Pulled     26s               kubelet            Successfully pulled image &quot;busybox&quot; in 1.57966103s
    Normal   Created    26s               kubelet            Created container prometheus-data-permission-setup
    Normal   Started    25s               kubelet            Started container prometheus-data-permission-setup
    Warning  Failed     23s               kubelet            Error: failed to start container &quot;prometheus&quot;: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused &quot;process_linux.go:449: container init caused \&quot;rootfs_linux.go:58: mounting \\\&quot;/var/lib/kubelet/pods/da8d1a99-a6d0-48f5-ad5e-cbc5db136145/volume-subpaths/config-volume/prometheus/0\\\&quot; to rootfs \\\&quot;/var/lib/docker/overlay2/32aead2ba4f70863e9cf643a1bad1fb0832eb43c217898bfe77cc67ab400d702/merged\\\&quot; at \\\&quot;/var/lib/docker/overlay2/32aead2ba4f70863e9cf643a1bad1fb0832eb43c217898bfe77cc67ab400d702/merged/etc/prometheus/prometheus.yml\\\&quot; caused \\\&quot;not a directory\\\&quot;\&quot;&quot;: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
    Warning  Failed     23s               kubelet            Error: failed to start container &quot;prometheus&quot;: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused &quot;process_linux.go:449: container init caused \&quot;rootfs_linux.go:58: mounting \\\&quot;/var/lib/kubelet/pods/da8d1a99-a6d0-48f5-ad5e-cbc5db136145/volume-subpaths/config-volume/prometheus/0\\\&quot; to rootfs \\\&quot;/var/lib/docker/overlay2/9dcb8fa640d8fc93f56755f5daeb2a350c482f5f3f26faee03edb320d52f53ff/merged\\\&quot; at \\\&quot;/var/lib/docker/overlay2/9dcb8fa640d8fc93f56755f5daeb2a350c482f5f3f26faee03edb320d52f53ff/merged/etc/prometheus/prometheus.yml\\\&quot; caused \\\&quot;not a directory\\\&quot;\&quot;&quot;: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
    Warning  BackOff    22s               kubelet            Back-off restarting failed container
    Normal   Pulled     7s (x3 over 24s)  kubelet            Container image &quot;rycus86/prometheus:latest&quot; already present on machine
    Normal   Created    7s (x3 over 24s)  kubelet            Created container prometheus
    Warning  Failed     6s                kubelet            Error: failed to start container &quot;prometheus&quot;: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused &quot;process_linux.go:449: container init caused \&quot;rootfs_linux.go:58: mounting \\\&quot;/var/lib/kubelet/pods/da8d1a99-a6d0-48f5-ad5e-cbc5db136145/volume-subpaths/config-volume/prometheus/0\\\&quot; to rootfs \\\&quot;/var/lib/docker/overlay2/5b263ca359000d143fd5f11fb26218ead46eabd1a1aeb39ce1a7b48302a2382c/merged\\\&quot; at \\\&quot;/var/lib/docker/overlay2/5b263ca359000d143fd5f11fb26218ead46eabd1a1aeb39ce1a7b48302a2382c/merged/etc/prometheus/prometheus.yml\\\&quot; caused \\\&quot;not a directory\\\&quot;\&quot;&quot;: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
  </code></pre>
  <p>I have prometheus-config configmap already setup.</p>
  <pre><code># kubectl get cm -n monitoring
  NAME                DATA   AGE
  kube-root-ca.crt    1      35h
  prometheus-config   1      41m
  </code></pre>
  <p>what might have gone wrong ?</p>
  
Tags: <kubernetes>
Title: Why am I getting error trying to mount a directory onto a file?
UserDisplayName: Chang Zhao
ThreadText: # Why am I getting error trying to mount a directory onto a file?
  
  (Asked by: username_0 on 2021-05-23)
  
  I have this prometheus deployment manifest file: to create prometheus deployment
  
  ```
  ---
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: prometheus
    namespace: monitoring
    labels:
      app: prometheus
  spec:
    replicas: 1
    strategy:
      rollingUpdate:
        maxSurge: 1
        maxUnavailable: 1
      type: RollingUpdate
    selector:
      matchLabels:
        app: prometheus
    template:
      metadata:
        labels:
          app: prometheus
        annotations:
          prometheus.io/scrape: "true"
          prometheus.io/port: "9090"
      spec:
        nodeSelector:
          boardType: aarch64
        initContainers:
        - name: prometheus-data-permission-setup
          image: busybox
          command: ["/bin/chmod","-R","777", "/data"]
          volumeMounts:
          - name: prometheus-data
            mountPath: /data
        containers:
        - name: prometheus
          image: rycus86/prometheus:latest
          imagePullPolicy: IfNotPresent
          args:
            - '--storage.tsdb.path=/data'
            - '--config.file=/etc/prometheus/prometheus.yml'
          command:
          - /bin/prometheus
          ports:
          - name: web
            containerPort: 9090
          volumeMounts:
          - name: config-volume
            mountPath: /etc/prometheus/prometheus.yml
            subPath: prometheus.yml
          - name: prometheus-data
            mountPath: /data
            readOnly: false
          resources:
            requests:
              cpu: "250m"
              ephemeral-storage: "4Gi"
            limits:
              memory: "200Mi"
              cpu: "500m"
              ephemeral-storage: "20Gi"
        restartPolicy: Always
        securityContext: {}
        terminationGracePeriodSeconds: 30
        volumes:
        - name: config-volume
          configMap:
            name: prometheus-config
        - name: prometheus-data
          hostPath:
            path: /data
            type: Directory
        serviceAccountName: prometheus
  
  ```
  
  but this error that "trying to mount a directory onto a file "
  
  ```
  Events:
    Type     Reason     Age               From               Message
    ----     ------     ----              ----               -------
    Normal   Scheduled  30s               default-scheduler  Successfully assigned monitoring/prometheus-b568c9967-tmdq7 to rockpro64
    Normal   Pulling    28s               kubelet            Pulling image "busybox"
    Normal   Pulled     26s               kubelet            Successfully pulled image "busybox" in 1.57966103s
    Normal   Created    26s               kubelet            Created container prometheus-data-permission-setup
    Normal   Started    25s               kubelet            Started container prometheus-data-permission-setup
    Warning  Failed     23s               kubelet            Error: failed to start container "prometheus": Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"rootfs_linux.go:58: mounting \\\"/var/lib/kubelet/pods/da8d1a99-a6d0-48f5-ad5e-cbc5db136145/volume-subpaths/config-volume/prometheus/0\\\" to rootfs \\\"/var/lib/docker/overlay2/32aead2ba4f70863e9cf643a1bad1fb0832eb43c217898bfe77cc67ab400d702/merged\\\" at \\\"/var/lib/docker/overlay2/32aead2ba4f70863e9cf643a1bad1fb0832eb43c217898bfe77cc67ab400d702/merged/etc/prometheus/prometheus.yml\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
    Warning  Failed     23s               kubelet            Error: failed to start container "prometheus": Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"rootfs_linux.go:58: mounting \\\"/var/lib/kubelet/pods/da8d1a99-a6d0-48f5-ad5e-cbc5db136145/volume-subpaths/config-volume/prometheus/0\\\" to rootfs \\\"/var/lib/docker/overlay2/9dcb8fa640d8fc93f56755f5daeb2a350c482f5f3f26faee03edb320d52f53ff/merged\\\" at \\\"/var/lib/docker/overlay2/9dcb8fa640d8fc93f56755f5daeb2a350c482f5f3f26faee03edb320d52f53ff/merged/etc/prometheus/prometheus.yml\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
    Warning  BackOff    22s               kubelet            Back-off restarting failed container
    Normal   Pulled     7s (x3 over 24s)  kubelet            Container image "rycus86/prometheus:latest" already present on machine
    Normal   Created    7s (x3 over 24s)  kubelet            Created container prometheus
    Warning  Failed     6s                kubelet            Error: failed to start container "prometheus": Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"rootfs_linux.go:58: mounting \\\"/var/lib/kubelet/pods/da8d1a99-a6d0-48f5-ad5e-cbc5db136145/volume-subpaths/config-volume/prometheus/0\\\" to rootfs \\\"/var/lib/docker/overlay2/5b263ca359000d143fd5f11fb26218ead46eabd1a1aeb39ce1a7b48302a2382c/merged\\\" at \\\"/var/lib/docker/overlay2/5b263ca359000d143fd5f11fb26218ead46eabd1a1aeb39ce1a7b48302a2382c/merged/etc/prometheus/prometheus.yml\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
  
  ```
  
  I have prometheus-config configmap already setup.
  
  ```
  # kubectl get cm -n monitoring
  NAME                DATA   AGE
  kube-root-ca.crt    1      35h
  prometheus-config   1      41m
  
  ```
  
  what might have gone wrong ?
  
  **Tags:** kubernetes
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-05-23. Score: 3
  
  `volumeMount` based on `configMap` actually creates the files for the data keys. You don't need the filename in the `mountPath` or the `subPath`
  
  ```bash
  $ cat <<EOF | kubectl apply -f -
  apiVersion: v1
  data:
    settings.conf: |
      test: config
      multi: line
  kind: ConfigMap
  metadata:
    name: test-config
  ---
  apiVersion: v1
  kind: Pod
  metadata:
    name: dapi-test-pod
  spec:
    containers:
      - name: test-container
        image: k8s.gcr.io/busybox
        command: [ "/bin/sh", "-c", "ls /etc/config/" ]
        volumeMounts:
        - name: config-volume
          mountPath: /etc/config
    restartPolicy: Never
    volumes:
      - name: config-volume
        configMap:
          name: test-config
  EOF
  
  $ kubectl logs dapi-test-pod
  settings.conf
  
  ```
  
  Ref:
  
  * <https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/>
  
  > **Comments:**
  > **username_0**: I have tried this already but it didn't help.  
  > **username_1**: I have updated my answer with the correct example and reference.  
  
  ---
  
  