请教个 docker 或是 iptables 防火墙问题:容器内能 ping 通 ip 但 ping 不通域名,提示“bad address”

2021-04-12 08:54:50 +08:00
 AllenHua

原帖发在了恩山,https://www.right.com.cn/forum/thread-4109783-1-1.html

dns 服务器应该不是问题了,可能就是防火墙的问题。用的是 x86 的 openwrt,防火墙是 iptables 程序,这条 iptables 规则怎么写? docker 中的容器 ping 不通域名这个问题是怎么一回事?

echo echo "91.189.92.201 archive.ubuntu.com" >> /etc/hosts 后就能 ping 通 archive.ubuntu.com 这个域名

搜到了下面的方法,但是我不知道用 iptables 该怎么解决,救救孩子🧒🏻吧

firewall-cmd --zone=public --add-masquerade --permanent
firewall-cmd --reload
systemctl restart docker
7143 次点击
所在节点    Linux
50 条回复
wunsch0106
2021-04-12 08:59:19 +08:00
修改下 dns
AllenHua
2021-04-12 09:04:46 +08:00
@wunsch0106 #1 改成了 223.5.5.5 180.76.76.76 8.8.8.8 这种公共 dns 都没成功

感觉是防火墙问题
cpstar
2021-04-12 09:08:10 +08:00
你那个 echo 相当于本地解析,所以 archive.ubuntu.com 肯定成功
判断的方法很简单,关掉防火墙,如果立马正常,那就是防火墙的问题。
加一条 udp53 的端口放行。但是定义了 wan 和 lan,默认 lan->wan 不拦截。
codehz
2021-04-12 09:09:15 +08:00
你用 nslookup/dig 测试 dns,别用 ping 测试域名,根本不是一个用途的
cpstar
2021-04-12 09:12:58 +08:00
原帖说的网络结构比本贴复杂啊
先找个外网的 IP,测试路由是否通顺,别 docker 里的数据包路由上就不带转发的。
然后网络结构略复杂,一层 NAT 就是一层损耗,想办法缩减一下结构吧。
AllenHua
2021-04-12 09:22:17 +08:00
@codehz #4 # ping 10.10.10.1 -c 2
PING 10.10.10.1 (10.10.10.1): 56 data bytes
64 bytes from 10.10.10.1: seq=0 ttl=64 time=0.458 ms
64 bytes from 10.10.10.1: seq=1 ttl=64 time=0.256 ms

--- 10.10.10.1 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.256/0.357/0.458 ms
/ # ping 8.8.8.8 -c 2
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=46 time=8.660 ms
64 bytes from 8.8.8.8: seq=1 ttl=46 time=8.481 ms

--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 8.481/8.570/8.660 ms
/ # cat /etc/resolv.conf
search lan
nameserver 8.8.8.8
nameserver 8.8.4.4
/ # dig baidu.com

; <<>> DiG 9.16.11 <<>> baidu.com
;; global options: +cmd
;; connection timed out; no servers could be reached

/ # ping baidu.com -c 2
ping: bad address 'baidu.com'

我的宿主机 是 10.10.10.1

docker 的 dns 指定了 8.8.8.8 和 8.8.4.4

能 ping 通 8.8.8.8 但是 ping 不通 baidu.com dig 也是超时,没有能到达的服务器
@cpstar #3
@cpstar #5
AllenHua
2021-04-12 09:23:37 +08:00
@cpstar #5 房东的路由器 我也没法拆 只能这样了吧,我自己还要在路由上搭建服务 于是只能 docker 内部 192.168.20.x -> 到我的路由器 10.10.10.1 再到房东的 192.168.1.1 然后出去
AllenHua
2021-04-12 09:24:32 +08:00
@cpstar #3 昨晚尝试 关掉了防火墙 /etc/init.d/firewall stop 整个网络都不行了 /捂脸
codehz
2021-04-12 09:26:14 +08:00
dig 强制指定一个 dns 试试看
dig @8.8.8.8 g.cn +trace
AllenHua
2021-04-12 09:33:13 +08:00
@codehz #9 谢谢,下面是输出

# dig @8.8.8.8 g.cn +trace

; <<>> DiG 9.16.11 <<>> @8.8.8.8 g.cn +trace
; (1 server found)
;; global options: +cmd
;; connection timed out; no servers could be reached
oluoluo
2021-04-12 09:34:16 +08:00
`iptables -A INPUT -p udp --dport 53 -j ACCEPT` 加个这个访问规则可不可以呢
AllenHua
2021-04-12 09:44:18 +08:00
@oluoluo #11 还是不可以 感谢。我附言一下最新“进展”
xuanbg
2021-04-12 09:58:10 +08:00
没有复杂网络需求的,容器一律使用宿主机网络,也就是 host 。
codehz
2021-04-12 09:58:11 +08:00
看起来是 udp 53 连接被阻止了,试试+tcp 的选项(附加到后面,另外多测试几种 dns 服务器,难说是抽风了)
应该就是防火墙的配置问题(((
AllenHua
2021-04-12 10:08:48 +08:00
@xuanbg #13 好的 谢谢
@codehz #14 感觉是 iptables 配置的问题

`iptables -t nat -vnL` 执行这一句 输出没有 docker0 这个接口的相关内容(防火墙小白阵亡……

https://www.cnblogs.com/wangxu01/articles/11316447.html 参考这个 感觉复杂 头大🤦🏻‍♂️
Jirajine
2021-04-12 10:17:16 +08:00
docker 的网络模式命名非常迷惑人,这个“bridge”模式在其他虚拟机等类似情况下都叫“nat 模式”,而对应传统虚拟机的 bridge 模式在 docker 里是 macvlan (类似但不同),而真正的 bridge 模式 docker 并不支持。
cpstar
2021-04-12 10:50:47 +08:00
哈,firewall 不能直接关,我好像忘了这茬事了。因为 iptables 上是不是带着路由转发呢,哎,不对啊,路由和 iptables 应当是两码事。

从 6#的情况看,肯定 UDP53 被拦截了。你定义一下 firewall 的区域。

然后我没整明白那个 10.10.10.x 是个什么存在,也就是 192.168.1.1-10.10.10.1-192.168.20.x ?有点怪。。。
AllenHua
2021-04-12 11:07:38 +08:00
@cpstar #17
@codehz #14
@oluoluo #11
![20210412110547.png]( https://cdn.jsdelivr.net/gh/hellodk34/image@main/img/20210412110547.png)

```
ACCEPT tcp -- anywhere anywhere tcp dpt:domain /* !fw3: tcpudp53 */
ACCEPT udp -- anywhere anywhere udp dpt:domain /* !fw3: tcpudp53 */
```

放行了 dns 查询的 53 端口 tcp+udp 容器内还是 ping 不通域名
bowser1701
2021-04-12 11:07:56 +08:00
试试 `dig @119.29.29.29 baidu.com` 和 `dig baidu.com` 比较一下看看是不是本地的 DNS server 出问题了。
zhangsanfeng2012
2021-04-12 11:10:24 +08:00
你的路由器是 openwrt,那你 openwrt 中能 ping 通域名吗?你的 docker 装在 openwrt 的机子里面?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/769968

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX