Golang API does returns an error -> curl: (56) Recv failure: Connection reset by peer

Hi,

I am building a simple API with go and I am trying to deploy with docker. The process builds, and when I run (from docker) the app starts.

But when I try to access endpoints with curl, I get this error (curl: (56) Recv failure: Connection reset by peer).

I have this snippet in my main function:

port := os.Getenv(“PORT”) if port == “” { port = “2000” }
and I then bind to listen and serve:
Like so:

err := http.ListenAndServe(":"+port, nil) if err != nil { //log.Fatal(“Server error”, err) }
I am not sure the error is coming from here, but I have tried to remove that first bit of code and leave the port empty, still makes no difference.

The API works without docker (with those snippets) but would not work if I try to run with docker.

what is the ‘endpoint’ ip address you are trying to connect to?

Hi @sdetweil. I am trying to connect to “localhost:2000” after run the app with docker

(with: docker run --publish 2000:8080 --name linktor-api --rm app )

ok, that --publish is host_port:container_port

so, where is the container listening on 8080?

ok, good… but where is code IN the container listening on port 8080?
cause the code says use port 2000 in the container

port := os.Getenv(“PORT”) if port == “” { port = “2000” }
and I then bind to listen and serve:
Like so:

err := http.ListenAndServe(":"+port, nil) if err != nil { //log.Fatal(“Server error”, err)

Oh! Yeah. Now I got it working. I did not understand that bit the entire time.

My app was listening on port 2000, but I used 8080 in docker run.

Thanks a lot @sdetweil!

no problem… thanks for letting us know…

the the port mapping will be successful, even if there is no app using the port.
Docker doesn’t KNOW, just doing what its told…