I have a docker container with multiple network namespaces. Call them root, namespace1, namespace2.
The root container has an eni attached to it, with connectivity to the public internet.
Namespace 1 has two veth pairs:
veth_pair1 – connected to namespace2 with ip address address123.
veth_pair2 – connected to root with ip address234
In namespace 1, the ip route command shows:
default via address123 dev veth_pair1 (namespace2 ip address)
address234 dev veth_pair2
By default, traffic is routed over the default route through vethpair1 between namespace1 and namespace2.
What I want to do is under specific conditions, make http requests over the veth_pair2 (address234), while also using the dns lookup server of the root namespace.
Following two posts:
- https://koraygocmen.medium.com/custom-dns-resolver-for-the-default-http-client-in-go-a1420db38a5d
- How to bind an http.Client in Go to an IP Address
I constructed a custom DNS http client bound to the local address to try and make HTTP requests from the namespace1 container over the veth_pair2 through root container.
localTCPAddr, err := net.ResolveTCPAddr("tcp", "address234")
if err != nil {
log.Printf("failed to resolve tcp local address with error %v", err)
}
log.Printf("using local tcp address with any port: %v", localTCPAddr)
dialer := &net.Dialer{
Resolver: &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
LocalAddr: &localTCPAddr,
Timeout: time.Duration(5000) * time.Millisecond,
}
return d.DialContext(ctx, "tcp", "DNS server address")
},
},
}
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, network, addr)
}
//
customHttpClient := &http.Client{
Transport: &http.Transport{
DialContext: dialContext,
},
}
However, when the docker container starts and the task to send the HTTP request out from the namespace1 over the root container gets called, I get the following error:
real addresses have been replaced with variables
dial tcp: lookup #redactedURI on {DNS address}:
dial tcp {localAddress}:80->{customDNSserverAddress}: bind: cannot assign requested address
it seems from my research that the address is unavailable. but ive tried multiple iterations of the local address and ports, so im suspicious that its a different setup problem. any ideas?
Daniel Di Giulio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.