Containerized API socket hangs up

Viewed 1559

I just finished working on a Go REST API and I would like to move my code into a container. I already accomplished this, but I am unable to hit my endpoints through postman since the error: Error: socket hang up is thrown.

According to this SO thread, this could be caused by my firewall, after running ufw status I noticed my firewall is not up, so I am unsure what could be causing this error.

My app seem to be running correctly and I mapped the container with port 8080 in my local machine.

└─(19:14:04 on master ✭)──> docker ps                                                                                                                     ──(Tue,Feb02)─┘
CONTAINER ID   IMAGE        COMMAND       CREATED          STATUS          PORTS                    NAMES
b001f19e2957   go-library   "./library"   15 minutes ago   Up 15 minutes   0.0.0.0:8080->8080/tcp   cranky_kare

I also made sure my request are using this port.

GET http://localhost:8080/books/3
Error: socket hang up
Request Headers
User-Agent: PostmanRuntime/7.26.10
Accept: */*
Postman-Token: 190a5849-b859-4324-a65e-37a7b69b49fb
Host: localhost:8080
Accept-Encoding: gzip, deflate, br
1 Answers

My problem was that I was starting the server on 127.0.0.1 and the port mapping was not pointing to this interface. Starting it on all interfaces (0.0.0.0) worked for me:

addr := fmt.Sprintf("0.0.0.0:%v", settings.Port)
server := &http.Server{
    Addr:         addr,
    Handler:      app.Router,
    IdleTimeout:  120 * time.Second,
    ReadTimeout:  5 * time.Second,
    WriteTimeout: 5 * time.Second,
}
Related