You can use tun2proxy as a proxy for other Docker containers by running it in a container and sharing its network namespace with worker containers.
1. Build the image
You can build a statically linked image (based on musl) or images based on Ubuntu or Alpine:
# Build static image
docker buildx build -t tun2proxy .
# Build Ubuntu-based image
docker buildx build -t tun2proxy --target tun2proxy-ubuntu .
# Build Alpine-based image
docker buildx build -t tun2proxy --target tun2proxy-alpine .
2. Run the tun2proxy container
Start the container with necessary permissions (NET_ADMIN) and access to the TUN device:
docker run -d \
-v /dev/net/tun:/dev/net/tun \
--sysctl net.ipv6.conf.default.disable_ipv6=0 \
--cap-add NET_ADMIN \
--name tun2proxy \
tun2proxy --proxy proto://[username[:password]@]host:port
3. Connect worker containers
To route a worker container's traffic through tun2proxy, use the --network flag to join the tun2proxy container's network namespace:
docker run -it \
--network "container:tun2proxy" \
ubuntu:latest
docker buildx build -t tun2proxy .
docker run -d \
-v /dev/net/tun:/dev/net/tun \
--sysctl net.ipv6.conf.default.disable_ipv6=0 \
--cap-add NET_ADMIN \
--name tun2proxy \
tun2proxy --proxy proto://[username[:password]@]host:port
docker run -it \
--network "container:tun2proxy" \
ubuntu:latest