Docker Desktop Windows and VPN - no network connection inside container Docker Desktop Windows and VPN - no network connection inside container docker docker

Docker Desktop Windows and VPN - no network connection inside container


Actually i did it using Docker Desktop and Hyper-V virtual machines. Using OpenConnect but i think it can be done for most VPN client with minor adaptations.

The fully explained instructions are here Docker Desktop, Hyper-V and VPN with the settings for Docker containers, Windows VMs and Linux VMs

  • I created a new internal Virtual Switch (let's call it "Internal") and assigned to it a static IP address (let's say 192.168.4.2)

  • I created a new VM with Ubuntu server and OpenConnect, connected to both the default Virtual Switch and the "Internal"

  • On the OpenConnect VM

    • Assigned to "Internal" a fixed ip (192.168.4.3)

    • Added a new tun interface "persistent" telling openconnect to use that tun (adding the "-i tun0" parameter as openconnect start parameter)

      sudo ip tuntap add name tun0 mode tun

    • Installed the persist-iptables

    • Forced the ip forwarding

      sudo echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf && sysctl -p

    • Setup the routing

      sudo iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADEsudo iptables -A FORWARD -i eth0 -o tun0 -j ACCEPTsudo iptables -A FORWARD -o tun0 -j ACCEPTsudo iptables -A FORWARD -i tun0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPTsudo iptables -A INPUT -i tun0 -j ACCEPT

    • After connecting the vpn i added permanently the dns servers to the resolve.conf

    • And retrieve the class of addresses of the VPN (like 10...* )

  • On the Docker containers

    • Added on Dockerfile the basic route

      RUN route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.4.3

    • Then running the docker file i added the dns giving net admin and sys module permissions

      --dns 8.8.8.8 --dns 10.1.77.21 --dns 10.4.52.21 --dns-search test.dns.it
      --cap-add=NET_ADMIN --cap-add=SYS_MODULE