Home >Backend Development >Golang >Why does my Docker container throw a 'Recv failure: Connection reset by peer' error and how can I fix it?

Why does my Docker container throw a 'Recv failure: Connection reset by peer' error and how can I fix it?

DDD
DDDOriginal
2024-11-14 10:49:02455browse

Why does my Docker container throw a

Docker Port Exposure Issue: Resolving "Recv Failure: Connection Reset by Peer"

When attempting to run a Go application binary within a Docker container, you may encounter an issue where HTTP requests to the container result in the "Recv failure: Connection reset by peer" error. This typically occurs when the application is erroneously listening on the loopback interface.

According to your code snippet, the app is set to listen on "localhost:8081" using http.ListenAndServe("localhost:8081", nil). This configuration restricts connections to the loopback interface, making it inaccessible from external devices.

To resolve this, modify the listening address to bind to all interfaces, allowing connections from both within and outside the container. Replace the existing line with:

http.ListenAndServe(":8081", nil)

With this change, the app will accept connections on the specified port from all sources, regardless of their origin. This should eliminate the "Recv failure: Connection reset by peer" error and allow you to successfully reach your application.

The above is the detailed content of Why does my Docker container throw a 'Recv failure: Connection reset by peer' error and how can I fix it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn