Home  >  Article  >  Backend Development  >  Why Can't I Access My Exposed Docker Port? - Troubleshooting 'Recv failure: Connection reset by peer' in Go Applications

Why Can't I Access My Exposed Docker Port? - Troubleshooting 'Recv failure: Connection reset by peer' in Go Applications

Patricia Arquette
Patricia ArquetteOriginal
2024-11-19 14:17:021021browse

Why Can't I Access My Exposed Docker Port? - Troubleshooting

Docker Port Exposure Failure: Resolving "Recv failure: Connection reset by peer"

When attempting to run a Go application within a Docker container, you encounter the error "Recv failure: Connection reset by peer" when accessing an exposed port. Despite having correctly configured port mapping in the docker-compose.yaml file, the application remains unreachable from external sources.

The root cause of this issue lies in the way the Go application listens for incoming requests. By default, Go applications bind to the local loopback interface ("localhost") when using the syntax http.ListenAndServe("localhost:8081", nil).

Solution:

Within the Docker container, the Go application should listen on all available interfaces instead of restricting itself to the loopback interface. To achieve this, modify the following code in the Go application:

// Original code
http.ListenAndServe("localhost:8081", nil)

// Updated code
http.ListenAndServe(":8081", nil)

By removing the "localhost" prefix, the application now accepts connections from both the loopback interface and external sources. This modification ensures that the application can be accessed after exposing the port in the docker-compose.yaml file.

The above is the detailed content of Why Can't I Access My Exposed Docker Port? - Troubleshooting 'Recv failure: Connection reset by peer' in Go Applications. 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