Create and use an NFS Docker volume

From EN Ikoula wiki
Revision as of 15:40, 23 January 2022 by Ccunha64415 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
⧼vector-jumptonavigation⧽ ⧼vector-jumptosearch⧽

en:Create and use an NFS Docker volume fr:Creer et utiliser un volume Docker NFS



This article describes how to create an NFS type 'volume Docker' ', ie create a volume that will present the data of an NFS export (eg an nfs share from a Synology NAS type) and then use this one from Docker containers.

We performed this procedure from a Cloud Ikoula One instance and use the NFS export of a Synology NAS but this procedure is fully operational on a dedicated server and with a standard NFS export.

We will assume that you already have access to a system with Docker installed and an existing NFS export.

Creating the NFS Docker Volume :

Here is the command to create an NFS type Docker volume in read / write access from an existing NFS export :

[root@ikoula ~]# docker volume create --driver local --opt type=nfs --opt o=addr=<adresse ip serveur nfs>,rw --opt device=:<chemin export nfs> <nom du volume NFS Docker>

Note: you can obviously name your Docker volume as you wish.

which gives in our case (our volume is created from an NFS export "/ volume1 / datas" of a NAS synology):

[root@CentOS7 ~]# docker volume create --driver local --opt type=nfs --opt o=addr=213.246.x.x,rw --opt device=:/volume1/datas synodatas 
synodatas

Our NFS volume is now in the list of Docker volumes:

[root@CentOS7 ~]# docker volume ls
DRIVER              VOLUME NAME
local               synodatas

We can also inspect your volume :

[root@CentOS7 ~]# docker volume inspect synodatas
[
    {
        "CreatedAt": "2018-11-26T11:00:27+01:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/synodatas/_data",
        "Name": "synodatas",
        "Options": {
            "device": ":/volume1/datas",
            "o": "addr=213.246.x.x,rw",
            "type": "nfs"
        },
        "Scope": "local"
    }
]

Using the NFS Docker Volume on Docker Containers :

You must first have the NFS client installed on the host.

In our case, the host (a CIO instance) is in "Centos7", just install the package "nfs-utils" :

[root@CentOS7 ~]# yum install nfs-utils

We can now use our previously created NFS Docker volume on one or more containers via the following command :

[root@CentOS7 ~]# docker run -d  -it --name <Nom container> --mount source=<Nom du volume NFS>,target=<point de montage du contenu du volume NFS dans le container> <Nom image docker>

Which gives in our case (we create a container named "deb9" from the debian image in which we mount the contents of our NFS volume "synodatas" on the mount point / mnt) :

[root@CentOS7 ~]# docker run -d  -it --name deb9 --mount source=synodatas,target=/mnt debian
ce6328ab1a44c8c7a07e2fd9c4022d33d394828376d3cb10171bbdeec745371a

The "mount" section of our container ("docker inspect deb9)::

        "Mounts": [
            {
                "Type": "volume",
                "Name": "synodatas",
                "Source": "/var/lib/docker/volumes/synodatas/_data",
                "Destination": "/mnt",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
        ],

If we connect to our container deb9, we can list the files of our export / volume NFS docker from the mount point :

[root@CentOS7 ~]# docker exec -it deb9 /bin/bash
root@ce6328ab1a44:/# ls -1 /mnt/
file_test1
file_test2
file_test3
file_test4
file_test5

Since we have created our read / write Docker volume, we can also create or modify files on it from our container:

root@ce6328ab1a44:/# touch /mnt/testfromdocker
root@ce6328ab1a44:/#

You will understand this procedure allows you to use persistent storage within one or more Docker containers (simultaneously or not) via an NFS type Docker volume, for example using an NFS export from your NAS..


Pour aller plus loin :

https://docs.docker.com/storage/volumes/ Catégorie:FLEX Catégorie:Linux