Make the project code into a mirror image, and then save it as a tar archive file. The whole process

1. Write make.sh and Dockerfile, requirements.txt for the project.

1.make.sh file template

#!/usr/bin/env bash
echo "开始制作镜像..."
#用日期标记镜像版本
image_tag=`date +%Y%m%d` #_%H%M
echo "当前时间:$image_tag"
#制作镜像,使用的腾讯云(用的当前目录下的Dockerfile)
docker build -t ccr.ccs.tencentyun.com/my_test/my_test_project:v${image_tag} . 

echo "制作镜像成功!"
#登录腾讯云账号
docker login --username=my_user_name -p=my_password ccr.ccs.tencentyun.com
#向腾讯云推送制作好的镜像
docker push ccr.ccs.tencentyun.com/my_test/my_test_project:v${image_tag}

echo "镜像版本保存"
#将刚才制作好的镜像版本标识为latest
docker tag ccr.ccs.tencentyun.com/my_test/my_test_project:v${image_tag} ccr.ccs.tencentyun.com/my_test/my_test_project:latest
#向腾讯云推送latest镜像
docker push ccr.ccs.tencentyun.com/my_test/my_test_project:latest

echo "删除本地镜像"
docker rmi ccr.ccs.tencentyun.com/my_test/my_test_project:v${image_tag}
docker rmi ccr.ccs.tencentyun.com/my_test/my_test_project:latest

2.Dockerfile template

#镜像基于某个python版本镜像
FROM ccr.ccs.tencentyun.com/my_test/python_base:data_v20230526

#设置时区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN mkdir -p /usr/src/app
ENV PYTHONPATH=/usr/src/app
WORKDIR /usr/src/app


#复制当前目录下所有文件到镜像里
COPY . /usr/src/app
COPY requirements.txt /usr/src/app/requirements.txt
#为镜像安装需要的pyton包
RUN pip install -r /usr/src/app/requirements.txt
CMD python

3.requirements.txt (the python package required by the project, and the version of the python package)

#列如
numpy==1.14.5
pandas==0.21.0
lxml==4.2.3
SQLAlchemy==1.2.10
SciPy==1.1.0
pymysql==0.9.2
requests==2.19.1
pyyaml==3.13
xlrd==1.1.0
arrow==0.13.2
statsmodels==0.9.0
patsy==0.5.0
redis==2.10.6

2. Put the project source code file into gitlab.

3. Use jenkins to automatically package the mirror image when the project is updated.

1. Create a new project on jenkins.
Option: Building a Freestyle Software Project

2. Configure source code management.
Select git for source code management, and fill in the gitlab link of the project for Repository URL.
Credentials sets the account password of the git account.
Specified branch (if it is empty, it means any): If the specified branch column specifies master, fill in */master.

3. Configure build triggers.
Select polling SCM, fill in the schedule * * * * *
(that is, judge whether the git code is updated every minute, if it is updated, pull the latest code, and then run the build step automatically)

4. Configure the build.
Add a build step to select Execute shell script on remote host using ssh.
Then the SSH site selects the Linux host responsible for packaging.
Command writes:

#登录腾讯云docker账号
sh /mypath/docker_login.sh
cd /mypath/jenkins_data/workspace/my_test_project
#为make.sh文件赋执行权限
chmod 777 make.sh
#运行make.sh(用最新代码制作镜像,并推送到腾讯云仓库)
./make.sh

4. Save the image as a tar archive.

#拉下最新的项目镜像
docker push ccr.ccs.tencentyun.com/my_test/my_test_project:latest
#把镜像保存为tar归档文件
docker save -o my_test_project_v20230526.tar ccr.ccs.tencentyun.com/my_test/my_test_project:latest

5. Use archive files. (Usually save and load will do)
1.docker load imports the image exported using the docker save command

docker load < my_test_project_v20230526.tar 

2. docker import creates a mirror image from an archive.

docker import  my_test_project_v20230526.tar  new_name:new_version

3. The difference between laod and import.
docker import is generally used in conjunction with docker export.
docker load is generally used in conjunction with docker save.
If you need to import the entire image, including the image hierarchy and metadata, you should use the docker load command.
If you just need to import the container's file system and convert it into a new image, you can use the docker import command.

5. You can also upload the tar file to the Tencent Cloud cos object storage for subsequent use.

Guess you like

Origin blog.csdn.net/qq_44821149/article/details/130887990