Ubuntu 22.04:docker:无法从外部访问容器

Phi*_*ipp 5 iptables networking docker 22.04

有一个现成的 Ubuntu 22.04 安装并根据安装了 docker 。

\n

我还使用 docker-compose 启动了三个容器,它们使用docker ps.

\n
CONTAINER ID   IMAGE                             COMMAND                  CREATED          STATUS                   PORTS                                                                                  NAMES\n60cf25f59a92   jc21/nginx-proxy-manager:latest   "/init"                  48 minutes ago   Up 8 minutes             0.0.0.0:80-81->80-81/tcp, :::80-81->80-81/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   infra_nginx_proxy_1\nf8081b137abf   graylog/graylog:4.2               "tini -- /docker-ent\xe2\x80\xa6"   55 minutes ago   Up 8 minutes (healthy)   0.0.0.0:1514->1514/tcp, :::1514->1514/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp   infra_graylog_1\nd3fef3332752   elasticsearch:7.9.3               "/tini -- /usr/local\xe2\x80\xa6"   55 minutes ago   Up 8 minutes             9200/tcp, 9300/tcp                                                                     infra_elasticsearch_1\n6b7920e89790   mongo:4.4.9                       "docker-entrypoint.s\xe2\x80\xa6"   56 minutes ago   Up 8 minutes             27017/tcp                                                                              infra_mongo_db_1\n
Run Code Online (Sandbox Code Playgroud)\n

现在,当尝试在本地卷曲其中一个时,一切都很好

\n
root$: curl http://localhost:81\n\n<!doctype html><html lang="en" dir="ltr"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1">...\n
Run Code Online (Sandbox Code Playgroud)\n

但同样的卷曲(当然是针对公共IP)以超时结束。

\n

在 docker 主机上执行 tcpdump 可以发现,流量正在到达

\n
root$: tcpdump port 81\ntcpdump: verbose output suppressed, use -v[v]... for full protocol decode\nlistening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes\n19:53:34.520888 IP 10.0.1.27.51942 > docker-infrastruktur.81: Flags [S], seq 1942358053, win 64240, options [mss 1460,sackOK,TS val 1217328475 ecr 0,nop,wscale 7], length 0\n19:53:35.535503 IP 10.0.1.27.51942 > docker-infrastruktur.81: Flags [S], seq 1942358053, win 64240, options [mss 1460,sackOK,TS val 1217329490 ecr 0,nop,wscale 7], length 0\n19:53:37.551436 IP 10.0.1.27.51942 > docker-infrastruktur.81: Flags [S], seq 1942358053, win 64240, options [mss 1460,sackOK,TS val 1217331506 ecr 0,nop,wscale 7], length 0\n19:53:41.775510 IP 10.0.1.27.51942 > docker-infrastruktur.81: Flags [S], seq 1942358053, win 64240, options [mss 1460,sackOK,TS val 1217335730 ecr 0,nop,wscale 7], length 0\n
Run Code Online (Sandbox Code Playgroud)\n

iptables - 所有 docker 生成的 - 看起来像这样

\n
sudo iptables --list\n\nChain INPUT (policy ACCEPT)\ntarget     prot opt source               destination         \n\nChain FORWARD (policy DROP)\ntarget     prot opt source               destination         \nDOCKER-USER  all  --  anywhere             anywhere            \nDOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere            \nACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED\nDOCKER     all  --  anywhere             anywhere            \nACCEPT     all  --  anywhere             anywhere            \nACCEPT     all  --  anywhere             anywhere            \nACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED\nDOCKER     all  --  anywhere             anywhere            \nACCEPT     all  --  anywhere             anywhere            \nACCEPT     all  --  anywhere             anywhere            \n\nChain OUTPUT (policy ACCEPT)\ntarget     prot opt source               destination         \n\nChain DOCKER (2 references)\ntarget     prot opt source               destination         \nACCEPT     tcp  --  anywhere             172.18.0.2           tcp dpt:https\nACCEPT     tcp  --  anywhere             172.18.0.2           tcp dpt:81\nACCEPT     tcp  --  anywhere             172.18.0.2           tcp dpt:http\nACCEPT     tcp  --  anywhere             172.18.0.5           tcp dpt:9000\nACCEPT     tcp  --  anywhere             172.18.0.5           tcp dpt:1514\n\nChain DOCKER-ISOLATION-STAGE-1 (1 references)\ntarget     prot opt source               destination         \nDOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            \nDOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            \nRETURN     all  --  anywhere             anywhere            \n\nChain DOCKER-ISOLATION-STAGE-2 (2 references)\ntarget     prot opt source               destination         \nDROP       all  --  anywhere             anywhere            \nDROP       all  --  anywhere             anywhere            \nRETURN     all  --  anywhere             anywhere            \n\nChain DOCKER-USER (1 references)\ntarget     prot opt source               destination         \nRETURN     all  --  anywhere             anywhere            \n
Run Code Online (Sandbox Code Playgroud)\n

甚至nmap显示所有的门都是敞开的

\n
root$: sudo nmap localhost\n\nStarting Nmap 7.80 ( https://nmap.org ) at 2022-08-12 19:56 UTC\nNmap scan report for localhost (127.0.0.1)\nHost is up (0.000013s latency).\nNot shown: 995 closed ports\nPORT     STATE SERVICE\n22/tcp   open  ssh\n80/tcp   open  http\n81/tcp   open  hosts2-ns\n443/tcp  open  https\n9000/tcp open  cslistener\n
Run Code Online (Sandbox Code Playgroud)\n

有趣的是,当启动本地网络服务器时,例如,python3 -m http.server 81它会启动(关闭容器后)并且可以访问。因此,我可以完全确定外部防火墙甚至主机操作系统的问题......

\n

请帮忙和建议..

\n

lp_*_*lp_ 9

我遇到了同样的问题(尽管我的 22.04 是从 20.04 升级的)。症状完全相同,所以我在这里分享解决方案,也许对某人有帮助。我尝试按照此处的建议重新安装 docker,但没有成功。重新安装服务器不是一个选择。

最后,该问题与iptables-nft 和 snap的 bug 有关(也报告给docker-snap):

在配置的符号链接之外操纵防火墙的软件(或者在使用 nftables 时使用 iptables-legacy/iptables 1.6)如果不知道 xtables/netfilter 不兼容,可能会引入问题。例如,对于发布自己的 iptables 或 nftables 并无条件使用它而不考虑系统上现有规则的 snap 可能会发生这种情况。[...] 自 2012 年 10 月 20 日起,稳定通道中的“docker”snap 已知无条件使用 xtable。在撰写本文时,它还没有办法调整为使用 netfilter,因此如果使用 docker snap,您可能必须更新系统以使用 iptables-legacy(在 Debian/Ubuntu 上,请参阅上面的 update-alternatives) )。

我发现 that/etc/alternatives/iptables指向/usr/sbin/iptables-nft,但我也有iptables-legacy规则。看:

$ sudo iptables -S
# Warning: iptables-legacy tables present, use iptables-legacy to see them
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
...
$ sudo iptables-legacy -S
-P INPUT ACCEPT
-P FORWARD DROP
-P OUTPUT ACCEPT
...
Run Code Online (Sandbox Code Playgroud)

发现这一点后,解决方案很简单:

sudo iptables-legacy -P FORWARD ACCEPT
Run Code Online (Sandbox Code Playgroud)


小智 5

无需重新安装整个系统。问题是安装程序提供的 docker 的“snap”版本。如果您之后通过 apt 安装 docker,您最终将拥有两个二进制文件。只需取下按扣即可

sudo snap remove docker
Run Code Online (Sandbox Code Playgroud)

并从官方 docker 存储库重新安装

sudo apt-get install --reinstall docker-ce docker-ce-cli containerd.io docker-compose-plugin
Run Code Online (Sandbox Code Playgroud)

检查: https: //docs.docker.com/engine/install/ubuntu/


Phi*_*ipp 3

经过更多分析后,我们找到了一个原因或影响因素:在安装操作系统时,选择同时安装 docker。

\n

之后我们按照安装指南(https://docs.docker.com/engine/install/ubuntu/)并删除了所有“旧”版本

\n
sudo apt-get remove docker docker-engine docker.io containerd runc\n
Run Code Online (Sandbox Code Playgroud)\n

然后我们从 docker apt 源安装。

\n

我们重新安装了操作系统,这次没有安装 docker,等等 voil\xc3\xa1:安装 docker 后一切正常。

\n