Updating service method and device based on docker environment
By using the GitLab platform in the Docker environment to monitor pipeline branch code merging, build new images, and switch traffic, the problems of high cost and complexity of Docker service updates are solved, enabling low-cost, simple-to-maintain, and non-stop updates.
Patent Information
- Application Number
- CN202410716210.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-04
- Publication Date
- 2024-09-03
AI Technical Summary
Existing Docker service update methods are costly and technically complex when updating without downtime, making them unsuitable for small projects or situations with limited resources. Furthermore, they require the installation of a container orchestration system, which adds extra resources and complexity.
By monitoring the merging of pipeline branch code, the GitLab platform is used as a code management tool to build new images and perform traffic switching operations to complete service updates, including container switching and image cleanup, avoiding the need to install a container orchestration system and reducing costs and complexity.
It enables low-cost, simple-to-maintain, and non-stop service updates in a Docker environment. By monitoring pipeline branch code merging to trigger new image building and traffic switching, it reduces the dependence on additional resources.
Smart Images

Figure CN118585221A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of docker update, and in particular to an update service method and device based on a docker environment. Background Art
[0002] When a docker container needs to be updated, the service of the docker container needs to be suspended. Currently, in the market, non-stop updates in a docker environment usually require the use of container orchestration tools, such as dockerswarm or kubernetes.
[0003] Although the existing Docker service update method can be achieved without downtime through container orchestration tools, container orchestration tools introduce additional complexity and require time and resources to learn, configure, optimize and maintain. In addition, the container orchestration system itself needs to run on a server or virtual machine, which adds additional resources and cannot be applied to small projects or situations with limited resources. Summary of the invention
[0004] The present invention provides a method and device for updating services based on a docker environment, so as to solve the problems of high cost and complex technology of the existing method for updating services without shutting down the docker service.
[0005] In order to solve the above technical problems, an embodiment of the present invention provides an update service method based on a docker environment, comprising:
[0006] Monitoring pipeline branch codes, and starting pipeline operations when development branch codes are merged with the pipeline branch codes;
[0007] When the pipeline job is started, a new image is constructed, and a traffic switching operation is performed according to the new image to complete the service update; the traffic switching operation includes container switching, container cleaning and image cleaning.
[0008] The present invention uses the gitlab platform as a code management tool to control the entire process of service updates, organically combines code management, integrated pipelines, delivery pipelines and non-stop updates, monitors pipeline branch codes, uses code merging as a trigger point, builds a new image to perform traffic switching operations, and completes service updates. There is no need to install a container orchestration system, add additional resources, and the cost is low and maintenance is simple.
[0009] Furthermore, in the monitoring pipeline branch code, when the development branch code is merged with the pipeline branch code, before starting the pipeline operation, it also includes:
[0010] Build the gitlab platform, register the platform operation service, and configure the integrated delivery tool according to the configuration file; the configuration file includes the instructions and parameters required to build the Docker image;
[0011] Write the image deployment file and traffic switching script file for application deployment. The image deployment file package is used to build a new image.
[0012] Further, a new image is constructed according to the image deployment file, specifically:
[0013] Select a base image and set the working directory of the base image;
[0014] Configure the main program path and execution program of the base image, and set the resource file path of the base image.
[0015] Further, the traffic switching operation is performed according to the new image to complete the service update, specifically:
[0016] Start the standby container, switch the service request to the standby container according to the traffic switching script file, and stop running the old container;
[0017] Start a new image and a new container, switch the script file according to the traffic, switch the service request to the new container, and stop running the standby container;
[0018] Clean up the backup containers and old images to complete the service update.
[0019] In a second aspect, the present invention further provides an update service device based on a docker environment, comprising:
[0020] Monitoring pipeline branch codes, and starting pipeline operations when development branch codes are merged with the pipeline branch codes;
[0021] When the pipeline job is started, a new image is constructed, and a traffic switching operation is performed according to the new image to complete the service update; the traffic switching operation includes container switching, container cleaning and image cleaning.
[0022] Furthermore, in the monitoring pipeline branch code, when the development branch code is merged with the pipeline branch code, before starting the pipeline operation, it also includes:
[0023] Build the gitlab platform, register the platform operation service, and configure the integrated delivery tool according to the configuration file; the configuration file includes the instructions and parameters required to build the Docker image;
[0024] Write the image deployment file and traffic switching script file for application deployment. The image deployment file package is used to build a new image.
[0025] Further, a new image is constructed according to the image deployment file, specifically:
[0026] Select a base image and set the working directory of the base image;
[0027] Configure the main program path and execution program of the base image, and set the resource file path of the base image.
[0028] Further, the traffic switching operation is performed according to the new image to complete the service update, specifically:
[0029] Start the standby container, switch the service request to the standby container according to the traffic switching script file, and stop running the old container;
[0030] Start a new image and a new container, switch the script file according to the traffic, switch the service request to the new container, and stop running the standby container;
[0031] Clean up the backup containers and old images to complete the service update.
[0032] In a third aspect, the present invention also provides a computer device, comprising: a processor, a communication interface and a memory, wherein the processor, the communication interface and the memory are interconnected, wherein the memory stores an executable program code, and the processor is used to call the executable program code to execute the update service method based on the docker environment.
[0033] In a fourth aspect, the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer instructions, and when the computer instructions are executed by a processor, the update service method based on the Docker environment is implemented. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] Figure 1 A flow chart of a method for updating a service based on a Docker environment provided by an embodiment of the present invention;
[0035] Figure 2 A flow chart of a traffic switching process provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0036] 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.
[0037] Embodiment 1
[0038] Please refer to Figure 1 , Figure 1 A flow chart of a method for updating a service based on a docker environment provided by an embodiment of the present invention includes steps 101 to 102, which are as follows:
[0039] Step 101: monitoring pipeline branch codes, and starting pipeline operations when development branch codes are merged with the pipeline branch codes;
[0040] Step 102: When the pipeline job is started, a new image is constructed, and a traffic switching operation is performed according to the new image to complete the service update; the traffic switching operation includes container switching, container cleaning, and image cleaning.
[0041] In this embodiment, in the monitoring pipeline branch code, when the development branch code is merged with the pipeline branch code, before starting the pipeline operation, it also includes:
[0042] Build the gitlab platform, register the platform operation service, and configure the integrated delivery tool according to the configuration file; the configuration file includes the instructions and parameters required to build the Docker image;
[0043] Write the image deployment file and traffic switching script file for application deployment. The image deployment file package is used to build a new image.
[0044] In this embodiment, a gitlab platform is built, the gitlab-runner service is registered, CI / CD integration, delivery and deployment pipelines are configured, and nginx is installed.
[0045] In this embodiment, the gitlab platform is a code management platform, and the entire process of service updates is managed and controlled based on the code management platform to achieve automated deployment and non-stop update mechanisms, thereby reducing the cost and complexity of non-stop updates.
[0046] In this embodiment, the configuration file is a .gitlab-ci.yml file, which specifies the image name, service name, normal service port mapping, backup service port mapping, normal NGINX configuration file path, backup NGINX configuration file path, pipeline name and order, pipeline tasks, pipeline monitoring and traffic switching instructions.
[0047] As a specific example in an embodiment of the present invention, an image name, a service name, a normal service port mapping, a backup service port mapping, a normal NGINX configuration file path, and a backup NGINX configuration file path are defined according to global variables. Specifically, a Docker image name variable is defined: DOCKER_IMAGE_NAME; a service name variable is defined: SERVER_NAME; a normal service port mapping is specified, and a variable is defined: SERVER_PORT; a backup service port mapping is specified, and a variable is defined: BACKUP_SERVER_PORT; a normal NGINX configuration file path variable is defined: NGINX_CONFIG_NORMAL; and a backup NGINX configuration file path variable is defined: NGINX_CONFIG_BACKUP.
[0048] In this embodiment, the pipeline task includes a first stage, a second stage and a third stage. The first stage is defined as build, the second stage is defined as sync, and the third stage is defined as deploy.
[0049] In this embodiment, the stage tasks of the first stage include specifying a Dockerfile to build a Docker image and specifying an image name.
[0050] In this embodiment, the stage tasks of the second stage include running the standby service container and switching the request to the standby service container.
[0051] As a specific example of an embodiment of the present invention, the stage tasks of the second stage include:
[0052] script:
[0053] #Get the old image name
[0054] -oldimage=$(docker inspect--format='{{.Config.Image}}'"$SERVER_NAME-${CI_COMMIT_REF_NAME:8}")
[0055] #Run the backup service named: test-backup, port mapping: 8889:8889
[0056] -docker run--name$SERVER_NAME-backup-p$BACKUP_SERVER_PORT-itd$DOCKER_IMAGE_NAME-${CI_COMMIT_REF_NAME:8}
[0057] #Switch to backup NGINX to start the backup container
[0058] -. / switch.sh$NGINX_CONFIG_BACKUP
[0059] #Stop the original service container
[0060] -docker stop$SERVER_NAME-${CI_COMMIT_REF_NAME:8}
[0061] #Delete the original service container
[0062] -docker rm$SERVER_NAME-${CI_COMMIT_REF_NAME:8}
[0063] In this embodiment, the stage tasks of the third stage include starting a new service container and switching the request to the new service container, and deleting the backup service container and deleting the old image.
[0064] As a specific example of an embodiment of the present invention, the stage tasks of the third stage include:
[0065] script:
[0066] #Use the new service container to start, which may be named: test-dev-65806174, port mapping: 8888:8888
[0067] -docker run--name$SERVER_NAME-${CI_COMMIT_REF_NAME:8}-p$SERVER_PORT-itd$DOCKER_IMAGE_NAME-${CI_COMMIT_REF_NAME:8}
[0068] #Switch back to normal NGINX configuration by executing the switch.sh script
[0069] -. / switch.sh NGINX_CONFIG_NORMAL
[0070] #Stop and delete the backup server
[0071] -docker stop$SERVER_NAME-backup
[0072] -docker rm $SERVER_NAME-backup
[0073] #Delete the old image
[0074] -docker rmi $oldimage
[0075] In this embodiment, a new image is constructed according to the image deployment file, specifically:
[0076] Select a base image and set the working directory of the base image;
[0077] Configure the main program path and execution program of the base image, and set the resource file path of the base image.
[0078] In this embodiment, loads / alpine:3.8 is used as the base image and the working directory is set to / app / main. The main program is added to the working directory, executable permissions are granted to the main program, the resource folder is added to the working directory, and the working directory is set to $WORKDIR.
[0079] In this embodiment, the traffic switching operation is performed according to the new image to complete the service update, specifically:
[0080] Start the standby container, switch the service request to the standby container according to the traffic switching script file, and stop running the old container;
[0081] Start a new image and a new container, switch the script file according to the traffic, switch the service request to the new container, and stop running the standby container;
[0082] Clean up the backup containers and old images to complete the service update.
[0083] Please refer to Figure 2 , Figure 2 A flow chart of a traffic switching process provided by an embodiment of the present invention.
[0084] In this embodiment, the traffic switching script file is a switch.sh file, which includes first checking whether the parameters are empty, checking whether the accessory file exists, writing the new configuration file into the Nginx configuration file, checking whether the configuration file syntax is correct, and then restarting.
[0085] In this embodiment, the user submits the code and merges it into the pipeline branch. The code branch includes the user development branch and the pipeline branch. After the user submits the code, the user development branch code is merged into the pipeline branch, and the pipeline job is triggered.
[0086] In this embodiment, after the pipeline job is triggered, a new image is built according to the image deployment file, and a standby container is started, and the request is switched to the standby container according to the traffic switching script file; after the switch, the currently running old container is stopped; the new container is started using the new image and the normal port; by executing the traffic switching script file, all requests are switched to the new container; the standby container is stopped by executing the docker stop command; the standby container is cleaned up by executing the docker rm command; the old image is cleaned up by executing the docker rmi command; and the service update is completed.
[0087] In this embodiment, the gitlab platform is used as a code management tool to control the entire process of service updates, organically combining code management, integration pipelines, delivery pipelines, and non-stop updates. By monitoring pipeline branch codes, using code merges as triggers, building new images for traffic switching operations, and completing service updates, there is no need to install a container orchestration system or add additional resources, which is low cost and simple to maintain.
[0088] In a second aspect, an embodiment of the present invention further provides an update service device based on a docker environment, comprising:
[0089] Monitoring pipeline branch codes, and starting pipeline operations when development branch codes are merged with the pipeline branch codes;
[0090] When the pipeline job is started, a new image is constructed, and a traffic switching operation is performed according to the new image to complete the service update; the traffic switching operation includes container switching, container cleaning and image cleaning.
[0091] In this embodiment, in the monitoring pipeline branch code, when the development branch code is merged with the pipeline branch code, before starting the pipeline operation, it also includes:
[0092] Build the gitlab platform, register the platform operation service, and configure the integrated delivery tool according to the configuration file; the configuration file includes the instructions and parameters required to build the Docker image;
[0093] Write the image deployment file and traffic switching script file for application deployment. The image deployment file package is used to build a new image.
[0094] In this embodiment, a new image is constructed according to the image deployment file, specifically:
[0095] Select a base image and set the working directory of the base image;
[0096] Configure the main program path and execution program of the base image, and set the resource file path of the base image.
[0097] In this embodiment, the traffic switching operation is performed according to the new image to complete the service update, specifically:
[0098] Start the standby container, switch the service request to the standby container according to the traffic switching script file, and stop running the old container;
[0099] Start a new image and a new container, switch the script file according to the traffic, switch the service request to the new container, and stop running the standby container;
[0100] Clean up the backup containers and old images to complete the service update.
[0101] In a third aspect, an embodiment of the present invention further provides a computer device, comprising: a processor, a communication interface and a memory, wherein the processor, the communication interface and the memory are interconnected, wherein the memory stores an executable program code, and the processor is used to call the executable program code to execute the update service method based on the docker environment.
[0102] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer instructions, and when the computer instructions are executed by a processor, the update service method based on the docker environment is implemented.
[0103] In this embodiment, the gitlab platform is used as a code management tool to control the entire process of service updates, organically combining code management, integration pipelines, delivery pipelines and non-stop updates, and realizing automated deployment and non-stop update mechanisms based on code management. By monitoring pipeline branch codes, using code merging as a trigger point, a new image is built to perform traffic switching operations and complete service updates. There is no need to install a container orchestration system, add additional resources, and the cost is low and maintenance is simple.
[0104] The specific embodiments described above further illustrate the purpose, technical solutions and beneficial effects of the present invention. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. It is particularly pointed out that for those skilled in the art, any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention should be included in the scope of protection of the present invention.
Claims
1. A method for updating services based on a docker environment, characterized in that: include: Monitoring pipeline branch codes, and starting pipeline operations when development branch codes are merged with the pipeline branch codes; When the pipeline job is started, a new image is constructed, and a traffic switching operation is performed according to the new image to complete the service update; the traffic switching operation includes container switching, container cleaning and image cleaning.
2. The update service method based on the Docker environment as claimed in claim 1, characterized in that: In the monitoring pipeline branch code, when the development branch code is merged with the pipeline branch code, before starting the pipeline operation, it also includes: Build the gitlab platform, register the platform operation service, and configure the integrated delivery tool according to the configuration file; the configuration file includes the instructions and parameters required to build the Docker image; Write the image deployment file and traffic switching script file for application deployment. The image deployment file package is used to build a new image.
3. The update service method based on the Docker environment as claimed in claim 2, characterized in that: Build a new image according to the image deployment file, specifically: Select a base image and set the working directory of the base image; Configure the main program path and execution program of the base image, and set the resource file path of the base image.
4. The update service method based on the docker environment as claimed in claim 3, characterized in that: The traffic switching operation is performed according to the new image to complete the service update, specifically: Start the standby container, switch the service request to the standby container according to the traffic switching script file, and stop running the old container; Start a new image and a new container, switch the script file according to the traffic, switch the service request to the new container, and stop running the standby container; Clean up the backup containers and old images to complete the service update.
5. An update service device based on a docker environment, characterized in that: include: Monitoring pipeline branch codes, and starting pipeline operations when development branch codes are merged with the pipeline branch codes; When the pipeline job is started, a new image is constructed, and a traffic switching operation is performed according to the new image to complete the service update; the traffic switching operation includes container switching, container cleaning and image cleaning.
6. The update service device based on Docker environment as claimed in claim 5, characterized in that: In the monitoring pipeline branch code, when the development branch code is merged with the pipeline branch code, before starting the pipeline operation, it also includes: Build the gitlab platform, register the platform operation service, and configure the integrated delivery tool according to the configuration file; the configuration file includes the instructions and parameters required to build the Docker image; Write the image deployment file and traffic switching script file for application deployment. The image deployment file package is used to build a new image.
7. The update service device based on Docker environment as claimed in claim 6, characterized in that: Build a new image according to the image deployment file, specifically: Select a base image and set the working directory of the base image; Configure the main program path and execution program of the base image, and set the resource file path of the base image.
8. The update service device based on Docker environment as claimed in claim 7, characterized in that: The traffic switching operation is performed according to the new image to complete the service update, specifically: Start the standby container, switch the service request to the standby container according to the traffic switching script file, and stop running the old container; Start a new image and a new container, switch the script file according to the traffic, switch the service request to the new container, and stop running the standby container; Clean up the backup containers and old images to complete the service update.
9. A computer device, characterized in that: include: A processor, a communication interface and a memory, wherein the processor, the communication interface and the memory are connected to each other, wherein the memory stores an executable program code, and the processor is used to call the executable program code to execute the update service method based on the docker environment as described in any one of claims 1 to 4.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and when the computer instructions are executed by a processor, the update service method based on the Docker environment described in any one of claims 1 to 4 is implemented.
