Home >Backend Development >Golang >Why Does My Go App Get 'Recv failure: Connection reset by peer' When Running in a Docker Container?

Why Does My Go App Get 'Recv failure: Connection reset by peer' When Running in a Docker Container?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-16 05:19:03506browse

Why Does My Go App Get

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.

The above is the detailed content of Why Does My Go App Get 'Recv failure: Connection reset by peer' When Running in a Docker Container?. 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