How to build docker image of online go project?


After the release of the go project, a netizen left a message saying that the image demonstrated in my article is to pack the project file and go into the image, so that the image will take up more space.

How to build docker image of online go project?

GoThe developed program can be compiled into binary files without installationGoIf you only put the compiled binary files directly into the image, you can save a lot of image space. My reply is that the focus of the article isKubernetesTherefore, the mirror does not occupy too much space.

It is true that the application image of real online projects is generally not built as mentioned in the previous article, because the requirements of production projects are more stringent. The process of image construction is generally used firstDockerThe container compiles the project into binary files, and then copies the compiled files into a new container image, which generally contains onlyLinuxSystem operation needs the most basic files, do not need to haveGoEnvironment, so it can reduce a lot of space. The whole process takes place in the process of image construction, which can ensure the consistency of multiple environments. The above constructionDockerThe way of mirroring is called multistage construction(multi stage build)。

Multi stage construction17.05Version of the function, so before use to determine the next useDocker EngineVersion of.

How to build docker image of online go project?

Here’s how to use itDockerMulti stage construction and production of virtual realityGoImage of the application.

The mirror image in the previous articleDockerfileIt looks like this:

FROM golang:1.14-alpine
RUN mkdir /app
COPY . /app
RUN go build -o main . 
CMD ["/app/main"]

After we build the image in a multi-stage way,DockerfileIt’s like this:

FROM golang:alpine AS build

RUN mkdir /app
COPY . /app
RUN CGO_ENABLED=0 GOOS=linux go build -o myapp

FROM scratch as final
COPY --from=build /app/myapp .
CMD ["/myapp"]

GoProject applicationDockerfileIt’s usually similar, but the details of each project may be different.FROM golang:alpineSpecifies the base image of the start phase (which contains go tools and libraries for building the program),AS buildIt’s called this stagebuild

golang:alpineSpecifiedGoBasic imagealpineedition,alpineIt’s a small container specially designed for containersLinuxDistribution. thisDockerfileUsed twice inFROMOrder, number twoFROM scratchOK, it tells meDockerStart over with a brand new, completely empty container image, and then copy the program compiled in the previous stage into it. This is what we’re going to use laterGoContainer image of the application.

scratchThe mirror image isDockerThe smallest predefined image of the project.Dockerbe used forGoIt is very common to build a program in multiple stages, and the use ofscratchMirroring saves a lot of space because we don’t really need itGoTools or anything else to run our compiled program, which may be the sameGoAn advantage in the age of containers.

usescratchMirror madeGoThere is a problem that the application image does not recognize the time zone when it is running. This is also our recent projectKubernetesThe first problem I encountered in the process of migration, but it’s OKGoogleAnd viewGoLoad time zone source code to find a solution, how to solve the specific next article to tell you.

How to build docker image of online go project?

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]