我的防火墙正在阻止从Docker容器到外部的网络连接

问题描述 投票:11回答:5

对我来说,这是一个非常标准的设置,我有一台运行docker和ufw的ubuntu计算机作为我的防火墙。

如果启用了我的防火墙,则Docker实例将无法连接到外部

$ docker run -i -t ubuntu /bin/bash
WARNING:  Docker detected local DNS server on resolv.conf. Using default external servers: [8.8.8.8 8.8.4.4]
root@d300c5f17207:/# apt-get update
Err http://archive.ubuntu.com precise InRelease
0% [Connecting to archive.ubuntu.com]
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/precise/InRelease  
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/precise/Release.gpg  Temporary failure resolving 'archive.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.

这里是ufw日志,显示了来自Docker容器的被阻止的连接。

$ sudo tail /var/log/ufw.log
Jun 30 15:41:56 localhost kernel: [61609.503199] [UFW BLOCK] IN=testbr0 OUT=eth0 PHYSIN=veth8Rj8Nh MAC=fe:ff:ed:42:b0:01:0a:7c:42:7c:a6:72:08:00 SRC=172.16.42.2 DST=8.8.8.8 LEN=64 TOS=0x00 PREC=0x00 TTL=63 ID=14886 DF PROTO=UDP SPT=60192 DPT=53 LEN=44 
Jun 30 15:42:01 localhost kernel: [61614.500867] [UFW BLOCK] IN=testbr0 OUT=eth0 PHYSIN=veth8Rj8Nh MAC=fe:ff:ed:42:b0:01:0a:7c:42:7c:a6:72:08:00 SRC=172.16.42.2 DST=8.8.4.4 LEN=64 TOS=0x00 PREC=0x00 TTL=63 ID=16137 DF PROTO=UDP SPT=44812 DPT=53 LEN=44 
Jun 30 15:42:06 localhost kernel: [61619.498516] [UFW BLOCK] IN=testbr0 OUT=eth0 PHYSIN=veth8Rj8Nh MAC=fe:ff:ed:42:b0:01:0a:7c:42:7c:a6:72:08:00 SRC=172.16.42.2 DST=8.8.8.8 LEN=64 TOS=0x00 PREC=0x00 TTL=63 ID=14887 DF PROTO=UDP SPT=60192 DPT=53 LEN=44

我尝试使用ip添加规则。

$ sudo ufw allow in from 172.16.42.2
$ sudo ufw allow out from 172.16.42.2

并且没有任何变化仍被阻止。

如何使用ufw规则轻松地允许从容器到外部的所有连接?

firewall docker
5个回答
7
投票

也许这是由于当前版本引起的,但是当前答案在我的系统上不起作用(带有基本Ubuntu映像的Docker 0.7.2)。

解决方案在here in the official Docker documentation中进行了说明。

对于懒惰的人:

  • 编辑/etc/default/ufw以将DEFAULT_FORWARD_POLICY的值更改为"ACCEPT"
  • 重新加载[sudo] ufw reload

这将确保ufw将您的流量转发到Docker的桥接网络(根据我目前对这些事情的了解...)。>


19
投票

如下编辑/etc/ufw/before.rules


18
投票
这已为我修复:

0
投票
我将下一个过滤器用于docker网络

-2
投票
您确定ufw允许相关端口上的连接吗?
© www.soinside.com 2019 - 2024. All rights reserved.