title: what does VOLUME inside Dockerfile do
body: <p>I am trying to understand the below DockerFile:</p>
  
  <p><a href="https://github.com/nfnty/dockerfiles/blob/master/images/arch-bootstrap/latest/Dockerfile" rel="noreferrer">https://github.com/nfnty/dockerfiles/blob/master/images/arch-bootstrap/latest/Dockerfile</a></p>
  
  <pre><code>FROM nfnty/arch-mini:latest
  .....
  RUN     install --directory --owner=root --group=root --mode=700 /var/lib/bootstrap/{,archive}
  
  USER root
  VOLUME ["/var/lib/bootstrap"]
  ENTRYPOINT ["/opt/bootstrap/build.sh"]
  </code></pre>
  
  <p>RUN is creating directory /var/lib/bootstrap/archive and after build the image will be having this folder permanenantly</p>
  
  <p>When the container is created from it, it will have the folder "/var/lib/bootstrap/archive" because its existing in the image.</p>
  
  <p>What is the point of declaring VOLUME /var/lib/bootstrap/</p>
  
  <p>I can understand in command line -v [host path]:[container:path] will mount the host folder on the containers folder.</p>
  
  <p>but what is Volumne in dockerfile especially in the above case do.</p>
  
  <p>Ok I am showing some test i have done:</p>
  
  <pre><code>-- trying to create a container with dockerfile above
  i.e VOLUME ["/var/lib/bootstrap"]
  
  hostsystem#  docker run -it --entrypoint=/bin/bash nfnty/arch-bootstrap
  [root@684120b46cfb /]# ls -al /var/lib/bootstrap/
  total 12
  drwx------ 3 root root 4096 Oct 18 05:53 .
  drwxr-xr-x 1 root root 4096 Aug 23 12:48 ..
  drwx------ 2 root root 4096 Aug 23 12:48 archive
  
  -- I have created a sample001.txt file inside it.
  [root@684120b46cfb /]# touch /var/lib/bootstrap/sample001.txt
  [root@684120b46cfb /]# ls -al /var/lib/bootstrap/
  total 12
  drwx------ 3 root root 4096 Oct 18 05:54 .
  drwxr-xr-x 1 root root 4096 Aug 23 12:48 ..
  drwx------ 2 root root 4096 Aug 23 12:48 archive
  -rw-r--r-- 1 root root    0 Oct 18 05:54 sample001.txt
  [root@684120b46cfb /]# 
  
  [root@684120b46cfb /]# exit
  
  -- As per [@izazkhan answer][1] the VOLUME ["/var/lib/bootstrap"]
  instruction is persisting the data by creating a volume in 
  /var/lib/docker on the host and mount it on /var/lib/bootstrap 
  in the container. So expect the sample001.txt lies there at 
  /var/lib/docker/(var/lib/bootstrap)
  
  -- Now again trying to create a container
  hostsystem#  docker run -it --entrypoint=/bin/bash nfnty/arch-bootstrap
  [root@5fa7c4fc72e2 /]# ls -al /var/lib/bootstrap/
  total 12
  drwx------ 3 root root 4096 Oct 18 06:00 .
  drwxr-xr-x 1 root root 4096 Aug 23 12:48 ..
  drwx------ 2 root root 4096 Aug 23 12:48 archive
  [root@5fa7c4fc72e2 /]# 
  
  -- i dont see my sample001.txt file here.
  
  And i check the dockers:
  
  #  docker container ls -a
  CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                      PORTS               NAMES
  ff2d37e5399a        nfnty/arch-bootstrap   "/bin/bash"         16 seconds ago      Exited (0) 3 seconds ago                        stupefied_sinoussi
  bfbff0778fe9        nfnty/arch-bootstrap   "/bin/bash"         7 minutes ago       Exited (0) 30 seconds ago                       objective_noether
  
  
  And i check the volumes:
  
  #  docker volume ls
  DRIVER              VOLUME NAME
  local               47ae26f1b4b17cd2792972b50dcae9da9af1d3f06ccd984cfbf5a75be7365bbd
  local               fd5a1caf07024f7103a3a225f4de00a2c1efb79a74fa939737f11c939837b32a
  
  What i found is there are two volumes since i have created two containers.
  
  -- Also checking the volume folders:
  
  # cd /var/lib/docker/volumes
  
  #  find . -exec ls -dl \{\} \; | awk '{print $3, $4, $9}'
  root root .
  root root ./47ae26f1b4b17cd2792972b50dcae9da9af1d3f06ccd984cfbf5a75be7365bbd
  root root ./47ae26f1b4b17cd2792972b50dcae9da9af1d3f06ccd984cfbf5a75be7365bbd/_data
  root root ./47ae26f1b4b17cd2792972b50dcae9da9af1d3f06ccd984cfbf5a75be7365bbd/_data/archive
  root root ./fd5a1caf07024f7103a3a225f4de00a2c1efb79a74fa939737f11c939837b32a
  root root ./fd5a1caf07024f7103a3a225f4de00a2c1efb79a74fa939737f11c939837b32a/_data
  root root ./fd5a1caf07024f7103a3a225f4de00a2c1efb79a74fa939737f11c939837b32a/_data/archive
  root root ./fd5a1caf07024f7103a3a225f4de00a2c1efb79a74fa939737f11c939837b32a/_data/sample001.txt
  root root ./metadata.db
  </code></pre>
  
  <p>I was expecting sample001.txt to be see in any container. i.e All containers are using the same volume folder. but it looks like they are having different folders created at /var/lib/docker/volumes eventhought the mount point is as defined by the VOLUME in dockerfile. </p>
  
  <p>I was confused that VOLUME in dockerfile is refering to one single folder on the host machine at /var/lib/docker/volumes irrespective of how many contianers we create. But its not true since they have different host folders at /var/lib/docker/volumes for each container.</p>
  
  <p>Then what is the purpose of VOLUMES. One help i feel is if i create some files in the container and store them at the VOLUME place and i want to access them from host then i can go and check the volume folders.</p>
  
  <p>But the names of volume folders are not easy to figure out which container they belong to.</p>
  
  <p>Sorry i am completely new to volumes in docker, i new -v [host]:[container], but first time came across VOLUME in dockerfile. So i was completely confused and unable to figure out whats happening.</p>
  
  <p>After reading <a href="https://docs.docker.com/storage/volumes/" rel="noreferrer">https://docs.docker.com/storage/volumes/</a> i found the answer for why VOLUME</p>
  
  <blockquote>
    <p>In addition, volumes are often a better choice than persisting data in
    a container’s writable layer, because a volume does not increase the
    size of the containers using it, and the volume’s contents exist
    outside the lifecycle of a given container.</p>
  </blockquote>
  
  <p>Also the below link helps to know how to have a common volume and use it in different container (not the same as my question)</p>
  
  <p><a href="https://linuxhint.com/storing-sharing-docker-volumes/" rel="noreferrer">https://linuxhint.com/storing-sharing-docker-volumes/</a></p>