创建volume

https://docs.docker.com/engine/reference/commandline/volume_create/

#创建了一个tmpfs数据卷,称为foo,大小为100M,uid为1000.
docker volume create –driver local –opt type=tmpfs –opt device=tmpfs –opt o=size=100m
docker volume create –driver local –opt type=btrfs –opt device=/dev/sda2 –name foo

#使用nfs从192.168.1.1挂载到/path/to/dir,模式为rw的示例:
docker volume create –driver local –opt type=nfs –opt o=addr=192.168.1.1,rw –opt device=:/NFSDir -name foo

#创建volume 
docker volume create --driver local --opt type=nfs \
    --opt o=addr=10.3.60.40,rw --opt device=:/NFSDir/db \
    nfsredis

#删除volume 
docker volume rm my-vol

使用volume

https://docs.docker.com/compose/compose-file/compose-file-v3/

#docker run
docker run -v foo:/data  -it  nginx:1.21.6-alpine /bin/sh

#docker service swarm
docker service create --name masl --mount type=volume,src=cwxvolume,dst=/home/mppay nginx:1.21.6

在创建时自动挂载

volumes:
  my-vol:
    driver_opts:
      type: "nfs"
      o: "addr=192.168.52.141,rw"
      device: ":/volume1/swarm"

docker-compose.yml

参考 https://docs.docker.com/compose/compose-file/compose-file-v3/

version: "3.8"
services:
  nginx:
    image: nginx:latest 
    volumes:
      - mydata:/mnt/data 挂载到的位置
      - dbdata:/mnt/data2 挂载方法三
    volumes:
      - type: volume  #挂载的方法一
        source: mydata
        target: /data
        volume:
          nocopy: true
      - type: bind   #挂载的方法二
        source: ./static
        target: /opt/app/static  
volumes:
  mydata:
  dbdata:
volumes:
  data_vol:
    driver: nfs
    driver_opts:
      share: 192.168.69.1:/data_vol
volumes:
  example:
    driver_opts:  #自动挂载
      type: "nfs"
      o: "addr=10.40.0.199,nolock,soft,rw"
      device: ":/docker/example"
volumes:
  data:
    external: true #使用外部
volumes:
  data:
    external: #使用外部
      name: actual-name-of-volume

netshare插件

#安装插件
wget https://github.com/ContainX/docker-volume-netshare/releases/download/v0.36/docker-volume-netshare_0.36_amd64.deb
dpkg -i docker-volume-netshare_0.36_amd64.deb
service docker-volume-netshare start