一、做好镜像文件
1、编辑脚本 pull.sh
#!/bin/bash if [ $# -ne 1 ];then echo "Usage:$0 filename" exit 1 fi file=$1 if [ ! -f $file ];then echo "the $file is not a file" exit 2 fi count=0 while read line #使用read命令循环读取文件内容,并将读取的文件内容赋值给变量line do line1=${line:16} line2=${line1///_} line2=${line2/:/_} let count++ echo "now pull $count: $line " >> replicat.log docker pull $line >/dev/null if [ $? == 0 ];then echo "now tag $count: $line " >> replicat.log zyd_tag=${line/harbor/harbor-zyd1} docker tag $line $zyd_tag docker save -o $line2.tar $zyd_tag >/dev/null echo "now gzip $count: $line2" >> replicat.log echo "now tar " tar -czvf $line2.tar.gz $line2.tar >/dev/null rm -f $line2.tar echo "$count: $line successed" >> success.log docker rmi $line > /dev/null docker rmi $zyd_tag > /dev/null else echo "pull failed $count: $line" >> failed.log fi done <$file #“done <$file”将整个while循环的标准输入指向文件$file echo -e " totle $count lines read" >> replicat.log exit 0
拉取的镜像列表
[root@ops0002 zyd1230]# cat list3
harbor.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005
执行操作 nohup sh pull.sh list3 &
执行过程如下
读取一个文件列表,按行执行操作 过程如下 docker pull harbor.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005 拉取镜像 docker tag harbor.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005 harbor-zyd1.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005 镜像打标记 docker save -o enos_dataexplorer_tag_dataexplore_20191227_005.tar harbor-zyd1.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005 将镜像保存为 gz文件 tar -czvf enos_dataexplorer_tag_dataexplore_20191227_005.tar.gz enos_dataexplorer_tag_dataexplore_20191227_005.tar 将镜像文件压缩为 tar.gz文件 rm -f enos_dataexplorer_tag_dataexplore_20191227_005.tarr 删除文件 docker rmi harbor.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005 删除拉取的镜像 docker rmi harbor-zyd1.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005 删除打标机的镜像 -------------------------------- 最后留下的文件为 enos_dataexplorer_tag_dataexplore_20191227_005.tar.gz
2、下载文件
nginx配置下载
server { listen 8080; server_name localhost; autoindex on; autoindex_exact_size on; autoindex_localtime on; charset utf-8,gbk; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /data/images; #index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} }
3)以上功能使用 python脚本实现。
[root@harbor0001 images]# cat pull_image.py #!/usr/bin/env python # -*- coding: UTF-8 -*- import os,sys class MyError(BaseException): def __init__(self,msg): super().__init__() self.msg=msg def __str__(self): return '<%s>' %self.msg class Images: def cmd(self,comand): ret = os.system(comand) if ret != 0: with open('failure.txt', mode='a') as f: f.write(comand) f.write(' ') raise MyError('command error') def pull(self,image): '''拉取镜像''' comand = "docker pull %s" % image self.cmd(comand) def tag(self,source_image,desc_image): '''标记镜像''' comand = "docker tag %s %s" %(source_image,desc_image) self.cmd(comand) def save(self,image_tar,desc_image,image_tar_gz): '''保存镜像''' comand = "docker save -o %s %s" %(image_tar,desc_image) self.cmd(comand) comand2 = "tar -czvf %s %s" %(image_tar_gz,image_tar) self.cmd(comand2) comand3 = "rm -f %s" % image_tar self.cmd(comand3) def delete_image(self,source_image,desc_image): comand1 = "docker rmi %s" %source_image comand2 = "docker rmi %s" %desc_image self.cmd(comand1) self.cmd(comand2) if len(sys.argv) == 2: file = sys.argv[1] image = Images() with open(file,mode='r') as f: for line in f: image_error = line.split('/') image_error[-1] = image_error[-1].split(' ')[0] source_image = '/'.join(image_error) image_error[0] = 'harbor-test2.eniot.io' desc_image = '/'.join(image_error) image_tar = '_'.join(image_error) image_tar = image_tar.split(':') image_tar = '_'.join(image_tar) + ".tar" image_tar_gz = image_tar + ".gz" # print(image_tar) # print(image_tar_gz) # print(source_image) # print(desc_image) image.pull(source_image) image.tag(source_image, desc_image) image.save(image_tar, desc_image, image_tar_gz) image.delete_image(source_image, desc_image) with open('success.txt', mode='a') as f: f.write(line) else: print('input image file')
nohup python pull_image.py list &
二、推送镜像1
[root@harbor0001 ~]# cat a.sh #!/bin/bash docker pull harbor-cn2.eniot.io/${1} docker tag harbor-cn2.eniot.io/${1} harbor-zyd1.eniot.io/${1} docker push harbor-zyd1.eniot.io/${1}
三、推送镜像2
[root@harbor0001 images]# cat pull.sh #!/bin/bash if [ $# -ne 1 ];then echo "Usage:$0 filename" exit 1 fi file=$1 if [ ! -f $file ];then echo "the $file is not a file" exit 2 fi count=0 while read line #使用read命令循环读取文件内容,并将读取的文件内容赋值给变量line do let count++ echo "now pull $count: $line " >> replicat.log docker pull $line >/dev/null if [ $? == 0 ];then echo "now tag $count: $line " >> replicat.log india_tag=${line/cn2/india1} docker tag $line $india_tag echo " now push $india_tag" >> replicat.log docker push $india_tag if [ $? == 0 ]; then echo "------$count: $line successed" >> success.log echo " push $india_tag successed" >> replicat.log docker rmi $line > /dev/null docker rmi $india_tag > /dev/null else echo " push $india_tag failed" >> replicat.log echo " $count : push $india_tag failed" >> failed.log fi else echo "pull failed $count: $line" >> failed.log fi done <$file #“done <$file”将整个while循环的标准输入指向文件$file echo -e " totle $count lines read" >> replicat.log exit 0
2)该脚本内容用python已经改写
[root@harbor0001 files]# cat push_image.py #!/usr/bin/env python # -*- coding: UTF-8 -*- import os,sys class MyError(BaseException): def __init__(self,msg): super().__init__() self.msg=msg def __str__(self): return '<%s>' %self.msg class Images: def cmd(self,comand): ret = os.system(comand) if ret != 0: with open('failure.txt', mode='a') as f: f.write(comand) f.write(' ') raise MyError('command error') def load(self,image_tar_gz,image): '''加载镜像''' image_tar = image_tar_gz.split(".gz")[0] comand1 = "tar -xf %s" %(image_tar_gz) self.cmd(comand1) comand2 = "rm -f %s" % image_tar_gz self.cmd(comand2) comand3 = "docker load -i %s" % image_tar self.cmd(comand3) comand4 = "docker push %s" % image self.cmd(comand4) comand5 = "docker rmi %s" % image self.cmd(comand5) comand6 = "rm -f %s" % image_tar self.cmd(comand6) def main(self): list_files = os.listdir('.') for image_tar_gz in list_files: if image_tar_gz.endswith('tar.gz'): new_image = image_tar_gz.split(".tar.gz")[0].split("_") if len(new_image) == 4: image = new_image[0] + '/' + new_image[1] + "/" + new_image[2] + ':' + new_image[3] self.load(image_tar_gz,image) with open('success.txt', mode='a') as f: f.write(image) f.write(" ") if __name__ == '__main__': image = Images() image.main()
注意上面镜像 bug 。标记的镜像 不能包含 “_”。以及下载的时候,就已经做了标记
三、镜像标记为: tag_nginx_20200227_005 ,有且只有 一个 tag 时。推送时,再给镜像地址
下载镜像
#!/usr/bin/env python # -*- coding: UTF-8 -*- import os,sys class MyError(BaseException): def __init__(self,msg): super().__init__() self.msg=msg def __str__(self): return '<%s>' %self.msg class Images: def cmd(self,comand): ret = os.system(comand) if ret != 0: with open('failure.txt', mode='a') as f: f.write(comand) f.write(' ') raise MyError('command error') def pull(self,image): '''拉取镜像''' comand = "docker pull %s" % image self.cmd(comand) def save(self,image_tar,desc_image,image_tar_gz): '''保存镜像''' comand = "docker save -o %s %s" %(image_tar,desc_image) self.cmd(comand) comand2 = "tar -czvf %s %s" %(image_tar_gz,image_tar) self.cmd(comand2) comand3 = "rm -f %s" % image_tar self.cmd(comand3) def delete_image(self,source_image): comand = "docker rmi %s" %source_image self.cmd(comand) if len(sys.argv) == 2: file = sys.argv[1] image = Images() with open(file,mode='r') as f: for line in f: image_error = line.split('/') if image_error[0] != 'harbor-test1.eniot.io': continue image_error[-1] = image_error[-1].split(' ')[0] source_image = '/'.join(image_error) desc_image = '/'.join(image_error) image_tar = '_'.join(image_error) image_tar = image_tar.split(':') image_tar = '_'.join(image_tar) + ".tar" image_tar_gz = image_tar + ".gz" image.pull(source_image) image.save(image_tar,source_image,image_tar_gz) image.delete_image(source_image) with open('success.txt', mode='a') as f: f.write(line) else: print('input image file')
推送镜像
#!/usr/bin/env python # -*- coding: UTF-8 -*- import os,sys,time class MyError(BaseException): def __init__(self,msg): super().__init__() self.msg=msg def __str__(self): return '<%s>' %self.msg class Images: def cmd(self,comand): ret = os.system(comand) if ret != 0: with open('failure.txt', mode='a') as f: f.write(comand) f.write(' ') raise MyError('command error') def load(self,image_tar_gz,source_image,desc_image): '''加载镜像''' image_tar = image_tar_gz.split(".gz")[0] comand1 = "tar -xf %s" %(image_tar_gz) self.cmd(comand1) comand2 = "rm -f %s" % image_tar_gz self.cmd(comand2) comand3 = "docker load -i %s" % image_tar self.cmd(comand3) comand4 = "docker tag %s %s" %(source_image,desc_image) self.cmd(comand4) comand5 = "docker push %s" % desc_image self.cmd(comand5) comand6 = "docker rmi %s" % source_image comand7 = "docker rmi %s" % desc_image self.cmd(comand6) self.cmd(comand7) comand8 = "rm -f %s" % image_tar self.cmd(comand8) def main(self): list_files = os.listdir('.') for image_tar_gz in list_files: if image_tar_gz.endswith('tar.gz'): new_image = image_tar_gz.split(".tar.gz")[0] if new_image.count("tag") == 1: new_image = new_image.split("_tag_") harbor_adress = "harbor-test2.eniot.io" app = new_image[0].split("_") source_image = app[0] + "/" + app[1] + "/" + app[2] + ":tag_" + new_image[1] desc_image = harbor_adress + "/" + app[1] + "/" + app[2] + ":tag_" + new_image[1] self.load(image_tar_gz,source_image,desc_image) with open('success.txt', mode='a') as f: f.write(desc_image) f.write(" ") if __name__ == '__main__': image = Images() image.main()