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

时间:2017-10-04 17:01:22

标签: ubuntu docker nfs

我在Windows主机上运行了一个Ubuntu容器。我安装了所有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脚本运行完全相同的命令我得到&#34 ;不存在点......" BTW the existst不是它在控制台中显示的错字。

编辑: 接受的答案完美无缺,但我只想在此添加如果路径已经存在于容器中,那么它就不会安装。还想添加更改yml文件,如果在docker-compose文件中定义mount将不会重新创建它,如果它不好,你必须先调整卷。

1 个答案:

答案 0 :(得分:1)

您可以使用docker deamon安装nfs vumes。这样你就不需要在你的容器内使用nfs supprt了。我无法看到为什么要在容器内安装

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