Background: Need to release go service, so I started editing dockerfile, encountered some problems, in this record, learn together, document at the same time updated to the public account, can add a concern about you, write wrong place please point out,3Q
1 says in front
The environment in this paper is based on the docker service that has been installed. Please prepare the Docker environment first
2 Docker image requirements
Let’s start by thinking about the different cloud native application runtime requirements:
Smaller size: For microservice distributed architectures, smaller size means less download bandwidth and faster distribution download speed.
Faster startup speed: For traditional monolithic applications, startup speed is not a critical metric compared to operational efficiency. The reason is that these apps restart and release relatively infrequently. However, for microservice applications that need rapid iteration and horizontal scaling, faster startup means higher delivery efficiency and faster rollback. Especially if you’re releasing an app with hundreds of copies, a slow startup is a time killer. For Serverless applications, end-to-end cold startup speed is even more critical, and even though the underlying container technology can achieve resource readiness in a hundred milliseconds, users will perceive access delays if the application cannot be started within 500ms.
Less footprint: Lower footprint at runtime means higher deployment density and lower computing costs.
(This time we will discuss how to reduce the volume)
3 Create the first version of dockerFile
In this dockerfile, I mainly do the following things
1 package
To get rid of the development environment’s influence on packaging, I chose to put the packaging process in a Dockerfile
2 start
After the first phase of packaging is complete, we can execute the packaged executable directly
Let’s take a look at the first version of DockerFile
The FROM golang MAINTAINER day south WORKDIR/go/src/git.liebaopay.com/cm/cm_ad_free COPY.. RUN go build main. Go EXPOSE was 20020 20021 20022 CMD ["./main"."./config_local.ini".": 20020".": 20021".": 20022"]
Copy the code
Let’s parse the dockerfile step by step:
The gopath of the official Golang is in /go/ SRC by default, so we can put our project directly in this # # Gopath
FROM golang
# specify the maintainer nameMAINTAINER day south# specify a working directory within the container. If no directory exists, it will be created automatically
WORKDIR /go/src/git.liebaopay.com/cm/cm_ad_free
Copy all directories and files in the current directory of dockerFile to the working directory specified in the previous step
COPY . .
# Start packing
RUN go build main.go
Expose the service port
EXPOSE 20020 20021 20022
Ini: 20020:20021:20022./config_local.ini: 20020:20021:20022
CMD ["./main"."./config_local.ini".": 20020".": 20021".": 20022"]
Copy the code
Build the image and start it
docker build -t cm_ad_free_img_v1 .
Copy the code
Create an image with the tag name cm_AD_free_IMg_v1
The image building process can be seen below
After a successful image build, we start a container service
docker run --name cm_ad_free_container_v1 -it -p 9988:20022 cm_ad_free_img_v1
Copy the code
Let’s talk about the parameter information
--name specifies the container name. -t tells Docker to assign a dummy terminal and bind it to the container's standard input. -i keeps the container's standard input open-dBackground -p is used for port mapping. Host port: container portCopy the code
Check the generated Docker image
I’m going! This image takes up a lot of space, 890Mb. What’s the reason? It turns out that when we build the image, we introduce the golang base image and do a lot of operations, and in the process, we generate a lot of temporary files, which are not needed at runtime
4 Mirror slimming
Docker provides multi-stage builds to achieve mirror weight loss.
We can divide the Dockerfile building process into two simple stages,
- The first stage is the generation of the executable, using golang’s base image and doing the Go Build operation
- The second stage is the executable stage, which uses the first stage’s executable file and base configuration without relying on the Golang base image
So our final release image contains only the files and configuration necessary to run the program, without any compile-time dependencies, thus reducing the size of the image
Let’s look at the second version of DockerFile, which only comments increment commands
# introduce golang as the base command and alias it buildThe FROM golang as build MAINTAINER day south WORKDIR/go/src/git.liebaopay.com/cm/cm_ad_free COPY.. RUN go build the main. Go# Introduce alphine minimum Linux image
FROM alpine
Set the working directory
WORKDIR /data/app
Copy the generated executable commands and some configuration files
COPY --from=build /go/src/git.liebaopay.com/cm/cm_ad_free/main .
COPY --from=build /go/src/git.liebaopay.com/cm/cm_ad_free/config_local.ini .
COPY --from=build /go/src/git.liebaopay.com/cm/cm_ad_free/logformat_local.xml .
#!!!!!! Note the lack of dynamic libraries found in Alpine by testing, and the solution after querying was to create a soft chain
RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
EXPOSE 20020 20021 20022
CMD ["./main"."./config_local.ini".": 20020".": 20021".": 20022"]
Copy the code
After building the image, check the disk usage and reduce it to 26Mb
Create the container, start the service successfully, and have fun