Docker-可从外部访问的公开端口-忽略iptables规则


15

我有一个像这样运行的docker容器:

 docker run --name some_container_1 -p 8080:80 -d some_image

哪个工作正常。该容器将其端口80暴露给8080,可从localhost访问。

但是由于某种原因,它完全忽略了INPUT iptables规则,并且也可以从外部访问。

如何限制对Docker容器的访问以仅允许IP 123.456.789.0从外部访问它?

谢谢。


须藤iptables -L -n -v --line-numbers

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
2      365 23380 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
4        7   788 LOG        all  --  *      *       0.0.0.0/0            0.0.0.0/0            limit: avg 5/min burst 5 LOG flags 0 level 7 prefix "iptables denied: "
5        7   788 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       24  1524 DOCKER     all  --  *      docker0  0.0.0.0/0            0.0.0.0/0           
2        0     0 ACCEPT     all  --  *      docker0  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
3       15 13320 ACCEPT     all  --  docker0 !docker0  0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     all  --  docker0 docker0  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT 204 packets, 21792 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain DOCKER (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1       24  1524 ACCEPT     tcp  --  !docker0 docker0  0.0.0.0/0            172.17.0.2           tcp dpt:80

须藤iptables-保存

# Generated by iptables-save v1.4.21 on Wed Apr  8 23:37:43 2015
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [100:16642]
:DOCKER - [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
-A INPUT -j DROP
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
-A DOCKER -d 172.17.0.2/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 80 -j ACCEPT
COMMIT
# Completed on Wed Apr  8 23:37:43 2015
# Generated by iptables-save v1.4.21 on Wed Apr  8 23:37:43 2015
*nat
:PREROUTING ACCEPT [13:2206]
:INPUT ACCEPT [1:64]
:OUTPUT ACCEPT [4:268]
:POSTROUTING ACCEPT [4:268]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
-A POSTROUTING -s 172.17.0.2/32 -d 172.17.0.2/32 -p tcp -m tcp --dport 80 -j MASQUERADE
-A DOCKER ! -i docker0 -p tcp -m tcp --dport 8080 -j DNAT --to-destination 172.17.0.2:80
COMMIT
# Completed on Wed Apr  8 23:37:43 2015

码头工人信息

Containers: 1
Images: 25
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 27
Execution Driver: native-0.2
Kernel Version: 3.16.0-4-amd64
Operating System: Debian GNU/Linux 8 (jessie)
CPUs: 4
Total Memory: 7.746 GiB
Name: nuc-001
ID: WCMU:MN3T:VFKR:IU42:6423:OEI6:IB5Q:WBNV:K75H:JZDS:UWU5:57WD
WARNING: No memory limit support
WARNING: No swap limit support

下降投票者可以解释吗?
二元异常2015年

1
不是我,但是我想无论是谁,都认为这更多是服务器故障而不是编程问题。他们可能是对的;您可能要迁移它。

1
您可以显示的输出iptables-save吗?我怀疑docker通过MASQUERADE / NAT通过iptables规则进行端口转换的方式,但我实际上不知道
ComputerDruid

Answers:


12

Docker内部使用iptables将端口8080上的Docker主机的连接转发到侦听容器上端口80的服务。您配置中的关键是这一行-

-A DOCKER ! -i docker0 -p tcp -m tcp --dport 8080 -j DNAT --to-destination 172.17.0.2:80

通过插入(-I)新的转发线,您可以阻止连接转发到容器IP,在这种情况下为172.17.0.2。试试这个规则-

/sbin/iptables -I FORWARD '!' -s 123.456.789.0 -d 172.17.0.2 -p tcp --dport 80 -j DROP


1
启动新容器时是否可以自动插入此规则?
Nicolas Massart

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.