如何避免权限拒绝与docker之后装入卷UID / GID 1000?

我正在使用Fedora版本1.12.5的Docker。

我尝试使用以下命令创build一个Mongodb容器:

sudo docker run -v $PWD/db:/data/db -p 27017:27017 --name db -it username/mongo /bin/bash 

这是我启动mongodb后的回应:

 # mongod root@32c9349a75b8:/# mongod 2016-12-26T09:59:08.643+0000 I CONTROL [initandlisten] MongoDB starting : pid=10 port=27017 dbpath=/data/db 64-bit host=32c9349a75b8 2016-12-26T09:59:08.644+0000 I CONTROL [initandlisten] db version v3.4.1 2016-12-26T09:59:08.644+0000 I CONTROL [initandlisten] git version: 5e103c4f5583e2566a45d740225dc250baacfbd7 2016-12-26T09:59:08.644+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016 2016-12-26T09:59:08.644+0000 I CONTROL [initandlisten] allocator: tcmalloc 2016-12-26T09:59:08.644+0000 I CONTROL [initandlisten] modules: none 2016-12-26T09:59:08.644+0000 I CONTROL [initandlisten] build environment: 2016-12-26T09:59:08.644+0000 I CONTROL [initandlisten] distmod: ubuntu1604 2016-12-26T09:59:08.644+0000 I CONTROL [initandlisten] distarch: x86_64 2016-12-26T09:59:08.644+0000 I CONTROL [initandlisten] target_arch: x86_64 2016-12-26T09:59:08.644+0000 I CONTROL [initandlisten] options: {} 2016-12-26T09:59:08.646+0000 I STORAGE [initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating 2016-12-26T09:59:08.646+0000 I NETWORK [initandlisten] shutdown: going to close listening sockets... 2016-12-26T09:59:08.646+0000 I NETWORK [initandlisten] shutdown: going to flush diaglog... 2016-12-26T09:59:08.646+0000 I CONTROL [initandlisten] now exiting 2016-12-26T09:59:08.646+0000 I CONTROL [initandlisten] shutting down with code:100 

我检查了/ data / db的文件权限:

 root@32c9349a75b8:/data# ls -al total 4 drwxr-xr-x. 3 root root 16 Dec 26 03:15 . drwxr-xr-x. 23 root root 266 Dec 26 09:59 .. drwxrwxr-x. 2 1000 1000 4096 Dec 25 16:24 db 

如果我只是使用mkdir来创build目录而不用一个卷来装载,那么所有者和/ db组将会是root,我不会为什么uid和gid只有在使用docker运行命令。 我该如何解决?

卷挂载的权限是您执行主机卷(到主机上的目录)的目录web的权限。 修复主机本身的权限解决了这个问题。

最好是,您可以使用命名卷而不是主机目录。 Docker将保持卷和初始内容,并且包含的​​权限将从这种types的卷的映像中复制。