dpca31461 2017-11-25 14:01
浏览 152
已采纳

构建Docker映像时Go构建失败

I'm a little new to golang and I'm still trying to get my head around the difference between go run main.go and go build [-o] main.go.

I've build a little gin app to try out locally with docker and kubernetes.

package main

import (
    "net/http"

    "github.com/gin-gonic/gin"
)

func main() {

    r := gin.Default()

    r.GET("/healthz", func(c *gin.Context) {
        c.String(http.StatusOK, "")
    })

    r.GET("/readinez", func(c *gin.Context) {
        c.String(http.StatusOK, "")
    })

    r.Run() // listen and serve on 0.0.0.0:8080
} 

The app runs perfectly fine with go run main.go.

My Dockerfile:

FROM golang:latest
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN go build -o main .
CMD ["/app/main"]

It fails:

enter image description here

It is definitely in there and it also works when I go run main.go. What is the difference to build?

I'm not sure what to do here. Coming from a node background. This does drive a noobie somewhat mad... Sure there is an easy solution.

  • 写回答

3条回答 默认 最新

  • douce1368 2017-11-25 14:26
    关注

    The program succeeds on your machine because you probably have the gin package installed. You can't assume a container will have it, and should install it explicitly. Just add the following line to your dockerfile before the go build line:

    RUN go get github.com/gin-gonic/gin
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services