Docker volume vs Kubernetes persistent volume

10/30/2018

The closest answer I found is this.

But I want to know is that, will the Dockerfile VOLUME command be totally ignored by Kubernetes? Or data will be persisted into two places? One for docker volume (in the host which pod running) and another is Kubernetes's PV?

The reason of asking this is because I deploy some containers from docker hub which contain VOLUME command. Meanwhile I also attach PVC to my pod. I am thinking whether local volume (docker volume, not K8 PV) will be created in the node? If my pod scheduled to another node, then another new volume created?


On top of this, thanks for @Rico to point out that -v command and Kubernetes's mount will take precedence over dockerfile VOLUME command, but what if as scenario below:

  • dockerfile VOLUME onto '/myvol'

  • Kubernetes mount PVC to '/anotherMyVol'

In this case, will myvol mount to my local node harddisk? and cause unaware data persisted locally?

-- GMsoF
docker
kubernetes

2 Answers

10/30/2018

You need to explicitly define a PersistentVolumeClaim and/or PersistentVolume. This is not done for you.

-- yomateo
Source: StackOverflow

10/30/2018

It will not be ignored unless you override it on your Kubernetes pod spec. For example, if you follow this example from the Docker documentation:

$ docker run -it container bash
root@7efcf5ef12a2:/# mount | grep myvol
/dev/nvmeXnXpX on /myvol type ext4 (rw,relatime,discard,data=ordered)
root@7efcf5ef12a2:/#

You'll see that it's mounted on the root drive of the host where the container is running on. Docker actually creates a volume on the host filesystem under /var/lib/docker/volumes (/var/lib/docker is your Docker graph directory):

$ pwd
/var/lib/docker/volumes
$ find . | grep greeting
./d0bc20d085243c39c4f386dce2f6cafcd8146128d6b0c8f9dcb27cfb61a7ecab/_data/greeting

You can override this with the -v option in Docker:

$ docker run -it -v /mnt:/myvol container bash
root@1c7211cf43d0:/# cd /myvol/
root@1c7211cf43d0:/myvol# touch hello
root@1c7211cf43d0:/myvol# exit
exit
$ pwd # <= on the host
/mnt
$ ls
hello

So on Kubernetes you can override it in the pod spec:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mycontainer
    image: container
    volumeMounts:
    - name: storage
      mountPath: /myvol
  volumes:
    - name: storage
      hostPath:
        path: /mnt
        type: Directory
-- Rico
Source: StackOverflow