A mirror image construction method, device, medium and program product
By translating Dockerfile instructions into equivalent image building operations, the inconsistency between Docker and VM/bare metal environment is solved, and the ability to create images based on Dockerfile in VM/bare metal is realized, reducing workload and improving migration flexibility.
Patent Information
- Application Number
- CN202412000227.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-31
- Publication Date
- 2025-04-29
- Estimated Expiration
- 2044-12-31
AI Technical Summary
When deploying images in virtual machines (VMs) or bare metal, due to differences in Docker and VM/bare metal file system structures, the need to write configuration and scripts separately, which increases the workload and leads to inconsistencies between Docker containers and VM/bare metal environments.
Provides a mirror construction method, which can be used to parse container configuration files (Dockerfiles) and translate them into equivalent mirror construction operations, and uses plug-ins of mirror construction tools (such as packer) to achieve support for VM/bare metal environments.
The ability to create images based on Dockerfile in VM/bare metal is realized, avoiding the need to write configurations and scripts separately for different environments, ensuring the singularity of configurations, reducing workload, and simplifying the migration from containerized environment to VM/bare metal environment.
Smart Images

Figure CN119415110B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of virtualization technology, and in particular to a mirror image construction method, device, medium and program product. Background Art
[0002] In the field of software development and software application deployment scenarios, virtualization and containerization technologies play an important role. Currently, a more common containerization platform is the application container engine (Docker). However, in some application scenarios, it is also necessary to deploy images in virtual machines (VMs) or bare metal.
[0003] Due to the differences in the file system structures of Docker and VM / bare metal, when deploying images in Docker and VM / bare metal, you need to write configurations and scripts separately. For Docker, images are generally created by writing container configuration files (Dockerfile). When building VM / bare metal images, different processes and tools are required from building Docker images. This is achieved by writing configurations and scripts separately for VM / bare metal environments, and the existing Dockerfile specifications cannot be directly used. This leads to additional workload and inconsistencies between Docker containers and VM / bare metal environments.
[0004] Therefore, technicians in this field are in urgent need of an image building method to solve the problems of inconsistency between Docker containers and VM / bare metal environments and extra workload when building images in VM / bare metal. Summary of the invention
[0005] The purpose of the present invention is to provide an image building method, device, medium and program product to solve the problems of inconsistency between Docker container and VM / bare metal environment and additional workload when building an image in VM / bare metal.
[0006] In order to solve the above technical problems, the present invention provides a mirror image construction method, comprising:
[0007] Parse the container configuration file to obtain container configuration instructions;
[0008] The container configuration instructions are sequentially translated into equivalent image building operations by a builder and a configurator; wherein the builder and the configurator are plug-ins of an image building tool; and the image building operation is an operation implemented by instructions directly supported by the image building tool and the virtual machine / bare metal;
[0009] The image building operation obtained by the translation is executed by the execution engine of the image building tool to generate a virtual machine / bare metal image.
[0010] In a possible embodiment, translating each of the container configuration instructions into an equivalent image building operation in sequence by a builder and a configurator includes:
[0011] Determining whether the container configuration instruction is a simple instruction or a complex instruction according to the instruction type of the container configuration instruction;
[0012] If the container configuration instruction is a simple instruction, the builder implements translation; wherein the builder includes an instruction translator; the instruction translator is used to convert the container configuration instruction into the uniquely corresponding image building operation;
[0013] If the container configuration instruction is a complex instruction, the translation is implemented by the configurator; wherein the configurator includes an instruction translation function; the instruction translation function corresponds one-to-one to the instruction type of the container configuration instruction, and is used to convert the container configuration instruction of the corresponding instruction type into one or more equivalent image building operations.
[0014] In a possible embodiment, the instruction types of the container configuration instructions include: add instructions, build parameter instructions, build command instructions, copy instructions, entry point instructions, environment variable instructions, exposed port instructions, base image instructions, image tag instructions, author information instructions, build execution instructions, build script instructions, working directory instructions, health check instructions, build time trigger instructions, stop signal instructions, user switching instructions, and mount point instructions;
[0015] The simple instructions include: add instructions, build parameter instructions, build command instructions, copy instructions, entry point instructions, environment variable instructions, exposed port instructions, base image instructions, image tag instructions, author information instructions, build execution instructions, build script instructions, and working directory instructions;
[0016] The complex instructions include: health check instructions, build-time trigger instructions, stop signal instructions, user switching instructions, and mount point instructions.
[0017] In a possible embodiment, the builder translates the simple instruction into the equivalent image building operation including:
[0018] If the container configuration instruction is an add instruction, the instruction translator translates the container configuration instruction into an equivalent virtual machine file system instruction, or translates it into an instruction for calling a specific application programming interface, so as to perform a file copy operation supporting a remote uniform resource locator system and self-decompression of compressed files;
[0019] If the container configuration instruction is a build parameter instruction, translating the container configuration instruction into an equivalent operation of storing build-time variables by the instruction translator;
[0020] If the container configuration instruction is a build command instruction, the container configuration instruction is translated into an equivalent virtual machine startup instruction, script or service configuration by the instruction translator;
[0021] If the container configuration instruction is a copy instruction, the container configuration instruction is translated into an equivalent virtual machine file system instruction by the instruction translator to perform a file copy operation;
[0022] If the container configuration instruction is an entry point instruction, the container configuration instruction is translated into an equivalent virtual machine startup instruction or service configuration by the instruction translator;
[0023] If the container configuration instruction is an environment variable instruction, the instruction translator translates the container configuration instruction into an equivalent operation of setting the environment variable in the virtual machine;
[0024] If the container configuration instruction is an expose port instruction, the container configuration instruction is translated into an equivalent virtual machine network and firewall setting operation by the instruction translator;
[0025] If the container configuration instruction is a basic image instruction, the instruction translator translates the container configuration instruction into an equivalent basic virtual machine image selection operation; wherein there is a mapping relationship between the container image and the basic virtual machine image;
[0026] If the container configuration instruction is an image label instruction, the instruction translator translates the container configuration instruction into an equivalent operation of adding a label to the metadata of the basic virtual machine image;
[0027] If the container configuration instruction is an author information instruction, the instruction translator translates the container configuration instruction into an equivalent operation of adding author information to the metadata of the basic virtual machine image;
[0028] If the container configuration instruction is a build execution instruction, the container configuration instruction is translated into an equivalent virtual machine file system instruction by the instruction translator, and the virtual machine file system instruction is executed during the construction process of the virtual machine;
[0029] If the container configuration instruction is a build script instruction, the container configuration instruction is translated into an equivalent operation of setting a default script in the virtual machine by the instruction translator;
[0030] If the container configuration instruction is a working directory instruction, the container configuration instruction is translated into an equivalent operation of setting a working directory in the virtual machine by the instruction translator.
[0031] In a possible embodiment, the configurator translates the complex instruction into the equivalent image building operation including:
[0032] According to the instruction type of the complex instruction, calling the corresponding instruction translation function;
[0033] Wherein, if the container configuration instruction is a health check instruction, the corresponding instruction translation function is used to: create a service or a planned task for implementing a health check in the virtual machine;
[0034] If the container configuration instruction is a build-time trigger instruction, the corresponding instruction translation function is used to: convert the container configuration instruction into an equivalent virtual machine file system instruction, and the virtual machine file system instruction is triggered to execute when the image created by the container configuration instruction is used as the basic virtual machine image;
[0035] If the container configuration instruction is a stop signal instruction, the corresponding instruction translation function is used to: configure the shutdown behavior of the virtual machine;
[0036] If the container configuration instruction is a user switching instruction, the corresponding instruction translation function is used to: set the user and group of the virtual machine;
[0037] If the container configuration instruction is a mount point instruction, the corresponding instruction translation function is used to set persistent storage or a mount point in the virtual machine.
[0038] In a possible embodiment, after determining whether the container configuration instruction is a simple instruction or a complex instruction according to the instruction type of the container configuration instruction, the method further includes:
[0039] If the container configuration instruction cannot be determined as the simple instruction or the complex instruction, an error message is returned and the method is terminated;
[0040] The error information includes the container configuration instruction causing the method to terminate.
[0041] In a possible embodiment, the method further includes:
[0042] If there is a translation failure of the container configuration instruction, the translation result is recorded in a pre-created translation log, and the translation of the container configuration instruction is performed again.
[0043] In a possible embodiment, the method further includes:
[0044] If the number of times the container configuration instruction is re-translated exceeds a preset threshold, the error message is returned and the method is terminated.
[0045] In a possible embodiment, the image building operation obtained by performing the translation by the execution engine of the image building tool to generate a virtual machine / bare metal image includes:
[0046] Whenever the translation of one of the container configuration instructions is completed, the image building operation obtained by translation is stored in a pre-created empty file according to the translation order of the container configuration instructions to obtain an image building file;
[0047] When the translation of all the container configuration instructions is completed, compile the image building file;
[0048] The compiled image building file is executed by the execution engine of the image building tool to generate a virtual machine / bare metal image.
[0049] In a possible embodiment, the steps of parsing the container configuration file to obtain the container configuration instruction, storing the translated image building operation in a pre-created empty file in the translation order of the container configuration instruction each time the translation of one of the container configuration instructions is completed to obtain the image building file; compiling the image building file when the translation of all the container configuration instructions is completed; and executing the compiled image building file through the execution engine of the image building tool to generate a virtual machine / bare metal image are all performed by the builder;
[0050] Executing the compiled image building file through the execution engine of the image building tool to generate a virtual machine / bare metal image includes:
[0051] The builder executes the compiled image building file by calling the application programming interface of the image building tool.
[0052] In a possible embodiment, it further includes:
[0053] Compile the builder and the configurator into two independent binary plug-in files;
[0054] Add the two binary plug-in files corresponding to the builder and the configurator to the image building tool;
[0055] The configuration file of the image building tool is updated to enable the builder and the configurator.
[0056] In a possible embodiment, adding the two binary plug-in files corresponding to the builder and the configurator to the image building tool includes:
[0057] The two binary plug-in files corresponding to the builder and the configurator are stored in the plug-in directory of the image building tool;
[0058] or:
[0059] The locations of the two binary plug-in files corresponding to the builder and the configurator are specified in the configuration file of the image building tool.
[0060] In order to solve the above technical problem, the present invention also provides a computer program product, including a computer program / instruction, which implements the steps of the image building method as described above when executed by a processor.
[0061] In order to solve the above technical problems, the present invention further provides a mirror image construction device, comprising:
[0062] Memory for storing computer programs;
[0063] A processor is used to implement the steps of the image building method as described above when executing the computer program.
[0064] In order to solve the above technical problem, the present invention also provides a non-volatile storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the image building method as described above are implemented.
[0065] The present invention provides a method for building an image. The method implements the parsing of a container configuration file (Dockerfile) through a plug-in of a custom image building tool (packer) to obtain corresponding Dockerfile instructions. Each Dockerfile instruction is then translated into an equivalent image building operation that can be directly supported by the packer tool and the virtual machine / bare metal. Finally, the above-translated image building operation is executed by the execution engine in the core of the packer tool to implement image creation in the virtual machine / bare metal based on Dockerfile. It can be seen that the method supports the implementation of image building based on Dockerfile in the virtual machine / bare metal, so that Docker and the virtual machine / bare metal can share a configuration to complete their respective image building, without the need to write configurations and scripts separately for the virtual machine / bare metal, thereby ensuring the unity of the configuration and reducing the workload. In addition, the method also makes the migration from the containerized environment to the VM / bare metal environment easier and more reliable, allowing developers to use familiar Dockerfile syntax to define virtual machine / bare metal images, increasing the flexibility of deployment options.
[0066] The image building device, non-volatile storage medium and computer program product provided by the present invention correspond to the above method and have the same effects as above. BRIEF DESCRIPTION OF THE DRAWINGS
[0067] In order to more clearly illustrate the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0068] Figure 1 A flowchart of a mirror image building method provided by an embodiment of the present invention;
[0069] Figure 2 An architecture of an image building system and an image creation flow chart provided by an embodiment of the present invention;
[0070] Figure 3 A structural diagram of an image building device provided by an embodiment of the present invention;
[0071] Figure 4 A structural diagram of another image building device provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0072] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0073] The core of the present invention is to provide a mirror image construction method, device, medium and program product.
[0074] In order to enable those skilled in the art to better understand the solution of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific implementation methods.
[0075] In the related art, due to the differences in the file system structure between the application container engine (Docker) and the virtual machine (VM) / bare metal, when images need to be deployed in Docker and virtual machines / bare metal, configurations and scripts need to be written separately. The creation of images in virtual machines / bare metal cannot be directly implemented using Docker's container configuration file (Dockerfile), which increases the workload and causes inconsistency in image configurations in the two environments.
[0076] To solve the above problems, the present invention provides a mirror image construction method, such as Figure 1 As shown, including:
[0077] S11: Parse the container configuration file to obtain container configuration instructions.
[0078] S12: The builder and configurator sequentially translate each container configuration instruction into an equivalent image building operation.
[0079] The builder and configurator are both plug-ins of the image building tool. The image building operation is an operation implemented by the image building tool and instructions directly supported by the virtual machine / bare metal.
[0080] S13: The translated image building operation is executed by the execution engine of the image building tool to generate a virtual machine / bare metal image.
[0081] For step S11, since Dockerfile is a text file used to define the Docker container building process, its format and specifications are known, and there are currently a variety of mature technical solutions to parse Dockerfile to obtain multiple container configuration instructions (Dockerfile instructions) in the Dockerfile that describe the entire Docker container building process. In a possible implementation, the Dockerfile can be parsed by a Dockerfile parser.
[0082] Step S12 is the core step of this method, which aims to translate the Dockerfile instructions to obtain equivalent image building operations, that is, operations that can be implemented by instructions directly supported by the image building tool (packer) and virtual machines / bare metal. The translation process is implemented by two plug-ins in the packer tool: the builder (PackerBuilder) and the configurator (Packer Provisioner).
[0083] It should be noted that the packer tool includes three customizable plug-ins, namely the builder, configurator, and post-processor. In this method, the first two plug-ins are mainly used to translate the Dockerfile instructions, so as to facilitate the execution of the image building operation by the execution engine in the packer core in step S13.
[0084] As for step S13, packer is a currently available tool for creating images in virtual machines / bare metals. The execution engine in the packer core is the execution subject of various operations to implement image building, and it performs specific image creation operations. The key is that the execution engine in the packer core can only recognize instructions and operations in the virtual machine / bare metal environment, and does not support the Dockerfile specification itself. Therefore, in this method, a custom plug-in is used to implement the equivalent translation of Dockerfile instructions into image building operations that can be recognized and executed by the execution engine in the packer core.
[0085] As can be seen from the above, a mirror image construction method provided by the present application uses a custom packer component, i.e., a builder and a configurator, to translate the Dockerfile instructions, so as to convert the Dockerfile instructions into a mirror image creation operation that can be directly recognized and executed by the execution engine in the packer tool core. Thus, the mirror image creation task based on Dockerfile in the virtual machine / bare metal environment is realized, thereby ensuring the consistency of the mirror image configuration created under the current Docker and virtual machine / bare metal environment. In addition, this also makes it possible for developers to only need to write a Dockerfile to realize the mirror image creation under the Docker and virtual machine / bare metal environment, without having to write the corresponding scripts and configurations separately for the virtual machine / bare metal environment, greatly reducing the workload of mirror image creation. In addition, this method also makes it easier and more reliable to migrate from a containerized environment to a virtual machine / bare metal environment, allowing developers to use familiar Dockerfile syntax to define virtual machine / bare metal images, increasing the flexibility of deployment options, and also ensuring that whether containers or virtual machines are used in development, testing, and production environments, consistency between each other can be guaranteed.
[0086] Furthermore, this embodiment also provides a possible implementation scheme: the method is integrated into a continuous integration / continuous deployment (CI / CD) pipeline to achieve full automation of the process from Dockerfile to virtual machine / bare metal image building.
[0087] It should also be noted that this method can not only realize the image creation in the virtual machine / bare metal environment based on Dockerfile, but also can be extended to the image building process of various cloud platforms (such as AWS, Azure, OpenStack and other cloud platforms), so as to achieve the purpose of using the same Dockerfile to generate cloud-specific virtual machine images. And in a hybrid cloud environment, this method can be used to ensure a consistent image building process between private data centers and public clouds. In addition, this method can also be applied to the image building of edge computing devices, and can also achieve the purpose of using the same Dockerfile to generate images suitable for various edge devices. Even this method only uses two customizable plug-ins in packer. Through more plug-ins or further customization of existing plug-ins, security reinforcement steps can also be integrated into the image building process in the virtual machine / bare metal environment to ensure that the generated image meets the security standards of the organization.
[0088] Furthermore, this embodiment provides a possible implementation scheme for how to implement the translation of Dockerfile instructions into image creation operations through the builder and configurator in the above step S12. The above step S12 specifically includes:
[0089] S121: Determine whether the container configuration instruction is a simple instruction or a complex instruction according to the instruction type of the container configuration instruction.
[0090] S122: If the container configuration instruction is a simple instruction, the builder implements translation.
[0091] The builder includes an instruction translator, which is used to convert container configuration instructions into unique corresponding image building operations.
[0092] S123: If the container configuration instruction is a complex instruction, the configurator implements translation.
[0093] The configurator includes an instruction translation function, which corresponds to the instruction type of the container configuration instruction one by one, and is used to convert the container configuration instruction of the corresponding instruction type into one or more equivalent image building operations.
[0094] As can be seen from the above, in this embodiment, the container configuration instructions are divided into two categories, simple instructions and complex instructions, according to the different instruction types, and the builder and configurator are used to translate them respectively. In packer, the builder is mainly used to read the configuration and create an image for the platform. The configurator is a module that installs and configures the software program on the device where the builder runs. Therefore, the builder itself has functional requirements, and its scalability is limited compared to the configurator.
[0095] Therefore, in this method, the builder is only used to implement the translation of simple instructions, which is implemented by adding an instruction translator in the builder. The instruction translator can convert the container configuration instructions into a unique corresponding image building operation. That is, the instruction type for which there is a unique corresponding image building operation is a simple instruction. For the instruction type that cannot be translated into a unique corresponding image building operation, it is a complex instruction, which is implemented by the configurator. In the configurator, the translation of complex instructions can be implemented by writing a software program. For example, a corresponding instruction translation function is written for each complex instruction in the software program, and the software program is implemented by the configurator. Therefore, the configurator can translate complex instructions by calling the corresponding instruction translation function, and translate to obtain one or more equivalent image building operations.
[0096] This method is based on the advantages and disadvantages of the builder and configurator in Packer in different functions. Different container configuration instructions are implemented by different plug-ins, thereby ensuring the smooth translation of container configuration instructions and ensuring that this method can be implemented through the Packer tool without the need to introduce new tools or functional modules.
[0097] Furthermore, with respect to the instruction types that may be involved in the above container configuration instructions in practical applications, and whether each instruction type is a simple instruction or a complex instruction, this embodiment also provides a possible implementation scheme:
[0098] The instruction types of container configuration instructions include: add instruction (ADD), build parameter instruction (ARG), build command instruction (CMD), copy instruction (COPY), entry point instruction (ENTRYPOINT), environment variable instruction (ENV), exposed port instruction (EXPOSE), base image instruction (FROM), image label instruction (LABEL), author information instruction (MAINTAINER), build execution instruction (RUN), build script instruction (SHELL), working directory instruction (WORKDIR), health check instruction (HEALTHCHECK), build time trigger instruction (ONBUILD), stop signal instruction (STOPSIGNAL), user switching instruction (USER), mount point instruction (VOLUME).
[0099] Among them, simple instructions include: add instruction (ADD), build parameter instruction (ARG), build command instruction (CMD), copy instruction (COPY), entry point instruction (ENTRYPOINT), environment variable instruction (ENV), expose port instruction (EXPOSE), base image instruction (FROM), image label instruction (LABEL), author information instruction (MAINTAINER), build execution instruction (RUN), build script instruction (SHELL), and working directory instruction (WORKDIR).
[0100] Complex instructions include: health check instructions (HEALTHCHECK), build-time trigger instructions (ONBUILD), stop signal instructions (STOPSIGNAL), user switching instructions (USER), and mount point instructions (VOLUME).
[0101] It should be noted that the basis for dividing the container configuration instructions of different instruction types in this embodiment into simple instructions or complex instructions is whether the container configuration instructions can be equivalent to a unique image building operation. If it can, the instruction type is a simple instruction, and the translation can be achieved by the instruction translator in the builder. If not, the instruction type is a complex instruction, and it is necessary to write an instruction translation function accordingly and implement the translation of the corresponding container configuration instruction through the configurator call. The division method of simple instructions and complex instructions provided in this embodiment is adapted to the characteristics that the builder and configurator in the above-mentioned packer have different performance advantages in different aspects, and is also conducive to using the existing packer tool to create an image based on Dockerfile in a virtual machine / bare metal environment.
[0102] Furthermore, to further illustrate the method, this embodiment also provides corresponding translation solutions for all instruction types mentioned in the above embodiments:
[0103] 1. Add instruction (ADD):
[0104] If the container configuration instruction is an add instruction, the container configuration instruction is translated into an equivalent virtual machine file system instruction through an instruction translator, or translated into an instruction for calling a specific application programming interface (Application Programming Interface, API) to perform file copy operations that support remote uniform resource locator (URL) and compressed file self-decompression.
[0105] 2. Build parameter instructions (ARG):
[0106] If the container configuration instruction is a build parameter instruction, the instruction translator translates the container configuration instruction into an equivalent operation of storing build-time variables. This instruction may require special processing in a virtual machine environment.
[0107] 3. Build command instructions (CMD):
[0108] If the container configuration instruction is a build command instruction, the container configuration instruction is translated into an equivalent virtual machine startup instruction, script or service configuration through an instruction translator.
[0109] 4. Copy instruction (COPY):
[0110] If the container configuration instruction is a copy instruction, the container configuration instruction is translated into an equivalent virtual machine file system instruction by an instruction translator to perform a file copy operation.
[0111] 5. Entry point instruction (ENTRYPOINT):
[0112] If the container configuration instruction is an entry point instruction, the container configuration instruction is translated into an equivalent virtual machine startup instruction or service configuration through an instruction translator.
[0113] 6. Environment variable directive (ENV):
[0114] If the container configuration instruction is an environment variable instruction, the container configuration instruction is translated into an equivalent operation of setting the environment variable in the virtual machine through the instruction translator.
[0115] Specifically, it may be achieved by modifying the / etc / environment file or its equivalent file in the virtual machine.
[0116] 7. Expose port instruction (EXPOSE):
[0117] If the container configuration instruction is an exposed port instruction, the container configuration instruction is translated into equivalent virtual machine network and firewall setting operations through an instruction translator.
[0118] 8. Basic image instructions (FROM):
[0119] If the container configuration instruction is a basic image instruction, the container configuration instruction is translated into an equivalent basic virtual machine image selection operation through an instruction translator.
[0120] There is a mapping relationship between the container image and the basic virtual machine image. The translation of this instruction may require the use of this mapping relationship.
[0121] 9. Image label instruction (LABEL):
[0122] If the container configuration instruction is an image label instruction, the container configuration instruction is translated into an equivalent operation of adding a label to the metadata of the basic virtual machine image through an instruction translator.
[0123] 10. Author information command (MAINTAINER):
[0124] If the container configuration instruction is an author information instruction, the container configuration instruction is translated into an equivalent operation of adding author information to the metadata of the basic virtual machine image through the instruction translator.
[0125] 11. Build execution instructions (RUN):
[0126] If the container configuration instruction is a build execution instruction, the container configuration instruction is translated into an equivalent virtual machine file system instruction by an instruction translator, and the virtual machine file system instruction is executed during the build process of the virtual machine.
[0127] 12. Build script instructions (SHELL):
[0128] If the container configuration instruction is a build script instruction, the container configuration instruction is translated into an equivalent operation of setting a default script in the virtual machine through an instruction translator.
[0129] It should be noted that the script here generally refers to a shell script.
[0130] 13. Working directory command (WORKDIR):
[0131] If the container configuration instruction is a working directory instruction, the container configuration instruction is translated into an equivalent operation of setting a working directory in the virtual machine through an instruction translator.
[0132] 14. Health check command (HEALTHCHECK):
[0133] If the container configuration instruction is a health check instruction, the corresponding instruction translation function is used to: create a service or scheduled task in the virtual machine for implementing the health check.
[0134] 15. Trigger command during build (ONBUILD):
[0135] If the container configuration instruction is a build-time trigger instruction, the corresponding instruction translation function is used to convert the container configuration instruction into an equivalent virtual machine file system instruction, and the virtual machine file system instruction is triggered to execute when the image created by the container configuration instruction is used as the base virtual machine image. This instruction requires special processing in the virtual machine environment and is a complex instruction.
[0136] 16. Stop signal command (STOPSIGNAL):
[0137] If the container configuration instruction is a stop signal instruction, the corresponding instruction translation function is used to: configure the shutdown behavior of the virtual machine.
[0138] 17. User switching command (USER):
[0139] If the container configuration instruction is a user switching instruction, the corresponding instruction translation function is used to: set the user and group of the virtual machine.
[0140] 18. Mount point command (VOLUME):
[0141] If the container configuration instruction is a mount point instruction, the corresponding instruction translation function is used to set persistent storage or a mount point in the virtual machine.
[0142] Based on the translation schemes of various instruction types of container configuration instructions provided in this embodiment, it can be known that the translation scheme provided in this embodiment takes into account the following key factors: 1) File system differences: The conversion process takes into account the differences in the file system structures of containers and virtual machines. 2) Network: Map Docker network concepts to appropriate virtual machine network and firewall configurations. 3) Base image: Maintain the mapping between Docker base images and equivalent virtual machine images. 4) Docker-specific concepts: Some Docker concepts (such as layers) may not have direct equivalents in virtual machines and require alternative implementations.
[0143] Based on the above considerations, this embodiment can achieve accurate and reliable translation of container configuration instructions, ensuring the smooth creation of images based on Dockerfile in a virtual machine / bare metal environment.
[0144] Furthermore, the instruction types mentioned in the above embodiments are only the more common instruction types in actual application scenarios when creating images. For a very small number of special instruction types, due to the wide variety of types, the above embodiments do not give examples one by one, but it does not mean that this method cannot be applied to other instruction types. It is only necessary to add the instruction translation function of the corresponding instruction type in the configurator to achieve the expansion of the translatable instruction types.
[0145] In actual applications, some instruction types may not be configured for translation (i.e., simple instructions and complex instructions that do not belong to the above configuration). In this case, the builder and configurator cannot translate the container configuration instruction, which will affect the subsequent translation of other container configuration instructions and image creation. Therefore, this embodiment provides a corresponding implementation plan for this situation, and the method also includes:
[0146] S21: If there is a container configuration instruction translation failure, the translation result is recorded in a pre-created translation log, and the container configuration instruction is translated again.
[0147] That is, this embodiment provides a robust error handling and verification mechanism. The translation result of the container configuration instruction is verified. If the translation is successful, the translation of the next container configuration instruction is performed in sequence. If the translation fails, the current translation is stopped, and the translation result and the container configuration instruction that failed to translate are recorded in the translation log, which is convenient for developers to analyze the cause of the translation failure later.
[0148] For example, when the reason for the translation failure is that the instruction type is not included as a simple instruction or a complex instruction (that is, the current builder and configurator do not support the translation of the instruction type), the developer can learn the instruction type of the container configuration instruction based on the translation log, thereby expanding the translation of the instruction type. It should be noted that one possible solution to expand the instruction type translation is to add a corresponding instruction translation function in the configurator, which is relatively simple to implement.
[0149] However, the translation failure scenario targeted by this embodiment may be caused by other reasons, in addition to the instruction type of the container configuration instruction not supporting translation. For example, the builder, configurator or even the hardware device has an exception, resulting in a translation failure. At this point, this embodiment attempts to solve the problem by repeating the translation, which can effectively solve the translation failure caused by sporadic problems and improve the robustness of instruction translation.
[0150] Furthermore, the above embodiment can effectively solve the translation failure problem caused by occasional faults by repeating the translation. However, it may not guarantee an effective solution for non-occasional faults, and repeated translation may cause the image creation process to get stuck at this step, resulting in the method being unable to proceed smoothly. Based on this, this embodiment also provides a corresponding implementation scheme, and the above method also includes:
[0151] S22: If the number of times that the container configuration instruction is re-translated exceeds a preset threshold, an error message is returned and the method is terminated.
[0152] It should be noted that the preset threshold in this embodiment does not limit its specific value. The larger the value, the better the effect of solving occasional faults. The smaller the value, the smaller the efficiency reduction caused by repeated translation. Therefore, a suitable value can be selected according to actual needs. This embodiment can effectively avoid the infinite repetition of the translation of the same container configuration instruction caused by non-occasional faults through the setting of the preset threshold, avoid the image creation process from being stuck at a certain step, and allow the image creation process to proceed smoothly.
[0153] On the other hand, it has been explained above that a Dockerfile usually contains not only one container configuration instruction, but multiple container configuration instructions are translated one by one according to their order in the Dockerfile. After each container configuration instruction is translated, how to create an image? This embodiment provides a possible implementation scheme, and the above step S13 specifically includes:
[0154] S131: Whenever a container configuration instruction is translated, the translated image building operation is stored in a pre-created empty file according to the translation order of the container configuration instruction to obtain an image building file.
[0155] S132: When the translation of all container configuration instructions is completed, compile the image building file.
[0156] S133: Execute the compiled image building file through the execution engine of the image building tool to generate a virtual machine / bare metal image.
[0157] This embodiment adopts a solution of executing all container configuration instructions uniformly after translation. On the one hand, it ensures that some instructions that rely on subsequent image creation operations can be executed smoothly. On the other hand, it also makes it possible to output a compiled file for creating an image based on Dockerfile in a virtual machine / bare metal environment. In other words, in addition to directly implementing this image creation through the packer tool, a file for replicating this image creation can also be generated, and this image creation operation can be replicated in other scenarios at other times. It is also possible that after translating the Dockerfile, the image creation is not executed immediately, but the image construction file is executed by the packer core at any time according to actual needs to realize the creation of the image.
[0158] On the other hand, in the above embodiment, the translation of container configuration instructions is implemented by the builder and the configurator. More specifically, in a possible embodiment, based on the characteristics of the builder and the configurator themselves, the builder implements the translation of simple instructions, and the configurator implements the translation of complex instructions. This embodiment further provides another possible implementation scheme:
[0159] The above steps S11 and S13 (S131-S133) are all completed by the builder.
[0160] When step S13 is implemented by a builder, it is specifically as follows:
[0161] The builder executes the compiled image building file by calling the application programming interface of the image building tool.
[0162] That is, among all the steps involved in the above embodiment of the method, only the translation of complex instructions is completed by the configurator, and all other steps can be implemented by the builder. Based on this, the system architecture of the packer tool and the image creation process after the improvement of this embodiment are as follows: Figure 2 Among them, the red part is the part newly added after the improvement in this method, and the blue part is the part supported by the original packer tool (packer core).
[0163] Depend on Figure 2It can be seen that the customized builder of this embodiment can specifically include a Dockerfile parser, an instruction loop module, an instruction type judgment module, an instruction translator, and a build step executor. Among them, the Dockerfile parser is used to read the Dockerfile and parse the Dockerfile instructions. The instruction loop module is used to implement sequential Dockerfile instruction parsing. The instruction type judgment module is used to determine whether the instruction is a simple instruction or a complex instruction. Simple instructions are translated by the instruction translator, and complex instructions are translated by the configurator. The image creation operation obtained after translation is submitted to the build step executor, and the build step executor is used to determine whether all Dockerfile instructions have been translated. If not, the image creation operation received this time is saved in the image build file. If so, it is output to the packer core for execution.
[0164] Therefore, this embodiment is based on a custom builder and configurator, and this method can be implemented entirely by packer to create an image in a virtual machine / bare metal based on Dockerfile. This makes the implementation of the method simpler, and only a packer tool and Dockerfile are needed to create an image in a virtual machine / bare metal.
[0165] Furthermore, this embodiment also provides a corresponding implementation scheme for how to implement the above-mentioned builder and configurator, how to add them to the packer and enable them. The method also includes:
[0166] S31: Compile the builder and configurator into two separate binary plugin files.
[0167] S32: Add the two binary plug-in files corresponding to the builder and configurator to the image building tool.
[0168] S33: Update the configuration file of the image building tool to enable the builder and configurator.
[0169] For step S31, the above steps implemented by the builder or configurator are written in the form of code and then compiled into a binary file, which implements the customization process of the builder and configurator. For step S32, the binary files of the above two customized plug-ins are added to the packer so that the packer can recognize and use the two plug-ins. For step S33, the two plug-ins are started so that the packer can enable the customized builder and configurator, thereby implementing the image building method provided in the above embodiment.
[0170] It should also be noted that for the builder, the Dockerfile can be parsed in the prepare method and the container configuration instructions can be translated in the run method.
[0171] In addition, for the specific implementation of step S32, this embodiment also provides two possible implementation schemes. Step S32 may specifically be:
[0172] S32-A: Store the two binary plug-in files corresponding to the builder and configurator in the plug-in directory of the image building tool.
[0173] S32-B: Specify the locations of the two binary plug-in files corresponding to the builder and configurator in the configuration file of the image building tool.
[0174] That is, in this embodiment, one solution is to store the binary files corresponding to the two plug-ins in the designated plug-in directory of the packer so that they can be recognized by the packer. Another solution is to declare the storage addresses of the binary files corresponding to the two plug-ins in the packer so that the packer can find and recognize the two plug-ins, and finally use the two customized plug-ins, ensuring that the method can be implemented by the packer tool.
[0175] In the above embodiment, a mirror image construction method is described in detail, and the present invention also provides a corresponding embodiment of a mirror image construction device. It should be noted that the present invention describes the embodiment of the device part from two perspectives, one is based on the functional module perspective, and the other is based on the hardware perspective.
[0176] Based on the perspective of functional modules, such as Figure 3 As shown, this embodiment provides a mirror image building device, including:
[0177] The parsing module 11 is used to parse the container configuration file to obtain the container configuration instructions.
[0178] The translation module 12 is used to translate each container configuration instruction into an equivalent image building operation in sequence through a builder and a configurator; wherein the builder and the configurator are plug-ins of the image building tool; the image building operation is an operation implemented by instructions directly supported by the image building tool and the virtual machine / bare metal.
[0179] The execution module 13 is used to execute the translated image building operation through the execution engine of the image building tool to generate a virtual machine / bare metal image.
[0180] Since the embodiments of the apparatus part correspond to the embodiments of the method part, please refer to the description of the embodiments of the method part for the embodiments of the apparatus part, which will not be repeated here.
[0181] Figure 4 A structural diagram of a mirror image building device provided in another embodiment of the present invention, such as Figure 4 As shown, an image building device includes: a memory 20, for storing a computer program;
[0182] The processor 21 is used to implement the steps of an image building method in the above embodiment when executing a computer program.
[0183] The image building device provided in this embodiment may include but is not limited to a mobile terminal, a personal computer, a workstation, etc.
[0184] Among them, the processor 21 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 21 can be implemented in at least one hardware form of a digital signal processor (DSP), a field-programmable gate array (FPGA), and a programmable logic array (PLA). The processor 21 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a central processing unit (CPU); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 21 may be integrated with a graphics processing unit (GPU), which is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 21 may also include an artificial intelligence (AI) processor, which is used to process computing operations related to machine learning.
[0185] The memory 20 may include one or more computer-readable storage media, which may be non-transitory. The memory 20 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In this embodiment, the memory 20 is at least used to store the following computer program 201, wherein, after the computer program is loaded and executed by the processor 21, it can implement the relevant steps of a mirror image building method disclosed in any of the aforementioned embodiments. In addition, the resources stored in the memory 20 may also include an operating system 202 and data 203, etc., and the storage method may be temporary storage or permanent storage. Among them, the operating system 202 may include Windows, Unix, Linux, etc. Data 203 may include, but is not limited to, a mirror image building method, etc.
[0186] In some embodiments, an image building device may further include a display screen 22 , an input / output interface 23 , a communication interface 24 , a power supply 25 , and a communication bus 26 .
[0187] Those skilled in the art will understand that Figure 4 The structure shown in the figure does not constitute a limitation on an image building device, and may include more or less components than those shown in the figure.
[0188] An embodiment of the present invention provides an image building device, which includes a memory and a processor. When the processor executes a program stored in the memory, it can implement the following method: an image building method.
[0189] In addition to the embodiments of the image building method and device provided in the above embodiments, the present invention also provides an embodiment corresponding to a computer program product. A computer program product includes a computer program / instruction, and when the computer program / instruction is executed by a processor, the steps of the image building method described in any of the above embodiments can be implemented.
[0190] Since the embodiments of the computer program product part correspond to the embodiments of the method part, please refer to the description of the embodiments of the method part for the embodiments of the computer program product part, which will not be repeated here.
[0191] Finally, the present invention also provides an embodiment corresponding to a non-volatile storage medium. The non-volatile storage medium stores a computer program, and when the computer program is executed by a processor, the steps recorded in the above method embodiment are implemented.
[0192] It is understandable that if the method in the above embodiment is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a non-volatile storage medium. Based on this understanding, the technical solution of the present invention, 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, and the computer software product is stored in a storage medium to execute 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, etc. Various media that can store program codes.
[0193] The above is a detailed introduction to a mirror image construction method, device, medium and program product provided by the present invention. The various embodiments in the specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same and similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description. It should be pointed out that for ordinary technicians in this technical field, without departing from the principle of the present invention, the present invention can also be improved and modified in a number of ways, and these improvements and modifications also fall within the scope of protection of the present invention.
[0194] It should also be noted that, in this specification, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "comprise", "include" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the statement "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or device including the element.
Claims
1. A mirror image construction method, characterized in that: include: Parse the container configuration file to obtain container configuration instructions; Determining whether the container configuration instruction is a simple instruction or a complex instruction according to the instruction type of the container configuration instruction; If the container configuration instruction is a simple instruction, the builder implements the translation; wherein the builder includes an instruction translator; the instruction translator is used to convert the container configuration instruction into a unique corresponding image building operation; If the container configuration instruction is a complex instruction, the translation is implemented by the configurator; wherein the configurator includes an instruction translation function; the instruction translation function corresponds to the instruction type of the container configuration instruction one by one, and is used to convert the container configuration instruction of the corresponding instruction type into one or more equivalent image building operations; the builder and the configurator are both plug-ins of the image building tool; the image building operation is an operation implemented by the instructions directly supported by the image building tool and the virtual machine / bare metal; The image building operation obtained by the translation is executed by the execution engine of the image building tool to generate a virtual machine / bare metal image.
2. The mirror image construction method according to claim 1, characterized in that: The instruction types of the container configuration instructions include: add instructions, build parameter instructions, build command instructions, copy instructions, entry point instructions, environment variable instructions, exposed port instructions, basic image instructions, image tag instructions, author information instructions, build execution instructions, build script instructions, working directory instructions, health check instructions, build time trigger instructions, stop signal instructions, user switching instructions, and mount point instructions; The simple instructions include: add instructions, build parameter instructions, build command instructions, copy instructions, entry point instructions, environment variable instructions, exposed port instructions, base image instructions, image tag instructions, author information instructions, build execution instructions, build script instructions, and working directory instructions; The complex instructions include: health check instructions, build-time trigger instructions, stop signal instructions, user switching instructions, and mount point instructions.
3. The mirror image construction method according to claim 2, characterized in that: The builder translates the simple instructions into equivalent image building operations including: If the container configuration instruction is an add instruction, the instruction translator translates the container configuration instruction into an equivalent virtual machine file system instruction, or translates it into an instruction for calling a specific application programming interface, so as to perform a file copy operation supporting a remote uniform resource locator system and self-decompression of compressed files; If the container configuration instruction is a build parameter instruction, translating the container configuration instruction into an equivalent operation of storing build-time variables by the instruction translator; If the container configuration instruction is a build command instruction, the container configuration instruction is translated into an equivalent virtual machine startup instruction, script or service configuration by the instruction translator; If the container configuration instruction is a copy instruction, the container configuration instruction is translated into an equivalent virtual machine file system instruction by the instruction translator to perform a file copy operation; If the container configuration instruction is an entry point instruction, the container configuration instruction is translated into an equivalent virtual machine startup instruction or service configuration by the instruction translator; If the container configuration instruction is an environment variable instruction, the container configuration instruction is translated into an equivalent operation of setting the environment variable in the virtual machine by the instruction translator; If the container configuration instruction is an expose port instruction, the container configuration instruction is translated into an equivalent virtual machine network and firewall setting operation by the instruction translator; If the container configuration instruction is a basic image instruction, the instruction translator translates the container configuration instruction into an equivalent basic virtual machine image selection operation; wherein there is a mapping relationship between the container image and the basic virtual machine image; If the container configuration instruction is an image label instruction, the instruction translator translates the container configuration instruction into an equivalent operation of adding a label to the metadata of the basic virtual machine image; If the container configuration instruction is an author information instruction, the instruction translator translates the container configuration instruction into an equivalent operation of adding author information to the metadata of the basic virtual machine image; If the container configuration instruction is a build execution instruction, the container configuration instruction is translated into an equivalent virtual machine file system instruction by the instruction translator, and the virtual machine file system instruction is executed during the construction process of the virtual machine; If the container configuration instruction is a build script instruction, the container configuration instruction is translated into an equivalent operation of setting a default script in the virtual machine by the instruction translator; If the container configuration instruction is a working directory instruction, the container configuration instruction is translated into an equivalent operation of setting a working directory in the virtual machine by the instruction translator.
4. The mirror image construction method according to claim 3, characterized in that: The configurator translates the complex instructions into equivalent image building operations including: According to the instruction type of the complex instruction, calling the corresponding instruction translation function; Wherein, if the container configuration instruction is a health check instruction, the corresponding instruction translation function is used to: create a service or a planned task for implementing a health check in the virtual machine; If the container configuration instruction is a build-time trigger instruction, the corresponding instruction translation function is used to: convert the container configuration instruction into an equivalent virtual machine file system instruction, and the virtual machine file system instruction is triggered to execute when the image created by the container configuration instruction is used as the basic virtual machine image; If the container configuration instruction is a stop signal instruction, the corresponding instruction translation function is used to: configure the shutdown behavior of the virtual machine; If the container configuration instruction is a user switching instruction, the corresponding instruction translation function is used to: set the user and group of the virtual machine; If the container configuration instruction is a mount point instruction, the corresponding instruction translation function is used to: set persistent storage or a mount point in the virtual machine.
5. The mirror image construction method according to claim 4, characterized in that: After determining whether the container configuration instruction is a simple instruction or a complex instruction according to the instruction type of the container configuration instruction, the method further includes: If the container configuration instruction cannot be determined as the simple instruction or the complex instruction, an error message is returned and the method is terminated; The error information includes the container configuration instruction causing the method to terminate.
6. The mirror image construction method according to claim 5, characterized in that: The method further comprises: If there is a translation failure of the container configuration instruction, the translation result is recorded in a pre-created translation log, and the translation of the container configuration instruction is performed again.
7. The mirror image construction method according to claim 6, characterized in that: The method further comprises: If the number of times the container configuration instruction is re-translated exceeds a preset threshold, the error message is returned and the method is terminated.
8. The mirror image construction method according to any one of claims 1 to 7, characterized in that: The image building operation obtained by performing the translation by the execution engine of the image building tool to generate a virtual machine / bare metal image includes: Whenever the translation of one of the container configuration instructions is completed, the image building operation obtained by translation is stored in a pre-created empty file according to the translation order of the container configuration instructions to obtain an image building file; When the translation of all the container configuration instructions is completed, compile the image building file; The compiled image building file is executed by the execution engine of the image building tool to generate a virtual machine / bare metal image.
9. The mirror image construction method according to claim 8, characterized in that: The step of parsing the container configuration file to obtain the container configuration instruction, and storing the translated image building operation in a pre-created empty file in the translation order of the container configuration instruction each time the translation of the container configuration instruction is completed, so as to obtain the image building file; When all the container configuration instructions are translated, the image building file is compiled; the steps of executing the compiled image building file through the execution engine of the image building tool to generate a virtual machine / bare metal image are all executed by the builder; Executing the compiled image building file through the execution engine of the image building tool to generate a virtual machine / bare metal image includes: The builder executes the compiled image building file by calling the application programming interface of the image building tool.
10. The mirror image construction method according to claim 9, characterized in that: Also includes: Compile the builder and the configurator into two independent binary plug-in files; Add the two binary plug-in files corresponding to the builder and the configurator to the image building tool; The configuration file of the image building tool is updated to enable the builder and the configurator.
11. The mirror image construction method according to claim 10, characterized in that: The adding the two binary plug-in files corresponding to the builder and the configurator to the image building tool comprises: The two binary plug-in files corresponding to the builder and the configurator are stored in the plug-in directory of the image building tool; or: The locations of the two binary plug-in files corresponding to the builder and the configurator are specified in the configuration file of the image building tool.
12. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the steps of the image building method according to any one of claims 1 to 11 are implemented.
13. A mirror image construction device, characterized in that: include: Memory for storing computer programs; A processor, configured to implement the steps of the image building method according to any one of claims 1 to 11 when executing the computer program.
14. A non-volatile storage medium, characterized in that: The non-volatile storage medium stores a computer program, and when the computer program is executed by the processor, the steps of the image building method according to any one of claims 1 to 11 are implemented.
Citation Information
Patent Citations
Method and device of migration of virtual machine to container
CN107766130A