Docker privileged参数:容器权限扩展

发布时间: 更新时间: 总字数:543 阅读时间:2m 作者: IP上海 分享 网址

指定 docker run --privileged 参数可以为容器扩展权限,使 root 拥有真正的 root 权限

privileged 参数

$ docker help run
...
--privileged=false         Give extended privileges to this container
...

大约在 0.6 版,privileged 被引入 docker。使用该参数,container 内的 root 拥有真正的 root 权限。否则,container 内的 root 只是外部的一个普通用户权限。privileged 启动的容器,可以看到很多 host 上的设备,并且可以执行 mount。甚至允许你在 docker 容器中启动 docker 容器。

未设置 privileged 启动的容器:

[root@xiexianbin_cn ~]# docker run -t -i centos:latest bash
[root@65acccbba42f /]# ls /dev
console  fd  full  fuse  kcore  null  ptmx  pts  random  shm  stderr  stdin  stdout  tty  urandom  zero
[root@65acccbba42f /]# mkdir /home/test/
[root@65acccbba42f /]# mkdir /home/test2/
[root@65acccbba42f /]# mount -o bind /home/test  /home/test2
mount: permission denied

设置 privileged 启动的容器:

[root@xiexianbin_cn ~]# docker run -t -i --privileged centos:latest bash
[root@c39330902b45 /]# ls /dev/
autofs           dm-1  hidraw0       loop1               null    ptp3    sg0  shm       tty10  tty19  tty27  tty35  tty43  tty51  tty6   ttyS1    usbmon3  vcs5   vfio
bsg              dm-2  hidraw1       loop2               nvram   pts     sg1  snapshot  tty11  tty2   tty28  tty36  tty44  tty52  tty60  ttyS2    usbmon4  vcs6   vga_arbiter
btrfs-control    dm-3  hpet          loop3               oldmem  random  sg2  snd       tty12  tty20  tty29  tty37  tty45  tty53  tty61  ttyS3    usbmon5  vcsa   vhost-net
bus              dm-4  input         mapper              port    raw     sg3  stderr    tty13  tty21  tty3   tty38  tty46  tty54  tty62  uhid     usbmon6  vcsa1  watchdog
console          dm-5  kcore         mcelog              ppp     rtc0    sg4  stdin     tty14  tty22  tty30  tty39  tty47  tty55  tty63  uinput   vcs      vcsa2  watchdog0
cpu              dm-6  kmsg          mem                 ptmx    sda     sg5  stdout    tty15  tty23  tty31  tty4   tty48  tty56  tty7   urandom  vcs1     vcsa3  zero
cpu_dma_latency  fd    kvm           net                 ptp0    sda1    sg6  tty       tty16  tty24  tty32  tty40  tty49  tty57  tty8   usbmon0  vcs2     vcsa4
crash            full  loop-control  network_latency     ptp1    sda2    sg7  tty0      tty17  tty25  tty33  tty41  tty5   tty58  tty9   usbmon1  vcs3     vcsa5
dm-0             fuse  loop0         network_throughput  ptp2    sda3    sg8  tty1      tty18  tty26  tty34  tty42  tty50  tty59  ttyS0  usbmon2  vcs4     vcsa6
[root@c39330902b45 /]# mkdir /home/test/
[root@c39330902b45 /]# mkdir /home/test2/
[root@c39330902b45 /]# mount -o bind /home/test  /home/test2

FAQ

问题 1

[root@44cb99fb9b7c /]# systemctl start nginx
Failed to get D-Bus connection: Operation not permitted

Docker的设计理念是在容器里不运行后台服务容器本身就是宿主机上的一个独立的主进程,也就是说容器里面只运行应用的主进程。

解决方式:以特权模式运行容器

docker run -itd --privileged --name c7 centos:7 /sbin/init
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数