Docker 允许您使用本地卷驱动程序将文件系统挂载到容器中,以通过 mount 系统调用访问任何您可以访问的内容。系统调用几乎与 Linux 中的 mount 命令相同。但是,当您执行 NFS 挂载之类的操作时,您会看到 mount 命令如何将该命令预处理到系统调用中。
以下是一些在 docker 中执行 NFS 卷挂载的不同方法的示例:
# create a reusable volume
$ docker volume create --driver local \
--opt type=nfs \
--opt o=nfsvers=4,addr=nfs.example.com,rw \
--opt device=:/path/to/dir \
foo
# or from the docker run command
$ 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.com\",volume-opt=device=:/host/path \
foo
# or to create a service
$ docker service create \
--mount type=volume,dst=/container/path,volume-driver=local,volume-opt=type=nfs,\"volume-opt=o=nfsvers=4,addr=nfs.example.com\",volume-opt=device=:/host/path \
foo
# inside a docker-compose file
...
volumes:
nfs-data:
driver: local
driver_opts:
type: nfs
o: nfsvers=4,addr=nfs.example.com,rw
device: ":/path/to/dir"
...
有关所有选项和潜在陷阱的更多详细信息,请参阅this answer 的类似问题。