docker下无root权限如何操作root权限目录

①可以看到admin用户的data目录下存在root权限的目录

[admin@localhost data]# ll

drwxr-xr-x. 3 root root 33 Jul  2 22:28 test

②无论是更改还是删除都会有错误提示

[admin@localhost data]$ chown -R nginx:nginx test/

chown: changing ownership of ‘test/pictures/1.jpg’: Operation not permitted

chown: changing ownership of ‘test/pictures/2.png’: Operation not permitted

chown: changing ownership of ‘test/pictures’: Operation not permitted

chown: changing ownership of ‘test/a.txt’: Operation not permitted

chown: changing ownership of ‘test/’: Operation not permitted

[admin@localhost data]$ rm -rf test/

rm: cannot remove ‘test/pictures/1.jpg’: Permission denied

rm: cannot remove ‘test/pictures/2.png’: Permission denied

rm: cannot remove ‘test/a.txt’: Permission denied

③使用卷映射进行更改

docker run -it --rm -u root -v /主机path/:/容器path/ 镜像id bash

       使用root权限将主机目录挂载到容器中的目录下,进入容器后可以以root权限操作该主机目录,可以实现主机目录的赋权、更改用户组、删除等操作

④操作过程(主机需要存在一个可以运行的镜像)

[admin@localhost data]# docker run -it --rm -u root -v /data/:/tmp/ nginx:latest bash

admin@f8ef771f2cfa:/# cd /tmp/

admin@f8ef771f2cfa:/tmp# ls -l

total 0

drwxr-xr-x. 3 root root 33 Jul  3 02:28 test

admin@f8ef771f2cfa:/tmp# chown -R admin:admin test/

你可能感兴趣的:(docker下无root权限如何操作root权限目录)