Docker -v 对挂载的目录没有权限 Permission denied

问题:今天在使用docker安装minio 挂载本地磁盘的时候一直报错

docker run -p 9000:9000 -p 9090:9090 \
 --net=host \
 --name myminio \
 -d --restart=always \
 -e "MINIO_ACCESS_KEY=minioadmin" \
 -e "MINIO_SECRET_KEY=minioadmin@2022" \
 -v /data/minio/data:/data \
 -v /data/minio/config:/root/.minio \
 minio/minio server \
 /data --console-address ":9090" -address ":9000"

回车之后 容器启动失败,查看日志:

Error: unable to create (/data/.minio.sys/tmp) file access denied, drive may be faulty please investigate (*fmt.wrapError)
       6: internal/logger/logger.go:259:logger.LogIf()
       5: cmd/prepare-storage.go:95:cmd.bgFormatErasureCleanupTmp()
       4: cmd/xl-storage.go:246:cmd.newXLStorage()
       3: cmd/object-api-common.go:83:cmd.newStorageAPI()
       2: cmd/format-erasure.go:692:cmd.initStorageDisksWithErrors.func1()
       1: internal/sync/errgroup/errgroup.go:123:errgroup.(*Group).Go.func1()
Unable to use the drive /data: disk access denied
ERROR Unable to initialize backend: Unable to write to the backend
      > Please ensure MinIO binary has write permissions for the backend
      HINT:
        Run the following command to add write permissions: `sudo chown -R root. <path> && sudo chmod u+rxw <path>`

提示没有权限,但实际上挂载的目录已经设置过权限了,下面的命令都尝试过还是报错:

chmod +x /data/minio/data
chmod -R 777 /data/minio/data
sudo chown -R root. /data/minio/data && sudo chmod u+rxw /data/minio/data

最终解决方案:

参考文章:
Docker -v 对挂载的目录没有权限 Permission denied

报错原因:

centos7中安全模块selinux把权限禁掉了

解决方案(我用的第一种):

  1. 在运行时加 --privileged=true
docker run -p 9000:9000 -p 9090:9090 \
 --net=host \
 --name myminio \
 --privileged=true \
 -d --restart=always \
 -e "MINIO_ACCESS_KEY=minioadmin" \
 -e "MINIO_SECRET_KEY=minioadmin@2022" \
 -v /data/minio/data:/data \
 -v /data/minio/config:/root/.minio \
 minio/minio server \
 /data --console-address ":9090" -address ":9000"

启动成功!!测试上传文件也成功挂载到本地了

  1. 临时关闭selinux然后再打开
setenforce 0
setenforce 1