背景描述docker
在使用Docker時,啓用centos7默認的firewall,啓動端口映射時,防火牆規則不生效。docker默認使用了iptables防火牆機制。因此須要關閉firewall使用iptables解決。
1.關閉默認的firewall防火牆vim
systemctl stop firewalld.service 關閉防火牆 systemctl disable firewalld.service 關閉開機啓動
2.開啓iptablescentos
yum install iptables (根據centOS7的版本和內核,有些版本已經裝過,能夠跳過此命令) yum install iptables-services
service iptables restart
chkconfig iptables on或者systemctl enable iptables.service開機自啓
3.編輯防火牆文件(開啓了21,22,80,3306端口)tcp
vim /etc/sysconfig/iptables # sampleconfiguration for iptables service # # you can edit thismanually or use system-config-firewall # # please do not askus to add additional ports/services to this default configuration *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
4.添加防火牆命令this
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
5.查看本機關於IPTABLES的設置狀況centos7
iptables -L -n