← Back to home

WireGuard for your apps via a local SOCKS proxy on Docker

I’m a huge fan of Docker Compose and I’m a huge fan of Wireguard. It keeps everything simple and modular. In many cases where you want to scrape some data you’ll hit issues with your datacenter IP, so offering a simple way to allow your applications to proxy out is ideal. The issue with Wireguard though is if you bring up wg0 with AllowedIPs = 0.0.0.0/0, every application on the host goes through the tunnel.

For your laptop, that’s probably what you want, but for a single application on a server, it probably isn’t.

So I’ve set up the following SOCKS5 on localhost which any application can point to and use.

The setup

services:
  wireguard:
    image: masipcat/wireguard-go:latest
    container_name: wg-go-client
    # privileged: AllowedIPs 0.0.0.0/0 routing inside the netns
    privileged: true
    devices:
      - /dev/net/tun:/dev/net/tun
    sysctls:
      - net.ipv4.ip_forward=1
    volumes:
      - ./wg-client:/etc/wireguard
    # Publish the SOCKS port the socks service (shared netns) listens on
    ports:
      - "127.0.0.1:1080:1080"
    restart: unless-stopped

  socks:
    image: serjs/go-socks5-proxy
    container_name: wg-socks
    network_mode: "service:wireguard"
    environment:
      - REQUIRE_AUTH=false   # loopback-only; enable auth if exposing wider
    depends_on:
      - wireguard
    restart: unless-stopped

Keys live in ./wg-client/wg0.conf in standard wg-quick format:

Using it

Any tool that speaks SOCKS5:

# curl
curl --socks5-hostname localhost:1080 https://ifconfig.me

Go

Dial the proxy programmatically. In Go, with golang.org/x/net/proxy:

dialer, _ := proxy.SOCKS5("tcp", "127.0.0.1", 1080, proxy.Direct)
conn, err := dialer.Dial("tcp", "example.com:443")

No other code changes needed :)