In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to create a minimum Docker Image for Go programs. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Preface
Previously introduced to you about the deployment of golang project in docker, which has a certain reference value for everyone's entry, this article will show you how to use docker to package an application written by golang, the final product is a Dockerfile file, do not underestimate this few lines of code, involving a lot of knowledge, and then we will carefully analyze it.
FROM golang:alpineADD src / go/srcRUN go install-v test ENTRYPOINT ["/ go/bin/test"] CMD ["- logtostderr"]
1. Basic image selection
The first line is to specify a base image, based on which our image is created, using the golang:alpine version here
This is a relatively small linux system, cut off a lot of tools in linux, package management tool uses apk, you can docker pull this image down to play, the default shell is sh, execute the command docker run-tmuri golang:alpine / bin/sh to enter the command line. After entering, execute env to check the environment variable, because its GOPATH is useful for the later environment deployment. You can see that the default value of the environment variable is / go.
two。 Map the code file and install
Use ADD src / go/src to map the host scr file to the / go/src directory, why does it have to be this / go/src directory? Yes, it is the path of the GOPATH environment variable above, because we need to execute the go install command later to install, otherwise we need to reset GOPATH to install the compiled binaries.
It should be noted that: at this time the local host in the src directory of the file organization, to implement go install must strictly follow the file structure of the generation package, test is the main program, glog is the use of open source log library, the entire file structure is as follows, because the main.go import package when using the "github.com/golang/glog" this path, so you need to give it a reasonable path.
. ├── Dockerfile └── src ├── github.com │ └── golang │ └── glog │ ├── glog_file.go │ ├── glog.go │ ├── glog_test.go │ ├── LICENSE │ └── README └── test └── main.go
There is also a small tips that uses the third-party open source glog for the log library of the program. When we use git for version management of our above code, we do not need to repeat the code that contains glog, but just add a reference to it. This has many advantages. When the code in the library is modified, it can be updated directly to the remote code repository, and the library can be imported automatically when clone. In other words, the specific code is pulled locally, but the reference is only saved in the remote repository.
The submodule glog can be generated with the command: git submodule add https://github.com/golang/glog.git src/github.com/golang/glog. Note: the location referenced in the git remoule command is src/github.com/golang rather than the direct src/, because after executing the command, the local code repository will clone glog the code repository, pull down its code, and just create the glog directory, so some of the previous parent directories need to be created by yourself.
For more information on commands, see Git.
After organizing the file structure, you can go install. The generated file can be executed in $GOPATH/bin, followed by the basic entry program and parameters specified. Through docker build-t = "name". Generate a mirror image
3. Go a step further: compile ahead of time
The above method is to copy the code into the base image and compile it internally. There is no doubt that golang:alpine contains a series of program running dependencies. Program running will dynamically load these libraries. We can use the ldd command to check the dependencies of the generated binaries:
Linux-vdso.so.1 = > (0x00007ffc5b1e4000) libpthread.so.0 = > / lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f50a1f13000) libc.so.6 = > / lib/x86_64-linux-gnu/libc.so.6 (0x00007f50a1b4a000) / lib64/ld-linux-x86-64.so.2 (0x00005611a4b0a000)
So here comes the question? If you compile these dependencies statically into an executable file, you don't need to save the extra information in the environment, and you can create a smaller image. Fortunately, both the compilation mechanism of golang and the basic image of docker provide such an implementation:
Use the command to generate a statically compiled binary: CGO_ENABLED=0 GOOS=linux go build-a-installsuffix cgo-o main.
At this point, using ldd to see the dependencies of the generated executable file, you can see that not a dynamic executable is displayed. Here we disable CGO to generate static binaries and set the system to linux. We set the base image to scratch, which is a very small image.
The rewritten Dockerfile is as follows:
FROM scratchADD main / ENTRYPOINT ["/ main"] CMD ["- logtostderr"]
Execute docker build-t example-scratch. Generate an image, and you can see that the size of the image is only 8.27m and can be executed normally.
In practice, the two situations are sometimes used together, first built in one image and executed in another. The following Dockerfile is taken from the Dockerfile of prometheus, a third-party monitored demonstration. You can see that it first downloads the corresponding dependency and compiles the program in the builder image, and finally executes the program in the scratch base image.
# This Dockerfile builds an image for a client_golang example.## Use as (from the root for the client_golang repository): jingx# docker build-f examples/$name/Dockerfile-t prometheus/golang-example-$name. # Builder image, where we build the example.FROM golang:1.9.0 AS builderWORKDIR / go/src/github.com/prometheus/client_golangCOPY. .WORKDIR / go/src/github.com/prometheus/client_golang/prometheusRUN go get-dWORKDIR / go/src/github.com/prometheus/client_golang/examples/simpleRUN CGO_ENABLED=0 GOOS=linux go build-a-tags netgo-ldflags'- wicked # Final image.FROM scratchLABEL maintainer "The Prometheus Authors" COPY-from=builder / go/src/github.com/prometheus/client_golang/examples/simple .EXPOSE 8080ENTRYPOINT ["/ simple"] Thank you for reading! This is the end of this article on "how to create a minimum Docker Image for Go programs". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.