Docker entry to actual combat (4)

Multi-stage construction of compressed image volumes

The size of the official python image used above is a full 882MB. On this basis, the third-party library used is installed, and the resources such as pictures required by the project are added. The size can easily exceed 1 G. Such a large image, the network transmission It is very inconvenient for customers, so reducing the size of the image is very necessary.

There is a python:3.8-alpine image on docker hub, the size is only 44.5MB. The reason why it is small is that alpine is an operating system using the busybox architecture, which is generally used for embedded applications. I tried to use this mirror and found that it is okay to install general libraries, but if I want to install numpy, etc., it will be difficult, and I can't even find a solution on the Internet.

Still go back to the basic route, the mainstream operating system image, the size of ubuntu is 72.9MB, and the size of centos is 209MB-this is also an important reason why I prefer to use ubuntu! Using ubuntu as the base image, the size after installing python is 139MB, and the size after installing pip suddenly rises to 407MB. If you install something else, it will easily catch up with or exceed the size of the official python image.

It seems that it is difficult to compress the size of the image file by the usual route. Fortunately, there is still a way to save the country with a curve, which is the multi-stage construction method.

The idea of ​​multi-stage construction is actually very simple. First build a large and complete image, and then only take out the useful part of the image and put it in a new image. In our scenario, pip is only needed during the process of building the image, but it is not useful at all to run our program. We only need to install pip, then use pip to install the third-party library, and then copy the third-party library from this mirror to a mirror with only python and no pip, so that the 268MB space occupied by pip can be saved.

1. Install python on the basis of the ubuntu image:

FROM ubuntu
RUN apt update \
    && apt install python3

Then run:

docker build -t python:3.8-ubuntu .

In this way, the python:3.8-ubuntu image is generated.

2. Install pip on the basis of python:3.8-ubuntu:

FROM python:3.8-ubuntu
RUN apt install pip

Then run:

docker build -t python:3.8-ubuntu-pip .

In this way, the python:3.8-ubuntu-pip image is generated.

3. Multi-stage build target image:

FROM python:3.8-ubuntu-pip
RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy
FROM python:3.8-ubuntu
COPY --from=0 /usr/local/lib/python3.8/dist-packages/ /usr/local/lib/python3.8/dist-packages/

This dockerfile needs to be explained, because it has two FROM commands.

The first one is based on the python:3.8-ubuntu-pip image and installs numpy. Of course, in practical applications, write all the third-party libraries used here.

The second FROM is based on the FROM python:3.8-ubuntu image, and all third-party libraries are copied over. The –from=0 after the COPY command means to copy from stage 0. In the actual application, copy the program code from the context, add the required ENTRYPOINT, etc.

Finally, run again:

docker build -t project:1.0 .

Of course, the image for our project is ready. About 750MB smaller than the version built using the official python mirror.

Import the image to the production environment

At this point, our image has been created, but where is the image file and how to run it in the production environment? When I used the docker images command just now, I have seen the generated image:

$ docker images                          
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello               1.0                 01fe19111dc7        59 minutes ago      893MB
python              3.8                 f5041c8ae6b1        13 days ago         884MB
ubuntu              20.04               f643c72bc252        5 weeks ago         72.9MB
hello-world         latest              bf756fb1ae65        12 months ago       13.3kB

We can use the docker save command to save the image to a specified file, and the saved file is a compressed file in .tar format:

docker save -o hello.tar hello:1.0

Copy hello.tar to the machine in the production environment, and then execute the import command:

docker load -i hello.tar

It is ready to use.

 

Guess you like

Origin blog.csdn.net/qq_28165595/article/details/131878251