其实从一个仓库迁移到另一个仓库是很容易的,大致如下:

  1. docker login登录新仓库,如:docker login register.linkscue.com
  2. docker tag将image的命令变更为新仓库起始的名字,如:alpine:3.8register.linkscue.com/scue/alpine:3.8
  3. docker push新Tag的名字的镜像即可,如:docker push register.linkscue.com/scue/alpine:3.8
  4. 单个镜像的操作,完~

有了这个基础前提,我们看一下批量转换的方式,比如批量从 k8s.gcr.io 导到 register.linkscue.com/scue

1)查看docker输出format支持的key

以docker images输出的格式来说对于批量执行任务的shell处理不是很方便处理(当然也可以使用awk)提取,但不是很友好,可以考虑docker images --format格式化输出

$ docker images --format '{{json .}}' | head -n 1
{"Containers":"N/A","CreatedAt":"2019-08-19 19:26:56 +0800 CST","CreatedSince":"4 weeks ago","Digest":"\u003cnone\u003e","ID":"ed8adf767eeb","Repository":"k8s.gcr.io/kube-proxy","SharedSize":"N/A","Size":"82.1MB","Tag":"v1.14.6","UniqueSize":"N/A","VirtualSize":"82.11MB"}

可以看到,我比较关注的是RepositoryTag,它俩合起来就是k8s.gcr.io/kube-proxy:v1.14.6

2)批量转换上传脚本

有了刚刚的一个format格式输出的前提,要批量转换上传就很简单了,只需要写一个upload_images.sh脚本:

#!/bin/bash

readonly old_repo=k8s.gcr.io
readonly new_repo=register.linkscue.com/scue

for image in $(docker images --format '{{.Repository}}:{{.Tag}}'); do
	name=${image##*/}
	new_img=${new_repo}/${name}
	echo "Processing ${image} -> ${new_img}"
	docker tag ${image} ${new_img}
	docker push ${new_img}
done

测试一下脚本效果:

$ ./upload_images.sh
Processing k8s.gcr.io/kube-proxy:v1.14.6 -> register.linkscue.com/scue/kube-proxy:v1.14.6
The push refers to repository [register.linkscue.com/scue/kube-proxy]
b5d55ee513d6: Pushed
15c9248be8a9: Pushed
fe9a8b4f1dcc: Pushed
v1.14.6: digest: sha256:c2f5d10f93d2269b047c1ef9af6d47cc94c1bd8d06f7ae77f3133816651f74f7 size: 951
Processing k8s.gcr.io/kube-apiserver:v1.14.6 -> register.linkscue.com/scue/kube-apiserver:v1.14.6
...

可以看到成功上传了~

3)验证一下新的仓库

$ docker rmi register.linkscue.com/scue/kube-proxy:v1.14.6
Untagged: register.linkscue.com/scue/kube-proxy:v1.14.6
Untagged: register.linkscue.com/scue/kube-proxy@sha256:c2f5d10f93d2269b047c1ef9af6d47cc94c1bd8d06f7ae77f3133816651f74f7
$ docker pull register.linkscue.com/scue/kube-proxy:v1.14.6
v1.14.6: Pulling from scue/kube-proxy
Digest: sha256:c2f5d10f93d2269b047c1ef9af6d47cc94c1bd8d06f7ae77f3133816651f74f7
Status: Downloaded newer image for register.linkscue.com/scue/kube-proxy:v1.14.6
register.linkscue.com/scue/kube-proxy:v1.14.6

OK,一切测试完成~ ;-)