我正在尝试创建一个Docker映像,该映像将通过VPN转发端口。我创建了一个简单的图像,它暴露了端口5144,并测试了它的正常工作:
sudo docker run -t -d -p 5144:5144 \
--name le-bridge \
--cap-add=NET_ADMIN \
--device=/dev/net/tun \
bridge
sudo docker exec -it le-bridge /bin/bash
我检查端口是否正确暴露,如下所示:
[CONTAINER] root@6116787b1c1e:~# nc -lvvp 5144
[HOST] user$ nc -vv 127.0.0.1 5144
然后,无论我键入什么内容,都会在容器的终端中正确回显。但是,一旦我启动openvpn守护程序,它将不再起作用:
[CONTAINER] root@6116787b1c1e:~# openvpn logger.ovpn &
[1] 33
Sun Apr 5 22:52:54 2020 OpenVPN 2.4.4 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on May 14 2019
Sun Apr 5 22:52:54 2020 library versions: OpenSSL 1.1.1 11 Sep 2018, LZO 2.08
Sun Apr 5 22:52:54 2020 TCP/UDP: Preserving recently used remote address: [AF_INET]
Sun Apr 5 22:52:54 2020 UDPv4 link local (bound): [AF_INET][undef]:1194
Sun Apr 5 22:52:54 2020 UDPv4 link remote:
Sun Apr 5 22:52:54 2020 WARNING: this configuration may cache passwords in memory -- use the auth-nocache option to prevent this
Sun Apr 5 22:52:55 2020 [] Peer Connection Initiated with [AF_INET]
Sun Apr 5 22:53:21 2020 TUN/TAP device tun0 opened
Sun Apr 5 22:53:21 2020 do_ifconfig, tt->did_ifconfig_ipv6_setup=0
Sun Apr 5 22:53:21 2020 /sbin/ip link set dev tun0 up mtu 1500
Sun Apr 5 22:53:21 2020 /sbin/ip addr add dev tun0 10.X.0.2/24 broadcast 10.X.0.255
Sun Apr 5 22:53:21 2020 Initialization Sequence Completed
root@6116787b1c1e:~#
root@6116787b1c1e:~# nc -lvvp 5144
listening on [any] 5144 ...
从这里,使用完全相同的netcat命令,我无法再从主机访问公开的端口。我想念什么?
编辑:值得一提的是,在VPN启动之后,连接仍然从主机成功;它只是永远不会到达容器内部的netcat进程。
我不确定为什么,但是事实证明,路由需要在容器内固定。就我而言,以下命令可以解决问题:
ip route add 192.168.0.0/24 via 172.17.42.1 dev eth0
...其中172.17.42.1
是我的主机上docker0接口的IP。希望有一天对某人有帮助。