<p>I have following nginx config file (named <em>nginx-daemonset.conf</em>) that I want to use inside my Daemonset:</p>
<pre><code>events {
    worker_connections 1024;
}

http {

    server {
        listen 80;
        
        location / {
            proxy_pass http://my-nginx;
        }
    }
}
</code></pre>
<p>I created a ConfigMap using following command: <code>kubectl create configmap nginx2.conf --from-file=nginx-daemonset.conf</code></p>
<p>I have following DaemonSet (<em>nginx-daemonset-deployment.yml</em>) inside which I am trying to mount this ConfigMap - so the previous nginx config file is used:</p>
<pre><code>apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx-daemonset
  namespace: kube-system
  labels:
    k8s-app: nginx-daemonset
spec:
  selector:
    matchLabels:
      name: nginx-daemonset
  template:
    metadata:
      labels:
        name: nginx-daemonset
    spec:
      tolerations:
      # this toleration is to have the daemonset runnable on master nodes
      # remove it if your masters can't run pods
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
      containers:
      - name: nginx
        image: nginx
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: nginx2-conf
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
      volumes:
      - name: nginx2-conf
        configMap:
            name: nginx2.conf
</code></pre>
<p>I deployed this Daemonset using <code>kubectl apply -f nginx-daemonset-deployment.yml</code> but my newly created Pod is crashing with the following error:</p>
<p><em><code>Error: failed to start container &quot;nginx&quot;: Error response from daemon: OCI runtime create failed: container_linux.go:370: starting container process caused: process_linux.go:459: container init caused: rootfs_linux.go:59: mounting &quot;/var/lib/kubelet/pods/cd9f6f7b-31db-4ab3-bbc0-189e1d392979/volume-subpaths/nginx2-conf/nginx/0&quot; to rootfs at &quot;/var/lib/docker/overlay2/b21ccba23347a445fa40eca943a543c1103d9faeaaa0218f97f8e33bacdd4bb3/merged/etc/nginx/nginx.conf&quot; 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</code></em></p>
<p>I did another Deployment with different nginx config before and everything worked fine, so the problem is probably somehow related to DaemonSet.</p>
<p>Please, how do I get over this error and mount the config file properly?</p>

<p>first create your config file as a ConfigMap like nginx-conf:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
data:
  envfile: |
    events {
        worker_connections 1024;
    }

    http {

        server {
            listen 80;
            
            location / {
                proxy_pass http://my-nginx;
            }
        }
    }

</code></pre>
<p>then create your DaemonSet, volumes, configMap and finial y mount volumeMounts with subPath:</p>
<pre><code>apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - name: http
          containerPort: 80
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
          readOnly: true
          name: nginx-vol
      volumes:
      - name: nginx-vol
        configMap:
          name: nginx-conf
          items:
            - key: envfile
              path: nginx.conf
</code></pre>
<p>note that for <em>file mounting</em> instead of <em>directory mounting</em> you must use &quot;path in configMap&quot; and &quot;subPath in volumeMounts&quot;.</p>
