【问题标题】:Docker expose a port only to localhostDocker 只向 localhost 公开一个端口
【发布时间】:2019-09-29 16:25:56
【问题描述】:

我想限制我对127.0.0.1的数据库访问,所以我执行了以下命令:

docker run -it mysql:5.5 -p 127.0.0.1:3306:3306 -name db.mysql 

但我有些困惑……

这里可以看到只有127.0.0.1的端口会被转发:

; docker ps
mysql:5.5     127.0.0.1:3306->3306/tcp   db.mysql

有趣的是,我在 iptables 中找不到这个限制:

; iptables -L
Chain FORWARD (policy DROP)
DOCKER     all  --  anywhere             anywhere

Chain DOCKER (2 references)                                                                                                                                                                  
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             192.168.112.2        tcp dpt:mysql

这条规则的来源是anywhere

【问题讨论】:

    标签: docker iptables


    【解决方案1】:

    传入的流量将如下所示:

    Incoming package to host's network -> use ip tables to forward to container
    

    而且,您的限制不在 iptables 中,而是在主机的网络中,您只需在 127.0.0.1 上打开 3306 绑定,而不是 0.0.0.0,因此您当然在 iptables 中看不到任何内容。 127.0.0.1:3306:3306 表示hostIp:hostPort:containerPort

    你可以用netstat -oanltp | grep 3306确认,看没有0.0.0.0在那里,所以没有外国主机可以访问你的主机,因此也不能访问你的容器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-23
      • 2015-03-17
      • 1970-01-01
      • 2015-04-16
      • 2019-03-05
      • 2021-04-24
      • 2018-10-07
      相关资源
      最近更新 更多