构建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:
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.
我对golang有点陌生,但我仍在努力弄清 我已经构建了一个杜松子酒应用程序,可以在本地使用docker和kubernetes进行尝试。 p>
应用程序运行 我的Dockerfile: p>
失败: p> \ n
它肯定在其中,当我 我不确定在这里做什么。 来自节点背景。 这确实使Noobie有点生气。当然,有一个简单的解决方案。 p>
div>运行main.go code>和
go构建[-o] main.go code>。 p>
包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 ()//收听并在0.0.0.0:8080
}上投放
code> pre>
go main.go code >。 p>
FROM golang:latest
RUN mkdir / app
ADD。 / app /
WORKDIR / app
RUN进行构建-o main。
CMD [“ / app / main”]
code> pre>
go运行main.go p> 代码>。 构建有什么区别? p>
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
It may have failed because you used gin, and the library cannot be found inside the container. Try to use glide or godep to vendoring third party library.
go get github.com/gin-gonic/gin
Then it should work.