今天,在運行docker容器將8080映射為內部80端口時失敗。報錯內容為“iptables failed”(如下圖)。
然后,經過搜索,認為應該啟用iptable防火牆,而不是firewall防火牆,然后,就有了iptables防火牆安裝和啟用的過程。這里記錄一下。
對應這個報錯,后來通過重啟docker解決了。
環境
操作系統:CentOS8 ,已安裝Docker(CentOS 8 的docker安裝 https://www.cnblogs.com/luyj00436/p/14515187.html)
步驟
1. 安裝iptable iptables-services。
1 #先檢查是否安裝了iptables 2 service iptables status 3 #安裝iptables 4 yum install -y iptables 5 #升級iptables(安裝的最新版本則不需要) 6 yum update iptables 7 #安裝iptables-services 8 yum install iptables-services
2. 禁用/啟用自帶的firewall服務。
1 #停止firewalld服務 2 systemctl stop firewalld 3 #禁用firewalld服務 4 systemctl mask firewalld
3.設置現有規則。
1 #查看iptables現有規則 2 iptables -L -n 3 #先允許所有,不然有可能會杯具 4 iptables -P INPUT ACCEPT 5 #清空所有默認規則 6 iptables -F 7 #清空所有自定義規則 8 iptables -X 9 #所有計數器歸0 10 iptables -Z 11 #允許來自於lo接口的數據包(本地訪問) 12 iptables -A INPUT -i lo -j ACCEPT 13 #開放22端口 14 iptables -A INPUT -p tcp --dport 22 -j ACCEPT 15 #開放21端口(FTP) 16 iptables -A INPUT -p tcp --dport 21 -j ACCEPT 17 #開放80端口(HTTP) 18 iptables -A INPUT -p tcp --dport 80 -j ACCEPT 19 #開放443端口(HTTPS) 20 iptables -A INPUT -p tcp --dport 443 -j ACCEPT 21 #允許ping 22 iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT 23 #允許接受本機請求之后的返回數據 RELATED,是為FTP設置的 24 iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 25 #其他入站一律丟棄 26 iptables -P INPUT DROP 27 #所有出站一律綠燈 28 iptables -P OUTPUT ACCEPT 29 #所有轉發一律丟棄 30 iptables -P FORWARD DROP 31 #如果要添加內網ip信任(接受其所有TCP請求) 32 iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT 33 #過濾所有非以上規則的請求 34 iptables -P INPUT DROP 35 #要封停一個IP,使用下面這條命令: 36 iptables -I INPUT -s ***.***.***.*** -j DROP 37 #要解封一個IP,使用下面這條命令: 38 iptables -D INPUT -s ***.***.***.*** -j DROP 39 #保存上述規則 40 service iptables save 41 #注冊iptables服務 42 #相當於以前的chkconfig iptables on 43 systemctl enable iptables.service 44 #開啟服務 45 systemctl start iptables.service 46 #查看狀態 47 systemctl status iptables.servic
4. 映射端口。
1 #將默認的3306端口映射長1306對外提供服務 2 iptables -t mangle -I PREROUTING -p tcp --dport 1306 -j MARK --set-mark 883306 3 iptables -t nat -I PREROUTING -p tcp --dport 1306 -j REDIRECT --to-ports 3306 4 iptables -I INPUT -p tcp --dport 3306 -m mark --mark 883306 -j ACCEPT
參考網址
Centos 7 docker 啟動容器 iptables 報 No chain/target/match by that name:https://blog.51cto.com/17099933344/1929664