Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the method of containerization of Go

Shulou Source: shulou.com Published: 2022-06-01 10:51:21 09月28日 Update

The main content of this article is to explain "what is the method of Go containerization". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the method of Go containerization"?

Demonstration project

Package mainimport ("github.com/gin-gonic/gin"net/http") func main () {r: = gin.Default () r.GET ("/", func (c * gin.Context) {c.JSON (http.StatusOK, gin.H {"code": 0, "message": "success", "data": "hello world" }) r.GET ("/ hello", func (c * gin.Context) {c.JSON (http.StatusOK, gin.H {"code": 0, "message": "success", "data": "world",})}) r.GET ("/ ping") Func (c * gin.Context) {c.JSON (http.StatusOK, gin.H {"code": 0, "message": "success", "data": "pong" }) r.Run (": 8080")} DockerFileFROM registry.cn-beijing.aliyuncs.com/baxiang/golang:1.14 as buildENV GO111MODULE=offENV GOBUILDPATH=github.com/baxiang/hello-worldRUN mkdir-p / go/src/$ {GOBUILDPATH} COPY. / / go/src/$ {GOBUILDPATH} RUN cd / go/src/$ {GOBUILDPATH} & & CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go install-vFROM alpine as debugWORKDIR / hello-wordldCOPY-- from=build / go/bin/hello-world. / Hello-worldEXPOSE 8080CMD [". / hello-world"] docker-compose

The go project builds a docker image based on alpine, and always reports standard_init_linux.go:211: exec user process caused "no such file or directory" when starting the container.

Dynamic linking is not supported on miniature mirror alpine. The default go uses static links, and dynamic compilation is used by default in docker's golang environment.

If you want to use docker compilation + alpine deployment, you can do this by disabling cgo set CGO_ENABLED=0.

If you want to use cgo, you can use go build-- ldflags "--extldflags-static" to make gcc use static compilation.

At this point, I believe you have a deeper understanding of "what is the method of Go containerization". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Container method compilation content dynamic available through link mirror static learning practical deeper interested practical practical engineering easy to operate time more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS Shulou Technology Linux Microsoft