我想以某种方式通过 docker-compose 配置两个容器,一个是 OpenVPN 客户端,另一个是 Squid HTTP 代理,公开 3128 端口以从外部访问该隧道。
现在,问题是由于某种原因,对 Squid 的请求需要整整 30 秒才能进入容器。
我开始调查该问题,似乎仅当 VPN 隧道连接并且暴露的端口位于其他容器中时才会重现。
示例 Web 服务器(模拟 Squid)正在监听 8080 端口。现在我对请求进行计时:
$ time { curl http://127.0.0.1:8080; }
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<body>
<h2>Directory listing for /</h2>
<hr>
<ul>
</ul>
<hr>
</body>
</html>
real 0m30.066s
user 0m0.005s
sys 0m0.011s
由于某种原因,数据包需要 30 秒才能到达容器内。将 Web 服务器与 OpenVPN 放在同一个容器内即可解决该问题。阻止 OpenVPN 连接可“解决”该问题。这在 Docker for Mac 和安装在 Ubuntu 虚拟机中的 Docker CE 上均有重现。
此外还有一个类似但不幸的是没有答案的问题:当来自 dockerhost 的数据包起作用时,如何让 Docker 从远程转发数据包?
为了您的方便,我提供了完整的工作配置来重现:
docker-compose.yml
version: "3.3"
services:
vpn:
build: ./vpn
cap_add:
- net_admin
devices:
- /dev/net/tun
restart: unless-stopped
ports:
- "8080:8080"
web:
image: trinitronx/python-simplehttpserver
depends_on:
- vpn
network_mode: "service:vpn"
restart: unless-stopped
VPN/Dockerfile
FROM dperson/openvpn-client
COPY vpn.conf /vpn/vpn.conf
COPY creds.txt /vpn/creds.txt
vpn/vpn.conf
client
dev tun
proto tcp
remote 176.126.237.217 443
remote euro217.vpnbook.com 443
resolv-retry infinite
nobind
persist-key
persist-tun
auth-user-pass /vpn/creds.txt
comp-lzo
verb 3
cipher AES-128-CBC
fast-io
pull
route-delay 2
redirect-gateway
<ca>
-----BEGIN CERTIFICATE-----
MIIDyzCCAzSgAwIBAgIJAKRtpjsIvek1MA0GCSqGSIb3DQEBBQUAMIGgMQswCQYD
VQQGEwJDSDEPMA0GA1UECBMGWnVyaWNo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