uNetworking / uSockets

Miniscule cross-platform eventing, networking & crypto for async applications
Apache License 2.0
1.29k stars 267 forks source link

Server fails to bind localhost in container #209

Open Hanaasagi opened 1 year ago

Hanaasagi commented 1 year ago

From https://github.com/oven-sh/bun/issues/5315

Reproduce

  1. Edit examples/http_server.c

https://github.com/uNetworking/uSockets/blob/ac4e90e4d2ad674db903accc5f3abc408a6bc5d8/examples/http_server.c#L123

change to bind localhost:3000

        struct us_listen_socket_t *listen_socket = us_socket_context_listen(SSL, http_context, "localhost", 3000, 0, sizeof(struct http_socket));
  1. Create a container using the debian:sid-slim image and then run the http_server. (docker has not been configured for IPv6 stack)
root@5b4095ebc3eb:/tmp/uSockets# ./http_server
Failed to listen!
root@5b4095ebc3eb:/tmp/uSockets# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      5b4095ebc3eb
root@5b4095ebc3eb:/tmp/uSockets# vim /etc/hosts  # Remove `::1     localhost ip6-localhost ip6-loopback`
root@5b4095ebc3eb:/tmp/uSockets# ./http_server  # works now
Listening on port 3000...
^C

The server fails to bind, but if you remove ::1 from /etc/hosts, it starts successfully. It binds 127.0.0.1 now.

Regarding why there is still ::1 record present in the container even though IPv6 is not configured, please see https://github.com/moby/moby/issues/35954