Docker 端口转发适用于 IPv6,但不适用于 IPv4

Seb*_*Seb 6 networking portforwarding docker docker-compose

我有以下非常简单的 docker-compose.yml,在 Mac 上运行:

version: "3.7"
services:
  apache:
    image: httpd:2.4.41
    ports:
      - 80:80
Run Code Online (Sandbox Code Playgroud)

我运行docker-compose up,然后运行此命令curl,Apache 返回内容:

/tmp/test $ curl -v http://localhost
*   Trying ::1:80...
* TCP_NODELAY set
* Connected to localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.66.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Sat, 26 Oct 2019 18:30:03 GMT
< Server: Apache/2.4.41 (Unix)
< Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
< ETag: "2d-432a5e4a73a80"
< Accept-Ranges: bytes
< Content-Length: 45
< Content-Type: text/html
< 
<html><body><h1>It works!</h1></body></html>
* Connection #0 to host localhost left intact
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试使用 127.0.0.1 而不是 localhost 访问容器,则会出现连接被拒绝的情况:

/tmp/test $ curl -v http://127.0.0.1
*   Trying 127.0.0.1:80...
* TCP_NODELAY set
* Connection failed
* connect to 127.0.0.1 port 80 failed: Connection refused
* Failed to connect to 127.0.0.1 port 80: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused
Run Code Online (Sandbox Code Playgroud)

本地主机确实指向 127.0.0.1:

/tmp/test $ ping localhost
PING localhost (127.0.0.1): 56 data bytes
Run Code Online (Sandbox Code Playgroud)

netstat 显示所有要转发的本地 IP 地址的 80 端口:

/tmp/test $ netstat -tna | grep 80
...
tcp46      0      0  *.80                   *.*                    LISTEN     
...
Run Code Online (Sandbox Code Playgroud)

我实际上尝试使用文件中指向/etc/hosts127.0.0.1 的自定义域来访问容器。我认为该域名有问题,但后来我尝试了 127.0.0.1,但也不起作用,所以我得出的结论是,关于 docker 的一些非常基本的东西我做得不对。

为什么curl http://localhost有效但curl http://127.0.0.1无效?

更新

看来 localhost 正在解析为 IPv6 ::1,因此端口转发似乎适用于 IPv6 而不是 IPv4 地址。这有任何意义吗?

更新2

我无法修复它,但暂时将我的域名指向::1而不是127.0.0.1在我的服务中作为解决方法。/etc/hosts

更新3

8个月后,我遇到了同样的问题,并在这里发现了我自己的问题,但仍未得到解答。但这次我无法应用相同的解决方法,因为我需要将端口转发绑定到我的 IPv4 地址,以便可以从其他主机访问它。

Seb*_*Seb 4

找到罪魁祸首:pfctl

AFAIK,pfctl不应该自动运行,但我/System/Library/LaunchDaemons/com.apple.pfctl.plist说不然。

数据包过滤配置为将端口 80 上的所有传入流量重定向到 8080,将 443 重定向到 8443。这是在没有任何进程实际侦听端口 80 和 443 的情况下完成的,这就是为什么lsof并且netstat不会显示任何内容。

/Library/LaunchDaemons/it.winged.httpdfwd.plist有以下内容

 <key>ProgramArguments</key>
 <array>
  <string>sh</string>
  <string>-c</string>
  <string>echo "rdr pass proto tcp from any to any port {80,8080} -> 127.0.0.1 port 8080" | pfctl -a "com.apple/260.HttpFwdFirewall" -Ef - &amp;&amp; echo "rdr pass proto tcp from any to any port {443,8443} -> 127.0.0.1 port 8443" | pfctl -a "com.apple/261.HttpFwdFirewall" -Ef - &amp;&amp; sysctl -w net.inet.ip.forwarding=1</string>
 </array>
 <key>RunAtLoad</key>
Run Code Online (Sandbox Code Playgroud)

解决方案只是监听端口 8080 和 8443。所有对端口 80 和 443 的请求现在都被透明地重定向。

在调试时,我发现了无数关于类似问题的未解决问题,但没有答案。我希望这对某人有帮助。