Docker Port Exposure Issue: Resolving "Recv failure: Connection reset by peer"
In a Docker environment, you've encountered an issue while trying to expose a gRPC service running in a container. The service, listening on port 8081, fails to receive connections with the error "Recv failure: Connection reset by peer."
Your Docker Compose configuration correctly maps port 8081 to the container's internal port, but the issue arises due to the way the gRPC server binds to its listening address. By default, when using "http.ListenAndServe("localhost:8081", nil)," the server listens only on the loopback interface (127.0.0.1). This means that it can only accept connections from within the container itself.
To resolve this issue, you can modify your Go application to listen on all interfaces. This can be achieved by using the following code instead:
http.ListenAndServe("0.0.0.0:8081", nil)
By specifying "0.0.0.0," the server will bind to all network interfaces, allowing it to accept both loopback and external connections. This will ensure that requests from outside the container can reach your gRPC service.
Additional Notes:
The above is the detailed content of Why Can't I Connect to My gRPC Service in Docker: 'Recv failure: Connection reset by peer'?. For more information, please follow other related articles on the PHP Chinese website!