Docker Community Forums

Share and learn in the Docker community.

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


(Dharnnie) #1

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.


(Sam) #2

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


(Dharnnie) #3

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 )


(Sam) #4

ok, that --publish is host_port:container_port

so, where is the container listening on 8080?


(Sam) #5

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)

(Dharnnie) #6

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!


(Sam) #7

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…