Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NFS Volumes with Docker Swarm

Create NFS Volumes:

Creating the NFS Volume:

$ docker volume create --driver local \
  --opt type=nfs \
  --opt o=addr=192.168.1.115,uid=1000,gid=1000,rw \
  --opt device=:/mnt/volumes/mysql-test \
  mysql-test-1

Creating The Service:

$ docker service create --name mysql \
  --network docknet \
  --mount "type=volume,source=mysql-test-1,destination=/var/lib/mysql,readonly=false" \
  --env MYSQL_ROOT_PASSWORD=pass \
  --replicas 1  hypriot/rpi-mysql

Another Method Container Only:

$ mkdir /opt/nfs/volumes/boo
$ docker volume create --opt type=nfs --opt device=:/opt/nfs/volumes/boo --opt o=addr=192.168.1.115  vol_boo
$ docker run -it -v vol_boo:/data rbekker87/armhf-alpine:3.5 sh
$ docker volume inspect vol_boo
[
    {
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/vol_boo/_data",
        "Name": "vol_boo",
        "Options": {
            "device": ":/opt/nfs/volumes/boo",
            "o": "addr=192.168.1.115",
            "type": "nfs"
        },
        "Scope": "local"
    }
]
$ docker run -it -v vol_boo:/data rbekker87/armhf-alpine:3.5 sh
#/ touch /data/test.txt
$ ls /opt/nfs/volumes/boo/
test.txt

Another Method with Compose/Stacks:

mkdir /opt/nfs/volumes/debug
version: "3.2"

services:
  a:
    image: rbekker87/armhf-alpine:3.5
    command: ping 127.0.0.1
    networks:
      - docknet
    volumes:
      - type: volume
        source: vol_debug
        target: /data
        volume:
          nocopy: true
    deploy:
      replicas: 1
      placement:
        constraints: [node.role == manager]

networks:
  docknet:
    external: true

volumes:
  vol_debug:
    driver: local
    driver_opts:
      type: "nfs"
      o: addr=192.168.1.115,nolock,soft,rw
      device: ":/opt/nfs/volumes/debug"

@smilelikeshit
Copy link

smilelikeshit commented Mar 13, 2020

thx

@vipera7
Copy link

vipera7 commented Aug 26, 2020

Using the first method, I'm getting

"failed to mount local volume: mount :/nfs/Myapp_1/custom:/var/lib/docker/volumes/Myapp_1_custom/_data, data: addr=192.168.1.115,uid=0,gid=0: invalid argument"

No worry I used my NFS server adress.

Have any idea why I'm getting the error?

@cybertschunk
Copy link

cybertschunk commented Sep 30, 2022

I

Using the first method, I'm getting

"failed to mount local volume: mount :/nfs/Myapp_1/custom:/var/lib/docker/volumes/Myapp_1_custom/_data, data: addr=192.168.1.115,uid=0,gid=0: invalid argument"

No worry I used my NFS server adress.

Have any idea why I'm getting the error?

I'm getting the same error :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment