Search code examples
dockervolumes

Docker Volume without linking. What is the use case?


So far, I really only understand VOLUME as a way to

  • Specify a directory inside of a data container that will be persistent
  • Specify a location that will link to your host container

What I am failing to understand is why I see so many Dockerfiles that use VOLUME /path/to/app or even worse VOLUME /var/lib/mysql. I understand that you might want to create a container that has this volume and then use --volumes-from to link to that container for persistence. But why would you make that specification inside the container that is actually using that data. How does it help? As far as I can see, VOLUME /var/data is not any different than just saying RUN mkdir /var/data. How are volumes beneficial when they are not inside a data container, shared with the host, or being used by other containers?


Solution

  • Docker images and Docker containers have a layered file system which is slow. By defining directories as data volumes you instruct docker to let those directories outside of the slow layered file system. This as multiple consequences among which:

    • fast file system
    • ability to share a volume between multiple containers
    • persistence (as long as at least one container that use that volume exists)

    This is why volumes are not only a commodity but a necessity for directories for which good I/O performances is expected.


    As far as I can see, VOLUME /var/data is not any different than just saying RUN mkdir /var/data.

    The difference is that with volumes the directory /var/data is a mount point on a different (and faster) file system. You can witness that /var/data is not just another directory by running the mount command in a container:

    $ docker run --rm -v /var/data busybox mount
    rootfs on / type rootfs (rw)
    none on / type aufs (rw,relatime,si=6c354c296f850c3c)
    proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
    tmpfs on /dev type tmpfs (rw,nosuid,mode=755)
    shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)
    devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)
    sysfs on /sys type sysfs (ro,nosuid,nodev,noexec,relatime)
    /dev/mapper/vg0-root on /etc/resolv.conf type ext4 (rw,relatime,errors=remount-ro,data=ordered)
    /dev/mapper/vg0-root on /etc/hostname type ext4 (rw,relatime,errors=remount-ro,data=ordered)
    /dev/mapper/vg0-root on /etc/hosts type ext4 (rw,relatime,errors=remount-ro,data=ordered)
    /dev/mapper/vg0-root on /var/data type ext4 (rw,relatime,errors=remount-ro,data=ordered)
    proc on /proc/sys type proc (ro,nosuid,nodev,noexec,relatime)
    proc on /proc/sysrq-trigger type proc (ro,nosuid,nodev,noexec,relatime)
    proc on /proc/irq type proc (ro,nosuid,nodev,noexec,relatime)
    proc on /proc/bus type proc (ro,nosuid,nodev,noexec,relatime)
    tmpfs on /proc/kcore type tmpfs (rw,nosuid,mode=755)
    

    / is on a aufs layered (and slow) file system

    /var/data is on a ext4 (and fast) file system