Docker批量拉取和推送的Linux Shell脚本

一、简介

今天给小伙伴们分享一个Docker批量拉取和推送到项目地镜像仓库的shell脚本,可以批量进行拉取和推送,比一个一个手动拉取和推送方便不少,话不多说,直接入正题:

【a】创建脚本文件auto-pull-and-push-images.sh

vim auto-pull-and-push-images.sh

将下面的脚本内容复制进去:

#!/bin/bash
#新镜像标签:默认取当前时间作为标签名
imageNewTag=`date +%Y%m%d-%H%M%S`
#镜像仓库地址
registryAddr="192.168.35.105:5000/lyzhxg/"

#循环读取images.txt,并存入list中
n=0

for line in $(cat images.txt | grep ^[^#])
do
	list[$n]=$line
	((n+=1))
done

echo "需推送的镜像地址如下:"
for variable in ${list[@]}
do
	echo ${variable}
done

for variable in ${list[@]}
do
	#下载镜像
	echo "准备拉取镜像: $variable"
	docker pull $variable
	
	# #获取拉取的镜像ID
	imageId=`docker images -q $variable`
	echo "[$variable]拉取完成后的镜像ID: $imageId"
	
	#获取完整的镜像名
	imageFormatName=`docker images --format "{{.Repository}}:{{.Tag}}:{{.ID}}" |grep $variable`
	echo "imageFormatName:$imageFormatName"

	#删掉最后一个/及其左边的字符串
	#如:192.168.35.126:5000/lyzhxg/bks/ly-sm-yxxt-ui:20200324-153539:0beed7b2fa8c  ->  ly-sm-yxxt-ui:20200324-153539:0beed7b2fa8c
	repository=${imageFormatName##*/}
	echo "repository :$repository"
	
	#删掉第一个:及其右边的字符串
	#如:ly-sm-yxxt-ui:20200324-153539:0beed7b2fa8c -> ly-sm-yxxt-ui
	repository=${repository%%:*}

	echo "新镜像地址: $registryAddr$repository:$imageNewTag"
	
	#重新打镜像标签
	docker tag $imageId $registryAddr$repository:$imageNewTag
	
	# #推送镜像
	docker push $registryAddr$repository:$imageNewTag
done

【b】images.txt:

vim images.txt

输入需要拉取和推送的镜像地址:

#基础服务
192.168.2.78:5000/dev/ly-sm-unauth-svc:4.2
192.168.2.78:5000/dev/ly-sm-bpm-expansion-ui:4.2

注意:每一行写一个镜像地址,因为脚本是循环读取images.txt文件中的每一行来获取镜像地址的 。

准备完这两个文件后,如下图所示: 

【c】测试:

执行脚本:

./auto-pull-and-push-images.sh

注意:执行脚本需要有可执行权限,可以使用下面的命令赋予可执行权限:

chmod 777 auto-pull-and-push-images.sh 
或者
chmod u+x auto-pull-and-push-images.sh 

【d】执行日志:

[root@node1 test01]# ./auto-pull-and-push-images.sh 
需推送的镜像地址如下:
#基础服务
192.168.2.78:5000/dev/ly-sm-unauth-svc:4.2
192.168.2.78:5000/dev/ly-sm-bpm-expansion-ui:4.2
准备拉取镜像: #基础服务
invalid reference format
[#基础服务]拉取完成后的镜像ID: 
imageFormatName:
repository :
新镜像地址: 192.168.35.105:5000/lyzhxg/:20200330-145604
"docker tag" requires exactly 2 argument(s).
See 'docker tag --help'.

Usage:  docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
invalid reference format
准备拉取镜像: 192.168.2.78:5000/dev/ly-sm-unauth-svc:4.2
4.2: Pulling from dev/ly-sm-unauth-svc
bdf0201b3a05: Already exists 
484b5c92c2b2: Already exists 
d77d79454db4: Already exists 
5a60d6145c09: Already exists 
17b0bf5a3271: Already exists 
e796987ed595: Already exists 
1877f24e6b98: Already exists 
e4642025412a: Pull complete 
Digest: sha256:4a6f5b1f76787a8433eaa28dd3baa11c88b24f9f9804128fe1287b60c931c3a0
Status: Downloaded newer image for 192.168.2.78:5000/dev/ly-sm-unauth-svc:4.2
[192.168.2.78:5000/dev/ly-sm-unauth-svc:4.2]拉取完成后的镜像ID: 28bf8a35801a
imageFormatName:192.168.2.78:5000/dev/ly-sm-unauth-svc:4.2:28bf8a35801a
repository :ly-sm-unauth-svc:4.2:28bf8a35801a
新镜像地址: 192.168.35.105:5000/lyzhxg/ly-sm-unauth-svc:20200330-145604
The push refers to a repository [192.168.35.105:5000/lyzhxg/ly-sm-unauth-svc]
b02da9392e12: Layer already exists 
77e5fe1379bd: Layer already exists 
82d4f7294cc9: Layer already exists 
caed85d2f965: Layer already exists 
501ba7f835da: Layer already exists 
19821cef557a: Layer already exists 
7a5b1b6f8771: Layer already exists 
a464c54f93a9: Layer already exists 
20200330-145604: digest: sha256:4a6f5b1f76787a8433eaa28dd3baa11c88b24f9f9804128fe1287b60c931c3a0 size: 1995
准备拉取镜像: 192.168.2.78:5000/dev/ly-sm-bpm-expansion-ui:4.2
4.2: Pulling from dev/ly-sm-bpm-expansion-ui
d9aaf4d82f24: Already exists 
d5ee1d58bc2b: Already exists 
546386b6129d: Already exists 
9841cc274ae3: Already exists 
513e4ea81866: Already exists 
741a0c892709: Already exists 
b8de0db39c27: Already exists 
13491ab36883: Already exists 
e0b8f13d740b: Already exists 
e70a3154ec3f: Already exists 
0ba8cabe3cb5: Already exists 
5f1b7262c6e6: Pull complete 
5a6fd0d45865: Pull complete 
Digest: sha256:bd66f37354061fae86a43f2686404d1f1eeec849050f6f8657ab3e2368b9aa99
Status: Downloaded newer image for 192.168.2.78:5000/dev/ly-sm-bpm-expansion-ui:4.2
[192.168.2.78:5000/dev/ly-sm-bpm-expansion-ui:4.2]拉取完成后的镜像ID: a7dbe9d8166b
imageFormatName:192.168.2.78:5000/dev/ly-sm-bpm-expansion-ui:4.2:a7dbe9d8166b
repository :ly-sm-bpm-expansion-ui:4.2:a7dbe9d8166b
新镜像地址: 192.168.35.105:5000/lyzhxg/ly-sm-bpm-expansion-ui:20200330-145604
The push refers to a repository [192.168.35.105:5000/lyzhxg/ly-sm-bpm-expansion-ui]
95bb7c95408a: Layer already exists 
8453f0eac2c0: Layer already exists 
9d20f94a2ac9: Layer already exists 
76d42c49a4b7: Layer already exists 
1ee7f41e8f64: Layer already exists 
2e91b69704ca: Layer already exists 
98ee4140c3a9: Layer already exists 
acd8813714a5: Layer already exists 
34f92ee5b1dc: Layer already exists 
9eeca9c73db1: Layer already exists 
855fa15376da: Layer already exists 
8e36976219db: Layer already exists 
cf516324493c: Layer already exists 
20200330-145604: digest: sha256:bd66f37354061fae86a43f2686404d1f1eeec849050f6f8657ab3e2368b9aa99 size: 3034

然后我们使用docker images查看刚刚推送过去的镜像:

可见,同时成功构建了两个镜像,实际项目中可以同时构建多个镜像,不需要构建的在images.txt中用#注释掉即可掉过本次构建,比起一个一个构建,显然方便不少。

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页
实付 19.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值