“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”
preface
Gin framework container deployment is not like the front-end to upload static files and restart the Nginx container to take effect, we need to package the code in the running environment, put the code directly into the image, and then replace the running image.
Operation in advance
The environment we deploy is Raspberry PI, arm architecture. Docker will automatically judge the system environment when pulling the image, and the image will correspond to the system, so there will be no problem. But we need to pull the project to Raspberry PI first, enter the project to package the image, run the container, The purpose of this is so that when CI is repeated, there will be a pre-run container to operate, no error. Git clone *****
Enter the project folder, package the image, and run the container using the following:
cd my-gin-web/
docker build -t my_gin_web .
docker run -d --name my_gin_web -p 8083:8080 my_gin_web
Copy the code
The host port was changed to 8083 because another container was occupying the host directory.
CI file writing
The project is also split into two branches: develop and Master, where the code only triggers deployment tasks. Create gitlab-ci.yaml in the project root directory and type:
Stages: - deploy # Task stage sequence, only one deploy deployment stage is written - docker build -t my_gin_web:$CI_JOB_ID. # CI_JOB_ID is an environment variable. -docker stop my_gin_web - docker rm my_gin_web - docker run -d --name my_gin_web -p 8083:8080 My_gin_web :$CI_JOB_ID only: - master # Specifies that this task is performed only on the master branch. Tags: -b4master # Specifies the runner to perform the task. The runner will be prompted to fill in the tag when installing the runner.Copy the code
Submit the CI script to Gitlab and merge it into the Master branch:
conclusion
The project is successfully deployed, and the task is deployed in three minutes. Docker will suspend service for a few seconds when it is started. In commercial projects, multiple containers will be used to update using rolling update method. There is no need to affect the online project, now connect to mysql database ~ online