<p>I am using kubernetes to deploy a postgresql pod.</p>
<p>My k8s manifest for that works nicely. I only paste the relevant code below:</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgresql
spec:
  ...
  template:
  ...
    spec:
      containers:
      - name: postgresql
        image: postgres:14
      ...
      volumeMounts:
        - name: my-postgres
          mountPath: /var/lib/postgresql/data
        ...

  volumeClaimTemplates:
  - metadata:
      name: my-postgres
    spec:
  
</code></pre>
<p>As you can see above, under container spec, I declared the <code>volumeMounts</code> with a mount path. And a <code>volumeClaimTemplates</code> to provision the persistent in cloud.</p>
<p>Like said, it works fine.</p>
<p>when I open the Dockerfile of <a href="https://github.com/docker-library/postgres/blob/4e56664f1797ba4cc0f5917b6d794792a5571b45/14/bullseye/Dockerfile" rel="nofollow noreferrer">official postgresql here</a>. On line 186, there is <code>VOLUME /var/lib/postgresql/data</code>.</p>
<p>I would like to verify if I understand the whole thing correctly:</p>
<ol>
<li><p>Is it so that the line 186 <code>VOLUME ...</code> in postgres Dockerfile is only meant to tell its official mount point? I mean if I use the Dockerfile to run a container, it DOESN'T create a volume but I need to specific a volume &amp; mount to that mount point. Am I right here?</p>
</li>
<li><p>In my k8s manifest above, under <code>volumeMounts:</code> the <code>mountPath:</code> value has to be exactly the same as the line 186 <code>VOLUME ...</code> in postgres Dockerfile, because that is the mount point they told. Am I right here?</p>
</li>
</ol>

<ul>
<li>I created simple Dockerfile to examine &quot;VOLUME&quot; instruction in Dockerfile as below :</li>
</ul>
<pre><code>[root@machine stack]# cat Dockerfile
FROM busybox
VOLUME /insidecontiner
RUN echo &quot;writing From Container&quot; &gt;&gt; /insidecontiner/file
CMD sleep 1000

[root@machine stack]# docker build -t test .
Sending build context to Docker daemon   7.68kB
Step 1/4 : FROM busybox
 ---&gt; 69593048aa3a
Step 2/4 : VOLUME /insidecontiner
 ---&gt; Running in 71188c3529d8
Removing intermediate container 71188c3529d8
 ---&gt; 6a83730242a2
Step 3/4 : RUN echo &quot;writing From Container&quot; &gt;&gt; /insidecontiner/file
 ---&gt; Running in d73b82b60333
Removing intermediate container d73b82b60333
 ---&gt; c2cfb57a967d
Step 4/4 : CMD sleep 1000
 ---&gt; Running in aff9f1d4a0b3
Removing intermediate container aff9f1d4a0b3
 ---&gt; 834cc7a98e91
Successfully built 834cc7a98e91
</code></pre>
<ul>
<li>There were no docker volumes in the system before running this contaainer</li>
</ul>
<pre><code>[root@machine stack]# docker volume ls
DRIVER              VOLUME NAME

</code></pre>
<ul>
<li>Once i created a container using this image , It automatically created a docker volume</li>
</ul>
<pre><code>[root@machine ~]# docker run -d --name boo test
e5994bb0975635354f60a238c19db472700949946c15b6a916e0c2e4ffed33e3

[root@machine ~]# docker volume ls
DRIVER              VOLUME NAME
local               2d7ec8ea6f61cd4c8758d2da666b11b28970b9ff630053bd4109a13f4516c504

[root@machine ~]# docker volume inspect 2d7ec8ea6f61cd4c8758d2da666b11b28970b9ff630053bd4109a13f4516c504
[
    {
        &quot;CreatedAt&quot;: &quot;2022-06-11T13:03:05-04:00&quot;,
        &quot;Driver&quot;: &quot;local&quot;,
        &quot;Labels&quot;: null,
        &quot;Mountpoint&quot;: &quot;/var/lib/docker/volumes/2d7ec8ea6f61cd4c8758d2da666b11b28970b9ff630053bd4109a13f4516c504/_data&quot;,
        &quot;Name&quot;: &quot;2d7ec8ea6f61cd4c8758d2da666b11b28970b9ff630053bd4109a13f4516c504&quot;,
        &quot;Options&quot;: null,
        &quot;Scope&quot;: &quot;local&quot;
    }
]
</code></pre>
<ul>
<li>That volume is mounted inside the container at <code>/insidecontiner </code> :</li>
</ul>
<pre><code>[root@machine ~]# docker exec -it boo /bin/sh
/ # df -h
Filesystem                Size      Used Available Use% Mounted on
overlay                  50.0G     18.7G     31.3G  37% /
tmpfs                    64.0M         0     64.0M   0% /dev
tmpfs                    62.8G         0     62.8G   0% /sys/fs/cgroup
shm                      64.0M         0     64.0M   0% /dev/shm
/dev/mapper/rhel-root

                         50.0G     18.7G     31.3G  37% /insidecontiner
</code></pre>
<blockquote>
<blockquote>
<blockquote>
<p>Coming to your question &quot;Is it so that the line 186 VOLUME ... in postgres Dockerfile is only meant to tell its official mount point? I mean if I use the Dockerfile to run a container, it DOESN'T create a volume but I need to specific a volume &amp; mount to that mount point. Am I right here?&quot;</p>
</blockquote>
</blockquote>
</blockquote>
<hr />
<ul>
<li><code>/var/lib/postgresql/data</code> is the place where postgres keeps in datbases</li>
<li>It does create a volume automatically</li>
<li>It mounts inside the container to the location mention in the Dockerfile</li>
</ul>
<hr />
<p><strong>Now to Kubernetes behavior</strong></p>
<ul>
<li>Creating a pod without mentioning any volumes in pod yaml run the pod successfully.ContainerRuntime on the worker node(docker in this example) will a local docker volume for that container inside the pod</li>
</ul>
<pre><code>[root@machine stack]# kubectl run boo --image prasasai/test
pod/boo created

[root@machine stack]# kubectl get pods -o wide
NAME   READY   STATUS    RESTARTS   AGE     IP               NODE                                     NOMINATED NODE   READINESS GATES
boo    1/1     Running   0          3m51s   192.168.187.80   machine1  &lt;none&gt;           &lt;none&gt;

[root@machine1 ~]# docker volume inspect 4dbf6bc943a55c89a45d01c4aa3e860141bb7efe01ce046959c5a208dfab7e3e
[
    {
        &quot;CreatedAt&quot;: &quot;2022-06-11T17:26:11-04:00&quot;,
        &quot;Driver&quot;: &quot;local&quot;,
        &quot;Labels&quot;: null,
        &quot;Mountpoint&quot;: &quot;/var/lib/docker/volumes/4dbf6bc943a55c89a45d01c4aa3e860141bb7efe01ce046959c5a208dfab7e3e/_data&quot;,
        &quot;Name&quot;: &quot;4dbf6bc943a55c89a45d01c4aa3e860141bb7efe01ce046959c5a208dfab7e3e&quot;,
        &quot;Options&quot;: null,
        &quot;Scope&quot;: &quot;local&quot;
    }
]

</code></pre>
<ul>
<li>If i mention <code>volumeMounts</code> section matching the <code>VOLUME</code> in Dockerfile then kubernetes is taking care of the volume mapping and ContainerRuntime is not creating the volume :</li>
</ul>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: boo
  name: boo
spec:
  containers:
  - image: prasasai/test
    name: boo
    volumeMounts:
    - name: mount1
      mountPath: /insidecontiner
  volumes: 
  - name: mount1 
    hostPath:
      path: /here
</code></pre>
<pre><code>[root@machine stack]# kubectl get pods -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP               NODE                                     NOMINATED NODE   READINESS GATES
boo    1/1     Running   0          13s   192.168.187.85   machine1   &lt;none&gt;           &lt;none&gt;

[root@machine1 ~]# docker volume ls
DRIVER    VOLUME NAME
</code></pre>
<blockquote>
<blockquote>
<blockquote>
<p>&quot; In my k8s manifest above, under volumeMounts: the mountPath: value has to be exactly the same as the line 186 VOLUME ... in postgres Dockerfile, because that is the mount point they told. Am I right here?&quot;</p>
</blockquote>
</blockquote>
</blockquote>
<hr />
<ul>
<li>Yes , Thats absolutely correct.</li>
</ul>
<hr />
