dpd66100 2019-05-26 10:45
浏览 77
已采纳

容器中的Go API不返回任何响应

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.

  • 写回答

1条回答 默认 最新

  • douxiong4250 2019-05-26 10:49
    关注

    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,
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来