<p>After spending a couple of hours I found that the relative path is not supported as a mounted path in Kubernetes. I found the reference here <a href="https://github.com/kubernetes/kubernetes/pull/48815/files" rel="nofollow noreferrer">mountpath should be absolute</a> if it is yes then why it doesn't have that capability of the relative path could anyone please explain a bit deeper?</p>

<pre class="lang-yaml prettyprint-override"><code>    Example code:
    apiVersion: v1
    kind: Pod
    metadata:
      name: task-pv-pod
    spec:
      volumes:
        - name: task-pv-storage
          hostPath:
            # directory location on host
            # path: "./code" # this is not supporting
            path: "/var/www/html/kubernetes/code"  # this is supporting
            # this field is optional
            type: DirectoryOrCreate
      containers:
        - name: task-pv-container
          image: nginx
          ports:
            - containerPort: 80
              name: "http-server"
          volumeMounts:
            - mountPath: "/usr/share/nginx/html"
              name: task-pv-storage
</code></pre>

<p>Under the <code>code</code> directory in the above code example, I just have an <code>index.html</code> page</p>

<p><strong>Screenshot of the project structure:</strong></p>

<p><a href="https://i.stack.imgur.com/OOTb9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OOTb9.png" alt="enter image description here"></a></p>

<p>If I use <code>path: "./code"</code> then the error shows like this: </p>

<pre><code>Error response from daemon: create ./code: "./code" includes invalid
characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.
If you intended to pass a host directory, use absolute path.
</code></pre>

<p>Thanks in advance!! </p>

<p><strong>I believe the source of your confusion is that you are running a single node Cluster</strong>, like Minikube.</p>
<ul>
<li>The process chain (roughly summarized) is:</li>
</ul>
<p><em>Kubectl &gt; Kube-API(master) &gt; Kubelet agent (node) &gt; Pod creation (as specified on the yaml manifest).</em></p>
<p>In a single node cluster all these agents are on the same computer, that's why the files in <code>/var/www/html/kubernetes/code</code> were mounted to the pod.</p>
<ul>
<li>I'll clarify it with this example:
<ul>
<li>You have a cluster with 3 nodes.</li>
<li>You manage your nodes remotely, with kubectl from your notebook.</li>
</ul>
</li>
</ul>
<p>When you use <code>hostPath</code> the files must exist on the <code>node</code>, not on your notebook, because it's not the <code>kubectl</code> on your computer that will trigger the creation of the pod and mount the files/directories.</p>
<p>This is the job of the <a href="https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet/" rel="nofollow noreferrer"><code>kubelet</code></a> agent of the node that will create the pod and apply it's manifest. This is why you need to specify the full path of the file/dir you want to mount.</p>
<hr />
<p>According to <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/#create-a-persistentvolume" rel="nofollow noreferrer">PersistentVolumes</a> documentation:</p>
<blockquote>
<p>Kubernetes supports <code>hostPath</code> <strong>for development and testing on a single-node cluster</strong>. A hostPath PersistentVolume uses a file or directory on the Node to emulate network-attached storage.</p>
<p><strong>In a production cluster, you would not use hostPath</strong>. Instead a cluster administrator would provision a network resource like a Google Compute Engine persistent disk, an NFS share, or an Amazon Elastic Block Store volume. Cluster administrators can also use  <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#storageclass-v1-storage" rel="nofollow noreferrer">StorageClasses</a>  to set up  <a href="https://kubernetes.io/blog/2016/10/dynamic-provisioning-and-storage-in-kubernetes" rel="nofollow noreferrer">dynamic provisioning</a>.</p>
</blockquote>
<p>Watch out when using <a href="https://kubernetes.io/docs/concepts/storage/volumes/#hostpath" rel="nofollow noreferrer">hostPath</a> type, because:</p>
<ul>
<li>Pods with identical configuration (such as created from a podTemplate) may behave differently on different nodes due to different files on the nodes.</li>
<li>when Kubernetes adds resource-aware scheduling, as is planned, it will not be able to account for resources used by a  <code>hostPath</code>.</li>
<li>the files or directories created on the underlying hosts are only writable by root. You either need to run your process as root in a  <a href="https://kubernetes.io/docs/user-guide/security-context" rel="nofollow noreferrer">privileged Container</a>  or modify the file permissions on the host to be able to write to a  <code>hostPath</code>  volume</li>
</ul>
<p>If you have any question let me know in the comments.</p>
