限制可以从docker容器中访问的IP地址的最佳方法是什么?

时间:2015-06-02 14:43:18

标签: docker iptables

我有一个答案,它会限制docker容器只能访问主机外的单个IP地址。在主机上使用此iptables规则:

# iptables -I FORWARD -i docker0  ! -d 8.8.8.8 -j DROP

表示从任何docker容器内部只能访问IP地址8.8.8.8。

这是相当激烈的 - 基本上,如果目的地不是8.8.8.8,那么丢弃数据包。

我可以设置哪些规则允许我将容器限制为一定数量的IP地址的最佳方式是什么?

3 个答案:

答案 0 :(得分:0)

使用iptables的最佳做法是首先删除所有IP或PORT中的所有内容。然后单独打开一些端口。

这是他用来管理端口的示例(脚本),但我假设你可以用IP做同样的事情:

#!/bin/sh

# reset :
iptables -t filter -F
iptables -t filter -X

# Block all :
iptables -t filter -P INPUT DROP
iptables -t filter -P FORWARD DROP
iptables -t filter -P OUTPUT DROP

# Authorize already established connections :
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

# Authorize backloop :
iptables -t filter -A INPUT -i lo -j ACCEPT
iptables -t filter -A OUTPUT -o lo -j ACCEPT

# Authorize ssh :
iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 22 -j ACCEPT

# Authorize HTTP :
iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 80 -j ACCEPT

# Authorize HTTPS :
iptables -t filter -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 443 -j ACCEPT

# Authorize DNS :
iptables -t filter -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -t filter -A INPUT -p udp --dport 53 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 53 -j ACCEPT

# Ping :
iptables -t filter -A INPUT -p icmp -j ACCEPT
iptables -t filter -A OUTPUT -p icmp -j ACCEPT

# Authorize FTP :
iptables -t filter -A INPUT -p tcp --dport 20 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 20 -j ACCEPT
iptables -t filter -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 21 -j ACCEPT

# # Authorize NTP :
# iptables -t filter -A INPUT -p udp --dport 123 -j ACCEPT
# iptables -t filter -A OUTPUT -p udp --dport 123 -j ACCEPT

# Authorize IRC :
iptables -t filter -A INPUT -p tcp --dport 6667 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 6667 -j ACCEPT

# Authorize port 10000 (for Node.JS server) :
iptables -t filter -A INPUT -p tcp --dport 10000 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 10000 -j ACCEPT

# Authorize port 631 (Cups server) :
iptables -t filter -A INPUT -p tcp --dport 631 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 631 -j ACCEPT

我希望这会有所帮助。

答案 1 :(得分:0)

如果您有一个允许访问的特定地址列表,您可能需要调查ipset命令,该命令允许您维护内核中可以使用的IP地址列表iptables规则。这可能会大大简化您的规则集。

创建一个新的ipset:

ipset create dockerdests hash:ip

在集合中添加一些地址:

ipset add dockerdests 8.8.8.8
ipset add dockerdests 162.13.208.130

创建引用集合的iptables规则:

iptables -I FORWARD 1 -i docker0 -m set --match-set dockerdests dst -j ACCEPT
iptables -I FORWARD 2 -i docker0 -j DROP

这些命令在FORWARD链的顶部插入两条规则;第一个将接受从的数据包从指定的ip集中列出的任何内容,第二个规则将丢弃docker0上未被接受的任何内容按照以前的规则。

如果你走这条路线,你需要安排在系统启动时加载你的ipset配置。

可以找到有关ipset的更多信息here

答案 2 :(得分:-1)

咄!!!

漫长的一天 - 没有任何借口......

我假设我可以添加一些记录......

如果IP1则允许。 如果IP2然后允许。 如果IP3然后允许。 其他一切都是DROP。

这似乎有效:

# iptables -I FORWARD -i docker0  -j DROP
# iptables -I FORWARD -i docker0  -d 8.8.8.8 -j ACCEPT
# iptables -I FORWARD -i docker0  -d 4.2.2.2 -j ACCEPT

导致iptables规则:

root@jessie-amd64:~# iptables -L -n 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            4.2.2.2             
ACCEPT     all  --  0.0.0.0/0            8.8.8.8             
DROP       all  --  0.0.0.0/0            0.0.0.0/0           
DOCKER     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain DOCKER (1 references)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            172.17.0.2           tcp dpt:3306
ACCEPT     tcp  --  0.0.0.0/0            172.17.0.2           tcp dpt:80
ACCEPT     tcp  --  0.0.0.0/0            172.17.0.2           tcp dpt:22
root@jessie-amd64:~#
相关问题