大网数据广告活动
服务器运维问答

Block Postgres port 5432 running on Docker container with iptables

· 52CE 我爱测

I am using Ubuntu 14.04 desktop (Xubuntu to be exact), and trying to block outside access to a locally running Postgres port 5432 with iptables. This is my iptables -S -A INPUT -i lo -p tcp -m tcp --dport 5432 -j ACCEPT -A INPUT …

问题原文

原帖链接 · garbagecollector · CC BY-SA 3.0

I am using Ubuntu 14.04 desktop (Xubuntu to be exact), and trying to block outside access to a locally running Postgres port 5432 with iptables. This is my iptables -S

-A INPUT -i lo -p tcp -m tcp --dport 5432 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 5432 -j DROP

And iptables -L:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:postgresql
DROP       tcp  --  anywhere             anywhere             tcp dpt:postgresql

But somehow a network scanner app on my Android, connected to the same wifi, can still see it. I can further test it with a PSQL utility on my Android, and indeed I can see all my tables. What am I missing?

Edit: might be worth mentioning that this is Postgres running on a docker container. This may have something to do with it.

已采纳解答

原帖链接 · garbagecollector · CC BY-SA 3.0

Did more digging. This looks like Docker behavior that modifies the iptables FORWARD chain.

Chain FORWARD (policy DROP)
target     prot opt source               destination         
DOCKER-OVERLAY  all  --  anywhere             anywhere            
DOCKER-ISOLATION  all  --  anywhere             anywhere            
DOCKER     all  --  anywhere             anywhere

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

This website outlines the issue.

To prevent Docker from forwarding all access to your container, specify 127.0.0.1 when running the container.

docker run -p 127.0.0.1:5432:5432 postgres

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。