docker 容器防火墙设置

docker 容器防火墙设置

启动容器时增加参数

  • 方法一:完全开放
--privileged=true 

但是这样的话就将系统的所有能力都开放给了docker容器
有一个image为aaa的将启动为容器名为bbb的且在容器内需要使用iptables功能,可以使用–privileged=true来进行开启,如:

docker run --privileged=true -d -p 4489:4489/tcp  --name bbb aaa
  • 方法二:部分开放
    对于iptables需要的权限进行开放,其它的权限不予开放
--cap-add NET_ADMIN --cap-add NET_RAW

操作实例(桥接模式配置容器防火墙):

#生成容器
docker run -itd --net=none --name=centos06 --cpuset-cpus="0-3"  -m 1024M --memory-reservation 1024M  --cap-add NET_ADMIN --cap-add NET_RAW ssh-java1.8-tomcat8-centos6 /bin/bash
#配置网络IP
pipework br0 centos06 132.97.8.6/24@132.97.8.1
#进入容器
docker attach centos06

#设置容器的防火墙
[root@dee8225a9bba /]# iptables -A INPUT -s 132.97.0.0/16 -p tcp -m tcp --dport 21 -j ACCEPT
[root@dee8225a9bba /]# iptables -A INPUT -s 132.97.0.0/16 -p tcp -m tcp --dport 22 -j ACCEPT
[root@dee8225a9bba /]# iptables -A INPUT -s 132.97.0.0/16 -p tcp -m tcp --dport 8080 -j ACCEPT
[root@dee8225a9bba /]# iptables -A INPUT  -j DROP
[root@dee8225a9bba /]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@dee8225a9bba /]# service iptablse restart
iptablse: unrecognized service
[root@dee8225a9bba /]# service iptables restart
iptables: Setting chains to policy ACCEPT: mangle nat filte[  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:  iptable_mangle iptable_nat ip[FAILED]lter iptable_mangle iptable_nat iptable_filter ip_tables
iptables: Applying firewall rules:                         [  OK  ]
[root@dee8225a9bba /]# 

猜你喜欢

转载自blog.csdn.net/u011704394/article/details/54599727