Tim J
Tim J

Reputation: 1271

Go API in container not returning any response

I have a simple API written in GO using Mux.

When running locally it works, however I am trying to containerize this app.

My main.go looks like this

package main

import (
    "net/http"
    "time"
    "log"
    "github.com/gorilla/mux"
    "github.com/<username>/tweet-media-api/controllers"
)

func main() {

    r := mux.NewRouter()

    c := controllers.Controller{}

    r.HandleFunc("/", c.BaseRoute()).Methods("GET")
    r.HandleFunc("/healthz", c.HealthzRoute()).Methods("GET")

    srv := &http.Server{
        Handler:      r,
        Addr:         "127.0.0.1:8080",
        WriteTimeout: 15 * time.Second,
        ReadTimeout:  15 * time.Second,
    }

    log.Fatal(srv.ListenAndServe())
}

and my project is basically set out like this

enter image description here

I am using go version go1.11.4 darwin/amd64 and my project is using go modules.

I have tried to create a multi stage Dockerfile as below

# build stage
FROM golang:alpine AS build-env
ADD . /src
RUN apk update && apk upgrade && apk add --no-cache git
RUN cd /src && go build -o goapp

# final stage
FROM alpine
WORKDIR /app
COPY --from=build-env /src/goapp /app/
EXPOSE 8080
ENTRYPOINT ./goapp

I run docker build . -t my-app in the root of the project and this completes with

Successfully built ebb41cfbef59
Successfully tagged my-app:latest

However when I run docker run -p 8080:8080 my-app there is no output from the terminal and a curl request on http://localhost:8080/ returns no response. This is also the same for a browser request.

I am very new to Docker and confused as to why this build is not working.

I know that my container is running my app as I added

fmt.Println("Running")

and this was printed to the console.

Upvotes: 0

Views: 970

Answers (1)

nodediggity
nodediggity

Reputation: 2488

I think you need to remove the host portion from your Addr property.

srv := &http.Server{
    Handler:      r,
    Addr:         ":8080",
    WriteTimeout: 15 * time.Second,
    ReadTimeout:  15 * time.Second,
}

Upvotes: 5

Related Questions