俗话说得好,好记性不如烂笔头!
- 日常工作中,我们都会在服务器在部署
docker
服务,在容器中部署我们的产品或者测试环境,而docker
服务又会使用到iptables
工具,因此在启动容器时,也会同时启动容器内的iptables
。但iptables
必须工作在容器的privileged
模式下,否则就会如上报错。
root@9db2fe517de6:/# sudo ufw enable
ERROR: problem running ufw-init
/lib/ufw/ufw-init: 118: /lib/ufw/ufw-init: modprobe: not found
/lib/ufw/ufw-init: 118: /lib/ufw/ufw-init: modprobe: not found
/lib/ufw/ufw-init: 118: /lib/ufw/ufw-init: modprobe: not found
iptables-restore v1.6.1: iptables-restore: unable to initialize table 'filter'
......
- 我们在容易中运行
iptables
命令,来查看容器内部的防火墙规则,发现也是会提示无法初始化filter
表,需要使用root
用户来执行。但是,我们会发现表象上其实我们就是以root
用户(看到了#
提示符)执行的呀? - 这是因为,容器中的
root
其实就类似于MacOS
上面的假Docker
是一个道理。我们只不是将服务器外面的用户映射到了容器内部而已,并不是真正意义上的root
用户。
root@9db2fe517de6:/# iptables -L -n
iptables v1.6.1: can't initialize iptables table `filter': Permission denied (you must be root)
Perhaps iptables or your kernel needs to be upgraded.
- 处理上述问题,就是需要在容器启动的时候添加
--privileged
参数,即可。
docker run -it --rm --privileged --entrypoint=/bin/bash ubuntu:latest
$ sudo docker inspect 9db2fe517de6
{
...
"instances": 1,
"container": {
"type": "DOCKER",
"volumes": [],
"docker": {
"image": "imageName",
"network": "BRIDGE",
"privileged": true,
"parameters": [],
}
}
...
}