Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use golang and docker together

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use golang and docker together, I believe many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Docker is based on Golang development and does not need to be explained, but Golang is inherently suitable for running in a docker container, but this is not the reason, thanks to the static compilation of Golang, when cgo is turned off at compile time, it can be completely independent of the system environment.

Test code

Package mainimport ("fmt"io/ioutil"net/http"os") func main () {resp, err: = http.Get ("https://www.baidu.com") check (err) body" Err: = ioutil.ReadAll (resp.Body) check (err) fmt.Println (len (body))} func check (err error) {if err! = nil {fmt.Println (err) os.Exit (1)}}

Write dockerfile

Based on scratch

Scratch is a special image, it is a virtual image, that is, a blank image; using the static compilation of Golang is independent, the compilation time and image size can be greatly reduced.

CGO_ENABLED=0 GOOS=linux go build-a-installsuffix cgo-o main.

GOOS=linux is to set the target of cross-compilation to Linux, so that you won't have any problems under Mac or Win. -installsuffix cgo is used to import net in static compilation

Dockerfile

FROM scratchRUN mkdir / appADD main / app/WORKDIR / appCMD ["/ app/main"] docker build-t golang-scratch-app.

Scratch has a problem, that is, the time zone, because scratch is an empty mirror and cannot be executed.

RUN cp / usr/share/zoneinfo/Asia/Shanghai / etc/localtime

Although the image packaged by scratch is minimized, its use is limited by time zone problems, so it is generally not recommended.

Based on alpine

Using alpine image, the image is also very small, about 4.4m. It also provides an apk package management tool, which is very suitable for making some customized basic images, so you can build relevant images of time zones that meet your needs. The dockerfile file is as follows:

FROM alpineRUN apk-- no-cache add tzdata ca-certificates & &\ ln-sf / usr/share/zoneinfo/Asia/Shanghai / etc/localtime & &\ echo "Asia/Shanghai" > / etc/timezone RUN mkdir / appADD main / app/WORKDIR / appCMD ["/ app/main"] after reading the above, have you mastered how to use golang and docker together? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report