俗话说得好,好记性不如烂笔头!

- 日常工作中,我们都会在服务器在部署
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": [],
}
}
...
}