docker出现Error response from daemon: driver failed programming external connectivity on endpoint解决方法

1
2
3
4
5
[root@VM-12-7-centos app]# docker run -d -p 8848:8848 -e MODE=standalone -e PREFER_HOST_MODE=hostname   nacos/nacos-server
cc4138848eb1964184ae7ad86a8f12ee22bfc4c18c2a65d22805ef82b443cc5a
docker: Error response from daemon: driver failed programming external connectivity on endpoint wonderful_bassi (709d64059def489b61481a01712c69cf6b379b5da8cec6ffc66a9bed4b0fb295): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8848 -j DNAT --to-destination 172.17.0.3:8848 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1)).

问题产生和原理

在网上查了很多资料,主要是因为在启动docker容器的时候或者做docker配置的时候,还对防火墙设置重新启动等配置
这样会清除docker的相关配置,导致在查询防火墙规则的时候显示不到docker的链
iptables -L 查询iptables 链

而docker容器的底层原理:
在启动docker的时候会自动在iptables中注册一个链,通过防火墙的链也可以找到其注册的信息,主要注册这些链,是docker中的容器为了暴露端口而使用的

具体原因是你删除了iptables中的链
删除链接的方式有很多种

  • 重启firewalld防火墙即可对其清除,firewalld是centos7以上,iptables是centos6以下都会有,而firewalld的底层是涉及在iptables上的,在启动firewalld的时候会自动删除iptables链的相关链接
  • 删除该链接的其他方式博主暂未遇到有特殊情况的,待发现

所以在涉及防火墙firewalld的命令或者是iptables中的命令的时候
小心删除其涉及docker的链
具体这两个命令可查看其博主相关的文章

问题解决

既然是firewalld重启导致
而docker重启又会将其注册iptables链回来
那么 我们只需要将其重启docker容器即可解决该问题

1
systemctl restart docker

再次查询器docker的链iptables -L
或者是通过这条命令查询iptables -t nat -nL