实验环境:

docker version (18.06.3-ce)
centos7

1.安装 iptables 管理命令

yum -y install iptables-services

2.启动iptables

systemctl start iptables

3.禁止所有的IP访问docker的3306端口

[root@192]# iptables -I DOCKER-USER -i ens160 -p tcp --dport 3306 -j DROP

4.允许192.168.100.100 地址访问docker 3306端口

[root@192]# iptables -I DOCKER-USER -i ens160  -s 192.168.100.100 -p tcp --dport 3306 -j ACCEPT

5.查看DOCKER-USER策略

[root@192]# iptables --line -nvL  DOCKER-USER
Chain DOCKER-USER (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     tcp  --  ens160 *       192.168.100.100      0.0.0.0/0            tcp dpt:3306
2        0     0 DROP       tcp  --  ens160 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:3306

保存DOCKER-USER策略

service iptables save

telnet测试

[root@111 ~]# telnet 192.168.100.100 3306
Trying 192.168.100.100...
Connected to 172.17.118.226.
Escape character is '^]'.

删除DOCKER-USER策略

iptables -D DOCKER-USER 1			#删除DOCKER-USER链第一条规则