Docker Port Exposure Issue: Resolving "Recv failure: Connection reset by peer"
Upon attempting to run a Go app binary within a Docker container, users may encounter an issue where the app fails to receive external connections. This error manifests itself as "Recv failure: Connection reset by peer" when invoking curl commands.
The source of this problem lies in the network configuration of the container. When the app is executed within the container, the following command is typically used:
http.ListenAndServe("localhost:8081", nil)
However, this configuration only allows connections to be established from within the container itself. To resolve this issue and enable external access to the app, the command should be modified to:
http.ListenAndServe(":8081", nil)
By omitting the "localhost" prefix, the app opens itself to connections originating from both within and outside the container. This modification allows for proper external communication with the app when it is run in a Docker environment.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3