无法使用我的自定义 iptables 从主机的公开端口访问容器服务

无法使用我的自定义 iptables 从主机的公开端口访问容器服务

我正在运行安装了 tomcat8 的 debian8 容器。在我的 Dockerfile 中,我有EXPOSE 8080,当我运行容器时,我设置了-p 8080:8080。它工​​作正常,我可以从主机访问 tomcathttp://本地主机:8080在浏览器上。

现在,我需要阻止任何来自互联网的入站/出站连接(在容器中),但保持局域网连接,这就是为什么我在容器中的 iptables 中添加了这些规则:

iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -A INPUT -s 192.168.0.0/24 -j ACCEPT
iptables -A OUTPUT -d 192.168.0.0/24 -j ACCEPT
iptables -A INPUT -s 127.0.0.0/8 -j ACCEPT
iptables -A OUTPUT -d 127.0.0.0/8 -j ACCEPT

请注意,我并不是 iptables 专家,但从容器内部进行 ping 的快速测试表明,我无法 ping 任何 WAN 地址,但仍然可以 ping LAN 地址,这正是我想要的。问题是,有了这些规则,我无法再从容器外部(在主机上运行的浏览器)访问 tomcat。为什么?请注意,我可以从容器 ping 主机。

我的docker信息(windows主机上的unix容器):

Server Version: 17.03.0-ce
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 25
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host ipvlan macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 977c511eda0925a723debdc94d09459af49d082a
runc version: a01dafd48bc1c7cc12bdb01206f9fea7dd6feb70
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.9.12-moby
Operating System: Alpine Linux v3.5
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.934 GiB
Name: moby
ID: JZ7S:H2PG:4YQ4:GLTO:DVBO:QILY:QYJ2:KRS5:VKJZ:LIXZ:ZAGQ:WEHY
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
 File Descriptors: 29
 Goroutines: 50
 System Time: 2017-03-29T13:32:41.4233556Z
 EventsListeners: 2
Registry: https://index.docker.io/v1/
Experimental: true
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

答案1

原因是您的规则 INPUT 和 OUPUT 仅允许访问/192.168.0.0/24 网络。如果您想从 WAN 访问,则必须指定可以访问您的服务的主机或网络

例如:

iptables -A INPUT -s X.X.X.X -j ACCEPT

iptables -A OUTPUT -d X.X.X.X -j ACCEPT

XXXX => 您想要允许访问您的 Docker 中的服务的互联网中的主机。

相关内容