Python project mirror image construction method and device, equipment and storage medium

Through multi-stage construction strategy and compatibility verification, the problems of large image size, security vulnerabilities and long construction time in the traditional single-stage construction method are solved, and smaller, safer and more efficient Python project mirroring is achieved, improving the Build efficiency and stability.

CN119201144APending Publication Date: 2024-12-27SHANGHAI DONGPU INFORMATION TECH CO LTD
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202411227741.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-03
Publication Date
2024-12-27

AI Technical Summary

Technical Problem

When building container images of Python projects, the traditional single-stage construction method has large images, security vulnerabilities and long construction time, which affects storage, transmission efficiency and system security, especially in the continuous integration/continuous deployment (CI/CD) process. Medium inefficiency.

Method used

Using a multi-stage construction strategy, use the first lightweight basic image as the build stage foundation image, install the basic dependencies and build tools, copy the dependency files and code to generate dependency layers and build products, and then copy it to the second lightweight In the basic image, delete non-run files and perform compatibility verification to ensure the stability and security of the image.

Benefits of technology

It effectively reduces the image size, improves the download, loading and running speed, improves the construction efficiency, ensures the compatibility and security of the mirror components, provides a correction mechanism for verification failures, and improves the stability and reliability of the project.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119201144A_ABST
    Figure CN119201144A_ABST
Patent Text Reader

Abstract

The invention relates to the field of containers, and discloses a Python project mirror image construction method, device and equipment and a storage medium, and the method is used for constructing a Python project mirror image by adopting a multi-stage construction strategy so as to reduce the size of the mirror image. The method comprises the following steps: acquiring a first lightweight basic mirror image as a construction stage basic mirror image, and installing a basic dependency item and a construction tool; copying a dependency file in the Python project into the basic mirror image in the construction stage, and generating a dependency layer in combination with a construction tool; copying a code in the Python project into the basic mirror image in the construction stage, and executing a construction step in the code to obtain a construction product; obtaining a second lightweight basic mirror image as a running environment basic mirror image, and copying the dependency layer and the construction product into the running environment basic mirror image to obtain an initial mirror image; and performing compatibility verification on the initial mirror image, and using the initial mirror image passing the verification as a Python item mirror image.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of container technology, and in particular to a Python project image building method, device, equipment and storage medium. Background Art

[0002] With the increasing popularity of microservice architecture, enterprises have begun to split large applications into a series of small, independent services, each of which runs in its own process and interacts through lightweight communication mechanisms (such as HTTP REST API). This architectural pattern greatly improves the scalability, maintainability, and flexibility of applications. In order to support this architectural pattern, container technology, especially container platforms such as Docker, has been widely used.

[0003] In a microservice architecture, Python projects are part of the service, and their containerized deployment becomes particularly important. Containerization allows developers to package applications and all their dependencies into an independent, portable image that can run in any environment that supports Docker, thus achieving the goal of "build once, run everywhere". However, the traditional single-stage build method faces several significant challenges when building container images for Python projects:

[0004] Large image size: Single-stage builds usually package build tools (such as Python interpreter, pip, etc.), project dependencies (installed through files such as requirements.txt), and project code into the same image. This results in a large image size, which not only increases storage and transmission costs, but may also affect the startup speed and operating efficiency of the container.

[0005] Many security vulnerabilities: Since the image contains all the tools and dependencies used in the build process, this increases potential security risks. For example, if a dependency library has a known security vulnerability and the developer does not update it in time, then this vulnerability may be brought into the production environment, posing a threat to system security.

[0006] Long build time: Single-stage builds require reinstalling all dependencies every time you build, which is usually a time-consuming process. Especially in the continuous integration / continuous deployment (CI / CD) process, frequent builds and deployments will further aggravate this problem, affecting development efficiency and delivery speed.

[0007] Therefore, the existing technology still needs to be improved and developed. Summary of the invention

[0008] The present invention provides a Python project image construction method, device, equipment and storage medium, which are used to construct a Python project image using a multi-stage construction strategy to reduce the image size.

[0009] A first aspect of the present invention provides a Python project image construction method, which comprises: obtaining a first lightweight base image as a construction stage base image, and installing basic dependencies and construction tools in the construction stage base image; copying dependency files in a Python project to the construction stage base image, and generating a dependency layer in combination with the construction tool; copying code in the Python project to the construction stage base image, and executing the construction steps in the code to obtain a construction product; obtaining a second lightweight base image as an operating environment base image, copying the dependency layer and the construction product to the operating environment base image to obtain an initial image; performing a compatibility check on the initial image, and if the check passes, using the initial image as the Python project image, and if the check fails, obtaining a corrected and re-verified initial image, and using the corrected and re-verified initial image as the Python project image.

[0010] Optionally, in a first implementation method of the first aspect of the present invention, obtaining a first lightweight base image as a build stage base image and installing basic dependencies and build tools in the build stage base image includes: obtaining configuration requirements, and determining the version of the first lightweight base image, basic dependencies, and build tools based on the configuration requirements; obtaining a first lightweight base image of a corresponding version from an official image library based on the determined version as the build stage base image; and installing the basic dependencies and the build tools in the file system layer created by the build stage base image.

[0011] Optionally, in a second implementation of the first aspect of the present invention, the copying of dependency files in the Python project to the build stage base image and generating a dependency layer in combination with the build tool includes: copying the dependency files in the Python project from a local project directory to a working directory of the build stage base image; generating dependency installation instructions based on the dependency files; and generating a dependency layer based on the dependency installation instructions in combination with the build tool.

[0012] Optionally, in a third implementation method of the first aspect of the present invention, copying the code in the Python project to the base image in the construction stage, and executing the construction steps in the code to obtain the construction product, includes: copying the code in the Python project from the local project directory to the working directory of the base image in the construction stage; generating several shell scripts based on the construction steps in the code; and executing the shell scripts in the Dockerfile to obtain the construction product.

[0013] Optionally, in a fourth implementation method of the first aspect of the present invention, the second lightweight base image is obtained as the operating environment base image, and the dependent layer and the build product are copied into the operating environment base image to obtain the initial image, including: obtaining configuration requirements, and determining the version of the second lightweight base image based on the configuration requirements; based on the determined version, obtaining the second lightweight base image of the corresponding version from the official image library as the operating environment base image; copying the dependent layer and the build product into the operating environment base image, and deleting cache files, temporary files, and files not required for operation to obtain the initial image.

[0014] Optionally, in a fifth implementation method of the first aspect of the present invention, the dependent layer and the build product are copied to the operating environment base image, and cache files, temporary files, and files not required for operation are deleted to obtain an initial image, including: copying the dependent layer and the build product to the operating environment base image; using the RUN instruction in the Dockerfile in combination with a shell command to delete cache files and temporary files; calling a preset deletion script, using the deletion script to traverse the files and directories in the operating environment base image, deleting files not required for operation, and obtaining an initial image.

[0015] Optionally, in a sixth implementation of the first aspect of the present invention, the compatibility check is performed on the initial image, and if the check passes, the initial image is used as the Python project image; if the check fails, the initial image that has been corrected and re-verified is obtained, and the corrected and re-verified initial image is used as the Python project image, including: obtaining preset compatibility check indicators, the compatibility check indicators include Python version, dependency library compatibility and operating system compatibility; performing a compatibility check on the initial image based on the compatibility check indicators to obtain a check result; if the check result is that the check passes, the initial image is used as the Python project image; if the check result is that the check fails, the initial image that has been corrected and re-verified is obtained, and the corrected and re-verified initial image is used as the Python project image.

[0016] The second aspect of the present invention provides a Python project image construction device, comprising: an installation module, used to obtain a first lightweight base image as a construction stage base image, and install basic dependencies and construction tools in the construction stage base image; a first construction module, used to copy the dependency files in the Python project to the construction stage base image, and generate a dependency layer in combination with the construction tool; a second construction module, used to copy the code in the Python project to the construction stage base image, and execute the construction steps in the code to obtain a construction product; a third construction module, used to obtain the second lightweight base image as a running environment base image, copy the dependency layer and the construction product to the running environment base image to obtain an initial image; a verification module, used to perform compatibility verification on the initial image, if the verification passes, use the initial image as the Python project image, if the verification fails, obtain the initial image that has been corrected and re-verified, and use the initial image that has been corrected and re-verified as the Python project image.

[0017] Optionally, in a first implementation method of the second aspect of the present invention, the installation module includes: a first determination unit, used to obtain configuration requirements, and determine the version, basic dependencies and build tools of the first lightweight base image based on the configuration requirements; a first acquisition unit, used to obtain the first lightweight base image of the corresponding version from the official image library based on the determined version as the build stage base image; an installation unit, used to install the basic dependencies and the build tools in the file system layer created by the build stage base image.

[0018] Optionally, in a second implementation of the second aspect of the present invention, the first construction module includes: a first copying unit, used to copy dependency files in the Python project from a local project directory to the working directory of the base image in the construction phase; a first generation unit, used to generate dependency installation instructions based on the dependency files; and a first construction unit, used to generate a dependency layer based on the dependency installation instructions in combination with the construction tool.

[0019] Optionally, in a third implementation of the second aspect of the present invention, the second construction module includes: a second copying unit, used to copy the code in the Python project from the local project directory to the working directory of the base image in the construction phase; a second generation unit, used to generate a number of shell scripts based on the construction steps in the code; and a second construction unit, used to execute the shell script in the Dockerfile to obtain a construction product.

[0020] Optionally, in a fourth implementation of the second aspect of the present invention, the third construction module includes: a second determination unit, which obtains configuration requirements and determines the version of the second lightweight base image based on the configuration requirements; a second acquisition unit, which is used to obtain the second lightweight base image of the corresponding version from the official image library based on the determined version as the operating environment base image; a third construction unit, which is used to copy the dependent layer and the construction product to the operating environment base image, and delete cache files, temporary files, and files not required for operation to obtain an initial image.

[0021] Optionally, in a fifth implementation of the second aspect of the present invention, the verification module includes: a third acquisition unit, used to obtain preset compatibility verification indicators, the compatibility verification indicators including Python version, dependency library compatibility and operating system compatibility; a verification unit, used to perform compatibility verification on the initial image based on the compatibility verification indicators to obtain a verification result; a processing unit, used to use the initial image as the Python project image if the verification result is a passed verification, and if the verification result is a failed verification, obtain the initial image that has been corrected and re-verified, and use the initial image that has been corrected and re-verified as the Python project image.

[0022] A third aspect of the present invention provides a Python project image building device, comprising: a memory and at least one processor, wherein the memory stores computer-readable instructions, and the memory and the at least one processor are interconnected through a line; the at least one processor calls the computer-readable instructions in the memory so that the Python project image building device executes the various steps of the Python project image building method as described above.

[0023] A fourth aspect of the present invention provides a computer-readable storage medium, in which computer-readable storage medium is stored computer-readable instructions, which, when executed on a computer, enable the computer to execute the various steps of the Python project image building method described above.

[0024] In the technical solution provided by the present invention, a multi-stage construction strategy is adopted, and a first lightweight base image and a second lightweight base image are used as the basis of the construction stage and the operating environment, which effectively reduces the size of the image, thereby speeding up the download, loading and running speed of the image. Moreover, by separating the construction stage and the operating environment, the construction process is more focused on generating necessary dependency layers and construction products without including additional components that are not required at runtime, thereby improving the construction efficiency. At the same time, the dependency layer, the construction product and the operating environment are processed separately, so that each part can be modified and reused independently. In addition, by performing strict compatibility verification on the initial image, it is ensured that the components in the image match the project requirements, reducing operating errors and performance problems caused by compatibility issues, and for images that fail verification, a correction and re-verification mechanism is provided to ensure that the image finally used has passed strict verification, thereby improving the stability and reliability of the project. BRIEF DESCRIPTION OF THE DRAWINGS

[0025] Figure 1 A first flow chart of the Python project image building method provided by an embodiment of the present invention;

[0026] Figure 2 A second flow chart of the Python project image building method provided by an embodiment of the present invention;

[0027] Figure 3 A third flow chart of the Python project image building method provided by an embodiment of the present invention;

[0028] Figure 4 A fourth flow chart of the Python project image building method provided by an embodiment of the present invention;

[0029] Figure 5 A fifth flow chart of the Python project image building method provided by an embodiment of the present invention;

[0030] Figure 6 A sixth flow chart of the Python project image building method provided by an embodiment of the present invention;

[0031] Figure 7 A schematic diagram of the structure of a Python project image building device provided by an embodiment of the present invention;

[0032] Figure 8 A schematic diagram of the structure of a Python project image building device provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0033] The embodiment of the present invention provides a Python project image construction method, device, equipment and storage medium, which is used to construct a Python project image using a multi-stage construction strategy to reduce the image size. The method includes: the Python project image construction method includes: obtaining a first lightweight base image as a construction stage base image, and installing basic dependencies and construction tools in the construction stage base image; copying the dependency files in the Python project to the construction stage base image, and generating a dependency layer in combination with the construction tool; copying the code in the Python project to the construction stage base image, and executing the construction steps in the code to obtain a construction product; obtaining a second lightweight base image as an operating environment base image, copying the dependency layer and the construction product to the operating environment base image, and obtaining an initial image; performing a compatibility check on the initial image, if the check passes, using the initial image as the Python project image, if the check fails, obtaining the initial image that has been corrected and re-verified, and using the initial image that has been corrected and re-verified as the Python project image.

[0034] The terms "first", "second", "third", "fourth", etc. (if any) in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms "including" or "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0035] For ease of understanding, the specific process of the embodiment of the present invention is described below. Figure 1 A first embodiment of a Python project image building method in an embodiment of the present invention includes:

[0036] S101. Obtain a first lightweight base image as a base image in the build phase, and install basic dependencies and build tools in the base image in the build phase.

[0037] It is understandable that the execution subject of the present invention may be a Python project image building device, or a terminal or a server, which is not specifically limited here. The embodiment of the present invention is described by taking a server as the execution subject as an example.

[0038] In this embodiment, lightweight basic images in Docker refer to those images that are small in size, low in resource usage, and can meet basic operating requirements, such as Alpine, Debian Slim, Ubuntu Slim, Scratch, and Distroless.

[0039] S102. Copy the dependency files in the Python project to the base image in the build phase, and generate a dependency layer in combination with the build tool.

[0040] In this embodiment, the COPY instruction is used to copy the requirements.txt or similar dependency files in the Python project to the base image in the build phase. Then, the pip install command is used to install these dependencies, which will be installed in a specific layer of the base image in the build phase, that is, a Docker layer containing all project dependencies is generated.

[0041] S1013. Copy the code in the Python project to the base image in the build phase, and execute the build steps in the code to obtain the build product.

[0042] In this embodiment, the COPY instruction is used to copy the source code in the Python project to the base image in the build phase. Then, the project-specific build steps (such as compiling Cython code, preprocessing data, etc.) are performed as needed to obtain the build product (such as an executable file, a packaged library, etc.).

[0043] S104: Obtain a second lightweight base image as the runtime environment base image, copy the dependency layer and the build product to the runtime environment base image, and obtain an initial image.

[0044] In this embodiment, a new lightweight base image is selected as the runtime environment base image, and then the COPY--from=<build stage image name> instruction is used to copy the dependency layers and build products from the build stage base image to the runtime environment base image, thereby obtaining an initial image that contains only the minimum set (dependencies and build products) required to run the project.

[0045] In this embodiment, the second lightweight base image and the first lightweight base image may be the same or different.

[0046] S105, performing compatibility check on the initial image, if the check passes, using the initial image as the Python project image, if the check fails, obtaining the corrected and re-verified initial image, and using the corrected and re-verified initial image as the Python project image.

[0047] In this embodiment, if the initial image passes the compatibility check, the image can be directly used as the Python project image.

[0048] If the initial image fails the compatibility check, the image is modified based on the check result. The modification includes updating the Python version or dependent library version, adding or removing necessary dependencies, modifying the Dockerfile to change the build process, and other operations.

[0049] After the correction, rebuild the image and perform compatibility check again until the check passes, obtain the corrected and re-checked initial image, and use the corrected and re-checked initial image as the Python project image.

[0050] In this embodiment, metadata can also be added indirectly by tagging the image. For example, the command docker build-t my-python-project:1.0.0 can be used to build and tag the initial image, where 1.0.0 is the metadata about the initial image version. By tagging the image, different versions of the image can be easily managed and easily rolled back to the previous version when needed. In addition, the label also provides additional contextual information about the image content, which helps communication and collaboration between team members.

[0051] The present embodiment provides a Python project image building method, which adopts a multi-stage building strategy and uses a first lightweight base image and a second lightweight base image as the basis for the building stage and the operating environment, effectively reducing the size of the image, thereby speeding up the download, loading and running speed of the image. Moreover, by separating the building stage and the operating environment, the building process is more focused on generating necessary dependency layers and build products without including additional components that are not required at runtime, thereby improving the building efficiency. At the same time, the dependency layer, the build product and the operating environment are processed separately, so that each part can be modified and reused independently. In addition, by performing strict compatibility verification on the initial image, it is ensured that the components in the image match the project requirements, reducing operating errors and performance problems caused by compatibility issues, and providing a correction and re-verification mechanism for images that fail verification, ensuring that the image finally used is strictly verified, thereby improving the stability and reliability of the project.

[0052] See also Figure 2 , the second embodiment of the Python project image building method in the embodiment of the present invention includes:

[0053] S201. Obtain configuration requirements, and determine a version, basic dependencies, and build tools of a first lightweight base image based on the configuration requirements.

[0054] In this embodiment, the configuration requirements include the required Python version, operating system type, basic dependencies (such as libraries, frameworks, etc.), and build tools (such as pip, make, gcc, etc.).

[0055] In this embodiment, according to the pre-set configuration requirement and the first lightweight base image version association relationship table, after the configuration requirement is determined, the configuration requirement and the first lightweight base image version association relationship table can be queried to determine the version of the first lightweight base image.

[0056] Similarly, the configuration requirements and basic dependency association table and the configuration requirements and construction tool association table are set in advance. After the configuration requirements are determined, the configuration requirements and basic dependency association table and the configuration requirements and construction tool association table can be queried to determine the basic dependencies and construction tools.

[0057] S202: Based on the determined version, obtain a first lightweight base image of the corresponding version from the official image library as a base image for the construction phase.

[0058] Understandably, Docker Hub is an image repository service officially provided by Docker, and it is also the most widely used official image repository.

[0059] In this embodiment, the first lightweight base image is a python:3.9-slim image.

[0060] S203: Install basic dependencies and build tools in the file system layer created by the base image in the build phase.

[0061] In this embodiment, after entering the image environment of the base image in the build phase, the basic dependencies are installed using the package management tool of the operating system (such as apt), and then the build tool pip is installed, that is, Python's package management tool pip is used for installation, and pip install pip is executed to ensure that the pip tool is the latest version, and then the wheel package is installed, and the pipinstall wheel command is executed.

[0062] In this embodiment, by using a lightweight base image and a standardized build process, efficient resource utilization, accelerated build process, improved application portability and security, and clear management of dependencies and easy maintenance and updating are achieved.

[0063] See also Figure 3 A third embodiment of a Python project image building method in an embodiment of the present invention includes:

[0064] S301. Copy the dependent files in the Python project from the local project directory to the working directory of the base image in the build phase.

[0065] In this embodiment, in the Dockerfile, the COPY instruction is used to copy the dependency files in the local project directory to the specified working directory of the base image in the build phase. For example, COPY requirements.txt / app / will copy the requirements.txt file to the / app / directory of the base image in the build phase.

[0066] S302: Generate dependency installation instructions according to the dependency files.

[0067] In this embodiment, during the construction phase, inside the base image, instructions for installing dependencies are generated and executed based on the copied dependency files.

[0068] S303. Generate a dependency layer based on the dependency installation instructions and in combination with the build tool.

[0069] In this embodiment, based on the dependency installation instruction, the RUN instruction is used in combination with a build tool (such as pip) to execute the dependency installation instruction. For example, RUN pip install -r / app / requirements.txt will install the dependencies listed in the / app / requirements.txt file to generate a dependency layer.

[0070] In this embodiment, the dependency files in the Python project are copied from the local project directory to the working directory of the base image in the build phase, and dependency installation instructions are generated according to the dependency files. Based on the dependency installation instructions, the dependency layer is generated in combination with the build tool, which can ensure that the dependencies of the Python project are correctly installed in the build phase, and the result is saved as a reusable, portable and secure image layer, which helps to improve the project build efficiency, maintainability and security.

[0071] See also Figure 4 A fourth embodiment of a Python project image building method in an embodiment of the present invention includes:

[0072] S401. Copy the code in the Python project from the local project directory to the working directory of the base image in the build phase.

[0073] In this embodiment, the code in the Python project is copied from the local project directory to the working directory of the base image in the build phase, that is, the COPY instruction is used in the Dockerfile to copy the local project directory (or specific files therein) to the working directory of the base image in the build phase. For example, COPY . / src / app / src will copy the local src directory to the / app / src directory of the base image in the build phase, so as to facilitate the execution of the build step inside the base image in the build phase.

[0074] S402: Generate several shell scripts based on the construction steps in the code.

[0075] In this embodiment, the construction steps in the project (such as installing dependencies, compiling code, running tests, etc.) are abstracted into shell scripts so as to be called in the Dockerfile.

[0076] Specifically, according to the project building requirements, one or more shell scripts are generated, and the shell scripts include the commands required to execute the building steps. For example, a script named build.sh contains pip install -r requirements.txt to install Python dependencies, and python setup.py build to compile the project code.

[0077] S403. Execute the shell script in the Dockerfile to obtain the build product.

[0078] In this embodiment, a shell script is called in the Dockerfile to execute the build steps and generate build products.

[0079] Specifically, use the RUN instruction in the Dockerfile to execute the shell script. Make sure that the correct working directory has been set using the WORKDIR instruction before executing the script. For example, WORKDIR / app / src sets the working directory to / app / src, and then RUN. / build.sh executes the build.sh script. After the script is executed, the build products (such as compiled binaries, packaged libraries, etc.) will remain in the location specified in the base image during the build phase.

[0080] In this embodiment, by defining the build process through Dockerfile and shell script, it can be ensured that the same results can be obtained each time the build is carried out, thereby improving the repeatability of the build. Moreover, the Dockerfile and shell script can be easily adjusted according to project requirements to adapt to different build environments and build steps. In addition, by executing the build steps in the base image during the build phase, the consistency and isolation of the build environment can be ensured, thereby avoiding the impact of differences in the local development environment on the build results.

[0081] See also Figure 5 A fifth embodiment of a Python project image building method in an embodiment of the present invention includes:

[0082] S501: Obtain configuration requirements, and determine a version of a second lightweight base image based on the configuration requirements.

[0083] In this embodiment, the configuration requirements include the required Python version, operating system type, basic dependencies (such as libraries, frameworks, etc.), and build tools (such as pip, make, gcc, etc.).

[0084] In this embodiment, according to the preset configuration requirement and second lightweight base image version association relationship table, after determining the configuration requirement, the configuration requirement and second lightweight base image version association relationship table can be queried to determine the version of the second lightweight base image.

[0085] S502: Based on the determined version, a second lightweight base image of a corresponding version is obtained from an official image library as a running environment base image.

[0086] In this embodiment, the second lightweight base image is the python:3.9-alpine image.

[0087] In this embodiment, a corresponding version of the second lightweight base image is obtained from the official image library as the operating environment base image, specifically including using the FROM instruction in the Dockerfile to specify that the second lightweight base image is the python:3.9-alpine image. For example, FROM python:3.9-alpine will pull the image of the python:3.9-alpine version from the Docker Hub.

[0088] S503: Copy the dependent layers and the build products to the basic image of the operating environment, and delete cache files, temporary files, and files not required for operation to obtain an initial image.

[0089] In this embodiment, the dependent layer and the build product are copied to the basic image of the operating environment, and the cache files, temporary files and files not required for operation are deleted to obtain the initial image, which specifically includes: copying the dependent layer and the build product to the basic image of the operating environment; using the RUN instruction in the Dockerfile in combination with the shell command to delete the cache files and temporary files; calling the preset deletion script, using the deletion script to traverse the files and directories in the basic image of the operating environment, deleting the files not required for operation, and obtaining the initial image. The initial image contains all the dependencies and build products required to run the application, and removes unnecessary files.

[0090] In this embodiment, a lightweight operating environment base image is used, and cache files, temporary files, and files not required for operation are deleted to obtain an initial image, which can significantly reduce the size of the initial image, improve deployment efficiency, and reduce resource consumption.

[0091] See also Figure 6 A sixth embodiment of a Python project image building method in an embodiment of the present invention includes:

[0092] S601. Obtain preset compatibility test indicators, where the compatibility test indicators include Python version, dependency library compatibility, and operating system compatibility.

[0093] In this embodiment, the dependency library compatibility includes the version range of a specific library, and the operating system compatibility includes the operating system version, architecture, etc.

[0094] S602: Perform compatibility check on the initial image based on the compatibility check index to obtain a check result.

[0095] In this embodiment, a compatibility check script can be written, and when the initial image is checked for compatibility, the compatibility check script can be directly called to perform the compatibility check. The check result is usually a Boolean value, indicating whether the check is passed.

[0096] S603. If the verification result is that the verification is passed, the initial image is used as the Python project image. If the verification result is that the verification fails, the initial image that has been corrected and re-verified is obtained, and the initial image that has been corrected and re-verified is used as the Python project image.

[0097] In this embodiment, if the verification result is "verification passed", the subsequent steps are continued, and the initial image is used as the Python project image. If the verification result is "verification failed", the correction process is entered, and the initial image that has been corrected and re-verified and passed the final feedback of the correction process is obtained, and the initial image that has been corrected and re-verified and passed is used as the Python project image.

[0098] Specifically, the correction process includes modifying the Dockerfile, updating dependency files, fixing known issues in the base image, or switching to another image version that meets the compatibility requirements. After the correction is completed, re-perform the compatibility check. Repeat this step until you get an initial image that passes the check.

[0099] In this embodiment, a compatibility check is performed on the initial image. Through the compatibility check, runtime errors or performance problems caused by environmental incompatibility can be avoided, compatibility problems can be discovered and solved in a timely manner, and the subsequent maintenance workload caused by environmental problems can be reduced.

[0100] The above describes the Python project image building method in the embodiment of the present invention. The following describes the device in the embodiment of the present invention. Figure 7 , the implementation method of the Python project image building device in the embodiment of the present invention includes:

[0101] An installation module 701 is used to obtain a first lightweight base image as a base image in the construction phase, and install basic dependencies and construction tools in the base image in the construction phase;

[0102] The first construction module 702 is used to copy the dependency files in the Python project to the base image in the construction phase, and generate a dependency layer in combination with the construction tool;

[0103] The second construction module 703 is used to copy the code in the Python project to the base image in the construction phase, and execute the construction steps in the code to obtain the construction product;

[0104] The third construction module 704 is used to obtain a second lightweight base image as the operating environment base image, copy the dependent layer and the construction product into the operating environment base image, and obtain an initial image;

[0105] The verification module 705 is used to perform compatibility verification on the initial image. If the verification passes, the initial image is used as the Python project image. If the verification fails, a corrected and re-verified initial image is obtained, and the corrected and re-verified initial image is used as the Python project image.

[0106] In this embodiment, the installation module 701 includes: a first determination unit 7011, which is used to obtain configuration requirements and determine the version, basic dependencies and build tools of the first lightweight base image based on the configuration requirements; a first acquisition unit 7012, which is used to obtain the first lightweight base image of the corresponding version from the official image library based on the determined version as the build stage base image; an installation unit 7013, which is used to install the basic dependencies and the build tools in the file system layer created by the build stage base image.

[0107] In this embodiment, the first construction module 702 includes: a first copying unit 7021, which is used to copy the dependency files in the Python project from the local project directory to the working directory of the base image in the construction phase; a first generating unit 7022, which is used to generate dependency installation instructions according to the dependency files; and a first construction unit 7023, which is used to generate a dependency layer based on the dependency installation instructions in combination with the construction tool.

[0108] In this embodiment, the second construction module 703 includes: a second copying unit 7031, which is used to copy the code in the Python project from the local project directory to the working directory of the base image in the construction phase; a second generating unit 7032, which is used to generate several shell scripts based on the construction steps in the code; and a second construction unit 7033, which is used to execute the shell script in the Dockerfile to obtain the construction product.

[0109] In this embodiment, the third construction module 704 includes: a second determination unit 7041, which obtains configuration requirements and determines the version of the second lightweight base image based on the configuration requirements; a second acquisition unit 7042, which is used to obtain the corresponding version of the second lightweight base image from the official image library as the operating environment base image based on the determined version; a third construction unit 7043, which is used to copy the dependent layer and the construction product to the operating environment base image, and delete cache files, temporary files, and files not required for operation to obtain an initial image.

[0110] In this embodiment, the verification module 705 includes: a third acquisition unit 7051, which is used to obtain preset compatibility verification indicators, where the compatibility verification indicators include Python version, dependency library compatibility and operating system compatibility; a verification unit 7052, which is used to perform compatibility verification on the initial image based on the compatibility verification indicators to obtain a verification result; a processing unit 7053, which is used to use the initial image as the Python project image if the verification result is a passed verification, and if the verification result is a failed verification, obtain the initial image that has been corrected and re-verified, and use the initial image that has been corrected and re-verified as the Python project image.

[0111] In this embodiment, a multi-stage build strategy is adopted, and a first lightweight base image and a second lightweight base image are used as the basis for the build stage and the operating environment, which effectively reduces the size of the image, thereby speeding up the download, loading and running speed of the image. Moreover, by separating the build stage and the operating environment, the build process is more focused on generating necessary dependency layers and build products without including additional components that are not required at runtime, thereby improving the build efficiency. At the same time, the dependency layer, build product and operating environment are processed separately, so that each part can be modified and reused independently. In addition, by performing strict compatibility verification on the initial image, it is ensured that the components in the image match the project requirements, reducing operating errors and performance problems caused by compatibility issues, and providing a correction and re-verification mechanism for images that fail verification to ensure that the image finally used has passed strict verification, thereby improving the stability and reliability of the project.

[0112] above Figure 7 The Python project image building device in the embodiment of the present invention is described in detail from the perspective of modular functional entities. The Python project image building device in the embodiment of the present invention is described in detail from the perspective of hardware processing.

[0113] Figure 8 It is a structural diagram of a Python project image building device provided by an embodiment of the present invention. The device 800 may have relatively large differences due to different configurations or performances, and may include one or more processors (central processing units, CPU) 810 (for example, one or more processors) and a memory 820, and one or more storage media 830 (for example, one or more mass storage devices) storing application programs 833 or data 832. Among them, the memory 820 and the storage medium 830 can be short-term storage or persistent storage. The program stored in the storage medium 830 may include one or more modules (not shown), and each module may include a series of instruction operations in the device 800. Furthermore, the processor 810 can be configured to communicate with the storage medium 830 and execute a series of instruction operations in the storage medium on the device 800.

[0114] The device 800 may also include one or more power supplies 840, one or more wired or wireless network interfaces 850, one or more input and output interfaces 860, and / or one or more operating systems 831, such as Windows Serve, Mac OS X, Unix, Linux, FreeBSD, etc.

[0115] An embodiment of the present invention also provides a computer-readable storage medium, which may be a non-volatile computer-readable storage medium or a volatile computer-readable storage medium. Instructions are stored in the computer-readable storage medium. When the instructions are executed on a computer, the computer executes the steps of the Python project image building method.

[0116] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the above-described system, device, or unit can refer to the corresponding process in the aforementioned method embodiment and will not be repeated here.

[0117] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program codes.

[0118] As described above, the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A Python project image building method, characterized in that: The Python project image building method includes: Obtain a first lightweight base image as a build phase base image, and install base dependencies and build tools in the build phase base image; Copy the dependency files in the Python project to the base image in the build phase, and generate a dependency layer in combination with the build tool; Copy the code in the Python project to the base image in the construction phase, and execute the construction steps in the code to obtain the construction product; Obtaining a second lightweight base image as the operating environment base image, and copying the dependent layer and the build product into the operating environment base image to obtain an initial image; A compatibility check is performed on the initial image. If the check passes, the initial image is used as the Python project image. If the check fails, a corrected and re-verified initial image is obtained, and the corrected and re-verified initial image is used as the Python project image.

2. The Python project mirroring method according to claim 1, characterized in that: The step of obtaining the first lightweight base image as the base image in the construction phase, and installing base dependencies and construction tools in the base image in the construction phase, includes: Obtain configuration requirements, and determine a version, basic dependencies, and build tools of a first lightweight base image based on the configuration requirements; Based on the determined version, obtain the first lightweight base image of the corresponding version from the official image library as the base image in the building phase; The basic dependencies and the building tools are installed in the file system layer created by the base image in the building phase.

3. The Python project mirroring method according to claim 1, characterized in that: The step of copying the dependency files in the Python project to the base image in the construction phase and generating a dependency layer in combination with the construction tool includes: Copy the dependency files in the Python project from the local project directory to the working directory of the base image in the build phase; Generate dependency installation instructions based on dependency files; Based on the dependency installation instruction, a dependency layer is generated in combination with the build tool.

4. The Python project mirroring method according to claim 1, characterized in that: The code in the Python project is copied to the base image in the construction phase, and the construction steps in the code are executed to obtain the construction product, including: Copy the code in the Python project from the local project directory to the working directory of the base image in the build phase; Generate several shell scripts based on the build steps in the code; Execute the shell script in Dockerfile to get the build product.

5. The Python project mirroring method according to claim 1, characterized in that: The step of obtaining the second lightweight base image as the operating environment base image, and copying the dependent layer and the build product into the operating environment base image to obtain the initial image includes: Obtain configuration requirements, and determine the version of the second lightweight base image based on the configuration requirements; Based on the determined version, a second lightweight base image of the corresponding version is obtained from the official image library as the operating environment base image; The dependent layer and the construction product are copied to the operating environment basic image, and cache files, temporary files and files not required for operation are deleted to obtain an initial image.

6. The Python project mirroring method according to claim 1, characterized in that: The step of copying the dependent layer and the construction product to the operating environment base image, and deleting cache files, temporary files, and files not required for operation to obtain an initial image includes: Copying the dependent layer and the built product into the operating environment base image; Use the RUN instruction in Dockerfile combined with shell commands to delete cache files and temporary files; A preset deletion script is called, and the deletion script is used to traverse the files and directories in the basic image of the operating environment, delete the files not required for the operation, and obtain the initial image.

7. The Python project mirroring method according to claim 1, characterized in that: The compatibility check of the initial image is performed, and if the check passes, the initial image is used as the Python project image; if the check fails, a corrected and re-verified initial image is obtained, and the corrected and re-verified initial image is used as the Python project image, including: Get the preset compatibility test indicators, including Python version, dependency library compatibility, and operating system compatibility; Perform compatibility check on the initial image based on the compatibility check index to obtain a check result; If the verification result is passed, the initial image is used as the Python project image. If the verification result is failed, the corrected and re-verified initial image is obtained and used as the Python project image.

8. A Python project image building device, characterized in that: include: An installation module, used to obtain a first lightweight base image as a build phase base image, and install base dependencies and build tools in the build phase base image; A first building module is used to copy the dependency files in the Python project to the base image in the building phase, and generate a dependency layer in combination with the building tool; The second building module is used to copy the code in the Python project to the base image in the building stage, and execute the building steps in the code to obtain the building product; A third construction module is used to obtain a second lightweight base image as an operating environment base image, and copy the dependent layer and the construction product to the operating environment base image to obtain an initial image; A verification module is used to perform compatibility verification on the initial image. If the verification passes, the initial image is used as the Python project image. If the verification fails, a corrected and re-verified initial image is obtained, and the corrected and re-verified initial image is used as the Python project image.

9. A Python project image building device, characterized in that: comprising a memory and at least one processor, wherein the memory has computer-readable instructions stored therein; The at least one processor calls the computer-readable instructions in the memory to execute the various steps of the Python project image building method as described in any one of claims 1-7.

10. A computer-readable storage medium having computer-readable instructions stored thereon, characterized in that: When the computer-readable instructions are executed by a processor, the steps of the Python project image building method as described in any one of claims 1 to 7 are implemented.

Citation Information

Cited By

  • Document generation method and device, equipment, storage medium and program product

    CN120429009A

  • Container configuration reconstruction quantitative analysis method fusing file semantics and cross-stage dependence

    CN121523693A

  • Container configuration restructuring and quantification analysis method fusing file semantics and cross-stage dependencies

    CN121523693B