如何限制端口到由docker-machine创build的主机?

我想创build一个数据库机器,只接受从某些IP到5432的连接。 我想允许连接到这台数据库机器的其他机器没有运行docker。 我使用docker-compose文件在数字海洋上部署了一台使用docker-machine的主机:

 postgres: restart: always image: postgres:latest volumes: - /tmp/postgrescont:/tmp volumes_from: - data env_file: .dbenv ports: - "5432:5432" data: restart: always image: postgres:latest volumes: - /var/lib/postgresql command: "true" 

并试图只允许来自另一个像这样的主机的stream量:

 $ iptables -A INPUT -p tcp --dport 5432 -s <ip address of certain other host> -j ACCEPT $ iptables -A INPUT -p tcp --dport 5432 -j DROP # deny other traffic 

但是,由于某种原因,我仍然可以从不需要的主机连接。 任何想法,为什么它不工作?

这是我能够将端口5432限制到某个IP:

 iptables -I DOCKER 1 -p tcp ! -s <other_host_ip_allowed> --dport 5432 -j DROP 

我插入规则是DOCKER链的第一条规则,并删除不是我指定的其他IP。

这正是Docker推荐的。

Docker的转发规则默认允许所有外部源IP。 要仅允许特定的IP或networking访问容器,请在DOCKERfilter链的顶部插入否定规则。 例如,要限制外部访问,只有源IP地址8.8.8.8才能访问容器,可以添加以下规则:

$ iptables -I DOCKER -i ext_if! -s 8.8.8.8 -j DROP

Dockernetworking