使用 shell 脚本在 docker 容器内挂载 NFS 共享

Mounting NFS shares inside docker container using shell script

我在 windows 主机上有一个 Ubuntu 容器 运行ning。我已经安装了所有的 nfs 客户端包。我可以连接到我的容器并挂载一个 nfs 共享。

我的想法是启动容器 运行 我的脚本安装了两个共享然后 运行 gunicorn。

docker-compose.yml
version: '2.1'

services:
  web:
  #  restart: always
    build: .
    expose:
      - "5000"
      - "80"
    ports:
      - "5000:80"

    #command: gunicorn -w 4 wsgi_mount:app -b 0.0.0.0:80 --timeout 500
    privileged: true
    command: /bin/bash /app/entrypoint.sh

entrypoint.sh
------------
mount --verbose -t nfs -o nolock -o nfsvers=3 server:/nfs/share /app/path_to_mount1
mount --verbose -t nfs -o nolock -o nfsvers=3 server:/nfs/share /app/path_to_mount2
gunicorn -w 4 wsgi_mount:app -b 0.0.0.0:80 --timeout 500

输出

does not existt point /app/path_to_mount1
 does not existt point /app/path_to_mount2
[2017-10-04 16:29:57 +0000] [41] [INFO] Starting gunicorn 19.7.1
[2017-10-04 16:29:57 +0000] [41] [INFO] Listening at: http://0.0.0.0:80 (41)
[2017-10-04 16:29:57 +0000] [41] [INFO] Using worker: sync
[2017-10-04 16:29:57 +0000] [46] [INFO] Booting worker with pid: 46
[2017-10-04 16:29:57 +0000] [49] [INFO] Booting worker with pid: 49
[2017-10-04 16:29:57 +0000] [50] [INFO] Booting worker with pid: 50
[2017-10-04 16:29:57 +0000] [53] [INFO] Booting worker with pid: 53

我发现真正令人沮丧的是,如果我只是拥有它 运行 gunicorn,然后附加到容器并从容器中的脚本中复制过去的行,用于它工作的每个挂载点。

如果我 运行 容器内的脚本我会得到完全相同的错误

我知道权限是正确的,我现在 NFS 设置正确,我可以手动让它在我的容器中工作,但如果尝试从容器内的 bash 脚本执行它 运行 与我得到的命令完全相同 "does not existt point ..." 顺便说一句,existt 不是拼写错误,它在控制台中以这种方式显示。

编辑: 接受的答案非常有效,但我只想在这里补充一点,如果路径已经存在于容器中,那么它将不会挂载。还想补充一点,如果您在 docker-compose 文件中定义挂载,则更改 yml 文件将不会重新创建它,如果它不好,您必须先 rm 卷。

您可以使用 docker 守护进程挂载 nfs vumes。这样你就不需要容器内的 nfs 支持。我看不出为什么要安装在容器内的原因

$ docker volume create --driver local \ --opt type=nfs \ --opt o=addr=192.168.1.1,rw \ --opt device=:/path/to/dir \富