NFS volumes in Docker containers

 In aws, Docker

In many cases we find ourselves using a volume in the docker that is shared from another server via NFS. For this purpose, the docker includes its own NFS driver that will allow us to use this volume directly without having to run it on the host machine.

We have different options when using these NFS volumes in our containers:

  • Firstly, we can create a volume that we can use either in docker compose or using a docker run. This is the reason why we will use the following command: 
      $ docker volume create --driver local \
          --opt type=nfs \
          --opt o=nfsvers=4,addr=nfs.example-domain.com,rw \
          --opt device=:/path/to/dir \
          volume-name

    Once we have created the volume, we can mount it inside our container:

      $ docker run -it --rm \
        --mount type=volume,dst=/container/path,volume-driver=local,volume-opt=type=nfs,\"volume-opt=o=nfsvers=4,addr=nfs.example-domain.com\",volume-opt=device=:/host/path \
        volume-name
  • The same configuration can be applied within our docker-comper. Inside our docker-compose.yaml we will have to create the volume and mount it inside our container.
    # Container conf
      ...
      container-conf:
        container_name: foo/bar
        image: imagename:tag
        ports:
          - 80:80
        volumes:
          - new_volume:/path/inside/container
      ...
    # Create volume
      ...
      volumes:
        new_volume:
          driver: local
          driver_opts:
            type: nfs
            o: nfsvers=4,addr=nfs.example-domain.com,rw
            device: ":/host/path/to/dir"
      ...

    We have found ourselves in the same situation needing to fit the container with an EFS (Elastic File System) from Amazon Web Services. This AWS service provides scalable, elastic and concurrent storage and we will be able to add it using the same NFS docker driver.

    # Container conf
     ...
      container-conf:
        container_name: foo/bar
        image: imagename:tag
        ports:
          - 80:80
        volumes:
          - new_volume:/path/inside/container
     ... 
    # Create volume
     ...
      volumes:
        efs_volume:
          driver: local
          driver_opts:
            type: nfs
            o: addr=[EFS_DNS],nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2
            device: [EFS_DNS]:/
     ...

    Please note that if you have just created this EFS, it may be that when you try to create this volume you will get an error warning that you cannot find the EFS endpoint. This is due to the fact that these DNS will take a few minutes to propagate. If you cannot add it via the endpoint, you can do it directly from the EFS’s IP.

We hope that this information has been useful to you and don’t forget that at Geko cloud we are at your disposal for the management of cloud infrastructures and microservices.

If you have other questions related to docker or Kubernetes, perhaps you can find answers in other posts on our blog.

See you next time!

Recommended Posts

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.