Docker Port Exposing Issue: Understanding "Recv failure: Connection reset by peer" Error
When attempting to run a Go app binary within a Docker container and access it via its mapped port, you may encounter the "Recv failure: Connection reset by peer" error. This indicates an issue with the port mapping or the application's internal configuration.
The issue arises when the application specifies an IP address like "localhost:8081" for its gRPC listening endpoint. When running within a container, this setup only allows connections from within the container itself.
Solution: Use Host-wide Listening
To resolve this issue, you should configure your application to bind to all available host interfaces, instead of "localhost". This can be achieved by simply omitting the IP address in your listening call:
http.ListenAndServe(":8081", nil)
By doing this, the application starts listening on all interfaces with port 8081, allowing connections from both within and outside the container. This resolves the issue and enables external access to your gRPC endpoint.
The above is the detailed content of Why Does My Docker Container Throw 'Recv failure: Connection reset by peer' When Accessing Its Mapped Port?. For more information, please follow other related articles on the PHP Chinese website!