Method and system for reducing mirror image volume of container
By selecting lightweight basic mirroring, multi-stage construction, streamlining dependency and cleaning caches, the problem of excessive container image size is solved, system performance and efficiency are improved, and cost is reduced, and it is suitable for modern cloud-native and large-scale distributed systems.
Patent Information
- Application Number
- CN202510473599.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-16
- Publication Date
- 2025-07-22
AI Technical Summary
The large container image size leads to too long delivery time, which affects the efficiency of development, operation and maintenance, storage, network bandwidth and computing resources. It is difficult to achieve performance improvement and cost reduction in large-scale distributed applications and cloud-native environments.
We use lightweight basic mirroring, multi-stage construction, deletion of unnecessary files, streamlining dependencies and cleaning the internal cache of the mirror, and reduce the image volume by selecting smaller basic mirroring, merging commands, compressing stateless service packages and deleting temporary files.
Optimize the mirror delivery time, reduce unnecessary content, improve system performance and efficiency, reduce network bandwidth and storage space consumption, and enhance security and portability.
Smart Images

Figure CN120353540A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software development products, and more particularly to a method and system for reducing the volume of container images. Background Art
[0002] With the development of containerization technology, reducing the volume of container images is crucial in modern cloud-native and containerized applications, especially with the popularization of microservices architecture and large-scale distributed systems. The size of the container image directly affects the efficiency of development, operation and maintenance, storage, network bandwidth, and container operation. Reducing the volume of container images can not only improve the deployment and startup speed of containerized applications, but also significantly reduce the consumption of network bandwidth, storage space, and computing resources, while enhancing security, improving portability, and reducing the complexity of operation and maintenance management. Especially in large-scale distributed applications and cloud-native environments, optimizing the image volume is of great significance for improving application performance, reducing costs, and enhancing the elasticity and reliability of the overall system. Summary of the Invention
[0003] The technical task of the present invention is to address the above deficiencies by providing a method and system for reducing the volume of container images, which can optimize the problem of excessive transfer time caused by too large image volume during the transfer process, reduce unnecessary content in the image, and improve system performance.
[0004] The technical solution adopted by the present invention to solve its technical problems is as follows:
[0005] A method for reducing the volume of container images, when constructing an image, the reduction of the image volume is achieved according to the following steps:
[0006] 1) Select a lightweight base image;
[0007] 2) Use multi-stage builds technology to build the application in one stage and then only copy the build products to the final image, thereby reducing unnecessary build dependencies and tool files;
[0008] 3) After each command executed in the image, ensure that all temporary files or no-longer-needed dependencies are deleted;
[0009] 4) For the text files used to build the container image, remove the redundant dependency packages. The more dependency packages are installed, the larger the image volume. Only install the minimum dependencies required by the application; for example, if the application only depends on a specific part of a certain library, only install the necessary components instead of the entire library;
[0010] 5) Combine the commands in the text file for creating and starting a new container. Each layer increases the image size. Therefore, if possible, combine multiple commands into one command. This can reduce the number of intermediate layers and thus decrease the image size.
[0011] 6) Many package managers (such as apt and yum) retain cache files when installing software packages. Cache files can be deleted in the text file for building the container image to reduce the image size.
[0012] This method reduces the image size by using technical means such as using a smaller base image, multi-stage build, deleting unnecessary files, streamlining dependencies, and cleaning the internal cache of the image.
[0013] Furthermore, this method includes:
[0014] At least one base image containing a Unix file system;
[0015] At least one text file for building the container image with complete syntax and capable of normal compilation, which contains all the instructions for building the container image;
[0016] Compress the deployment package of the stateless service;
[0017] Try not to install tool-related commands (such as vim, curl, ping, etc.) in the base image; only install the tools required to start the service.
[0018] Furthermore, the size of the base image is less than 100MB.
[0019] Furthermore, the content of the text file for building the container image does not exceed 10 lines.
[0020] Furthermore, the compression of the deployment package of the stateless service does not exceed 100MB, and the image after building remains within 300MB.
[0021] Furthermore, the tools required to start the service are one of go, java, and python.
[0022] Furthermore, the lightweight base images include Alpine Linux, Debian Slim, Busybox, and UBI.
[0023] Furthermore, the temporary files or no-longer-needed dependencies include: intermediate files, cache files, logs, etc. generated during compilation.
[0024] The present invention also claims to protect a system for reducing the size of a container image, including:
[0025] At least one base image containing a Unix file system;
[0026] At least one text file for building a container image that is grammatically complete and can be compiled normally; the text file contains all the instructions for building a container image;
[0027] Compress the deployment package of the stateless service;
[0028] Try not to install tool-related commands in the base image, such as vim, curl, ping, etc.; only install the tools required to start the service, such as one of go, java, and python;
[0029] The system reduces the size of container images through the above method.
[0030] The present invention also claims a device for reducing the size of a container image, comprising: at least one memory and at least one processor;
[0031] The at least one memory is used to store a machine-readable program;
[0032] The at least one processor is used to call the machine-readable program to implement the above method.
[0033] Compared with the prior art, the method and system for reducing the size of container images of the present invention have the following advantages:
[0034] Beneficial effects:
[0035] This method reduces the size of the image by using smaller base images, multi-stage builds, deleting unnecessary files, streamlining dependencies, cleaning up the internal cache of the image, etc. It can optimize the problem of long delivery time caused by large image size during the delivery process, reduce unnecessary content in the image, and improve system performance. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] Figure 1 The figure is a flowchart of a method for reducing the size of a container image provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0037] The present invention will be further described below in conjunction with the accompanying drawings and specific embodiments.
[0038] A method for reducing the size of container images, which reduces the size of images by using smaller base images, multi-stage builds, deleting unnecessary files, streamlining dependencies, cleaning up image internal caches, and other technical means. The method includes:
[0039] At least one base image containing a Unix file system, with a base image size of less than 100MB;
[0040] At least one syntactically complete and normally compilable text file for building a container image, which contains all the instructions for building the container image; the content of the text file should be as concise as possible, preferably not exceeding 10 lines;
[0041] Compress the deployment package of the stateless service as much as possible; preferably not exceeding 100MB, and the image after building should be kept within 300MB as much as possible;
[0042] Try not to install tool-related commands in the base image, such as vim, curl, ping, etc.; only install the tools required to start the service, such as one of go, java, python.
[0043] When building the image, reduce the image size according to the following steps:
[0044] 1. Select a lightweight base image, such as Alpine Linux, Debian Slim, Busybox, UBI.
[0045] 2. Use multi-stage builds technology, which can build the application in one stage and then only copy the build artifacts to the final image, thus reducing unnecessary build dependencies and tool files.
[0046] 3. After each command run in the image, ensure that all temporary files or no-longer-needed dependencies are deleted. For example, intermediate files, cache files, logs, etc. generated during compilation.
[0047] 4. For the text file for building the container image, remove the redundant dependency packages. The more dependency packages are installed, the larger the image size. Only install the minimum dependencies required by the application; for example, if the application only depends on a specific part of a library, only install the necessary components instead of the entire library.
[0048] 5. Merge the commands in the text file for creating and starting a new container. Each layer will increase the image size. Therefore, if possible, merge multiple commands into one command; this can reduce the number of intermediate layers and thus reduce the image size.
[0049] 6. Many package managers (such as apt and yum) will retain cache files when installing software packages; cache files can be deleted in the text file for building the container image to reduce the image size.
[0050] Taking a Docker application as an example, when building an image based on a Dockerfile, the following steps are used to reduce the image size. (A Dockerfile is a text file used to build an image, and its text content contains instructions and explanations for building the image.)
[0051] Select a lightweight base image, such as Alpine Linux, Debian Slim, Busybox, UBI.
[0052] Use multi-stage builds technology. By building the application in one stage and then only copying the build artifacts to the final image, unnecessary build dependencies and tool files can be reduced.
[0053] After each command run in the image, ensure that all temporary files or dependencies that are no longer needed are deleted.
[0054] Remove redundant dependency packages from the Dockerfile. The more dependency packages installed, the larger the image size. Only install the minimum dependencies required for the application.
[0055] Merge the RUN commands in the Dockerfile. Each layer increases the image size. Therefore, if possible, merge multiple RUN commands into one command. This can reduce the number of intermediate layers and thus reduce the image size.
[0056] Many package managers (such as apt and yum) will retain cache files when installing software packages. These cache files can be deleted in the Dockerfile to reduce the image size.
[0057] The embodiment of the present invention also provides a system for reducing the size of a container image, including:
[0058] At least one base image containing a Unix file system, with the base image size less than 100MB;
[0059] At least one text file for building a container image that is syntactically complete and can be compiled normally; this text file contains all the instructions for building the container image; the content of this text file is as concise as possible, preferably not exceeding 10 lines;
[0060] Compress the deployment package of the stateless service; preferably not exceeding 100MB, and the image after building should be kept within 300MB as much as possible;
[0061] Try not to install tool-related commands in the base image, such as vim, curl, ping, etc.; only install the tools required to start the service, such as one of go, java, python;
[0062] The system reduces the volume of the container image by the method of reducing the volume of the container image described in the above embodiments.
[0063] When building the image, the following steps are taken to reduce the image volume:
[0064] 1. Select a lightweight base image, such as Alpine Linux, Debian Slim, Busybox, UBI.
[0065] 2. Use multi-stage builds technology. By building the application in one stage and then only copying the build artifacts to the final image, unnecessary build dependencies and tool files can be reduced.
[0066] 3. After each command run in the image, ensure that all temporary files or no-longer-needed dependencies are deleted. For example, intermediate files, cache files, logs, etc. generated during compilation.
[0067] 4. For the text file used to build the container image, remove the redundant dependency packages. The more dependency packages are installed, the larger the image volume. Only install the minimum dependencies required by the application. For example, if the application only depends on a specific part of a library, only the necessary components can be installed instead of the entire library.
[0068] 5. Merge the commands in the text file for creating and starting a new container. Each layer will increase the image volume. Therefore, if possible, merge multiple commands into one command; this can reduce the number of intermediate layers and thus reduce the image volume.
[0069] 6. Many package managers (such as apt and yum) will retain cache files when installing software packages; the cache files can be deleted in the text file for building the container image to reduce the image volume.
[0070] An embodiment of the present invention also provides a device for reducing the volume of a container image, including: at least one memory and at least one processor;
[0071] The at least one memory is used to store machine-readable programs;
[0072] The at least one processor is used to call the machine-readable program to implement the method of reducing the volume of the container image described in the above embodiments.
[0073] Through the above specific embodiments, those skilled in the art can easily implement the present invention. However, it should be understood that the present invention is not limited to the above specific embodiments. Based on the disclosed embodiments, those skilled in the art can arbitrarily combine different technical features to achieve different technical solutions.
[0074] Except for the technical features described in the specification, all other technical features are well-known to those skilled in the art.
Claims
1. A method for reducing the volume of a container image, characterized in that, When building an image, reduce the image size according to the following steps: 1) Select a lightweight base image; 2) Use multi-stage build technology by building the application in one stage and then copying only the build artifacts to the final image; 3) After each command run in the image, ensure that all temporary files or dependencies that are no longer needed are deleted; 4) For the text file used to build the container image, remove the redundant dependency packages and install only the minimum dependencies required for the application; 5) Combine the commands in the text file for creating and starting a new container, merging multiple commands into one command; to reduce the number of intermediate layers and thus reduce the image size; 6) Delete the cache files retained by the package manager when installing packages in the text file for building the container image to reduce the image size.
2. The method for reducing the volume of a container image according to claim 1, wherein, The method includes: At least one base image containing a Unix file system; At least one text file for building a container image with complete syntax and capable of normal compilation, which contains all instructions for building the container image; Compress the deployment package of the stateless service; Only install the tools required to start the service in the base image.
3. The method for reducing the volume of a container image according to claim 2, wherein The size of the base image is less than 100 MB.
4. A method for reducing the volume of a container image according to claim 2, characterized in that, The content of the text file for building the container image does not exceed 10 lines.
5. A method for reducing the volume of a container image according to claim 2, characterized in that, The compression of the deployment package of the stateless service does not exceed 100 MB, and the image after building remains within 300 MB.
6. A method for reducing the volume of a container image according to claim 2, characterized in that, The tools required to start the service are one of go, java, and python.
7. A method for reducing the volume of a container image according to claim 1, characterized in that The lightweight base images include Alpine Linux, Debian Slim, Busybox, and UBI.
8. A method for reducing the volume of a container image according to claim 1, characterized in that, The temporary files or dependencies that are no longer needed include: intermediate files, cache files, and logs generated during compilation.
9. A system for reducing the volume of a container image, characterized in that, It includes: At least one base image containing a Unix file system; At least one text file for building a container image with complete syntax and capable of normal compilation; This text file contains all instructions for building the container image; Compress the deployment package of the stateless service; Only install the tools required to start the service in the base image; This system reduces the container image size by the method described in any one of claims 1 to 8.
10. A device for reducing the volume of a container image, characterized in that, It includes: At least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is used to call the machine-readable program to implement the method described in any one of claims 1 to 8.