docker privileged

本文详细介绍了Docker的privileged参数,解释了如何通过此参数使容器内的root用户获得完全的root权限,包括能够看到主机上的许多设备、执行挂载操作,甚至在Docker容器中启动另一个Docker容器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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@localhost ~]# 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@localhost ~]# 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
--------------------- 
作者:寻觅神迹 
来源:优快云 
原文:https://blog.youkuaiyun.com/halcyonbaby/article/details/43499409 
版权声明:本文为博主原创文章,转载请附上博文链接!

### 配置 `runners.docker.privileged` 的使用方法 在 GitLab CI/CD 中,`runners.docker.privileged` 是一个用于控制 Docker 容器运行权限的关键参数。当启用此选项时,Docker 运行器将以特权模式启动容器,从而允许容器内的进程访问更广泛的资源和功能。 #### 什么是 Privileged Mode? Privileged mode 提供了一种机制,使容器能够绕过大多数隔离特性,获得几乎与宿主机相同的权限级别。这通常适用于需要执行特定操作的任务,例如: - 使用嵌套虚拟化技术。 - 调试或分析底层硬件设备。 - 启动其他 Docker 容器。 然而需要注意的是,启用 privileged mode 可能带来安全风险,因为它削弱了容器之间的隔离屏障[^4]。 #### 如何配置 `runners.docker.privileged` 要启用 `runners.docker.privileged` 参数,在 `.gitlab-ci.yml` 文件中可以按照如下方式定义 job 或者全局默认行为: ```yaml variables: DOCKER_DRIVER: overlay2 services: - docker:dind stages: - build build_job: stage: build image: docker:latest script: - echo "Running with privileged mode" - docker info tags: - docker variables: GIT_STRATEGY: none services: - name: docker:dind entrypoint: ["dockerd", "--tls=false"] before_script: - docker version after_script: - echo "Job completed." runner_config: privileged: true ``` 在此示例中,通过设置 `privileged: true` 来激活该 Job 下所有由 Docker-in-Docker (dind) 创建的子容器都处于特权状态[^5]。 如果希望在整个 Runner 层面应用这一设定,则需修改 `/home/gitlab-runner/.gitlab-runner/config.toml` 文件中的对应条目: ```toml [[runners]] ... [runners.docker] privileged = true ``` 完成更改之后重启 GitLab Runner 服务即可生效。 对于 Kubernetes 类型的 Runners, 则应在 Helm Chart 的 values 文件或者直接编辑 ConfigMap 中加入类似的声明来实现相同效果: ```yaml runners: config: | [[runners]] executor = "kubernetes" [runners.kubernetes] privileged = true ``` 最后记得同步更新到实际使用的模板文件里去[^6]。 #### 注意事项 尽管使用 privileged 模式非常方便,但也伴随着潜在的安全隐患。因此建议仅限于确实必要的场景下才开启此项开关,并且定期审查相关作业逻辑以减少不必要的暴露面积。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值