Docker-批量打包镜像

在生产业务中,批量迁移镜像是必不可少的,
有人说写脚本,会不会太麻烦了!!!!

方法一

由于在内网环境或网速较慢的时候,下载镜像比较慢,所以可以将镜像打包成文件进行拷贝。我写了一个批量打包镜像的语句。
批量打包镜像:

# docker save $(docker images | grep -v REPOSITORY | awk 'BEGIN{OFS=":";ORS=" "}{print $1,$2}') -o haha.tar

将机器上的所有镜像打包到haha.tar文件里面。
加载镜像:

# docker load -i haha.tar

然后docker images就可以看到拷贝过来的镜像了
在网上搜集资料,发现命令用不了,就像这个
https://blog.csdn.net/qq_28451255/article/details/80564433

看来想要批量打包镜像,只用通过脚本实现了
下面是自己的操作记录,欢迎广大博友提建议。
使用正则表达式

看一下镜像

[root@master ~]# docker images
REPOSITORY                                                                    TAG                 IMAGE ID            CREATED             SIZE
k8s.gcr.io/kube-proxy                                                         v1.15.3             232b5c793146        4 weeks ago         82.4MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy                v1.15.3             232b5c793146        4 weeks ago         82.4MB
k8s.gcr.io/kube-apiserver                                                     v1.15.3             5eb2d3fc7a44        4 weeks ago         207MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver            v1.15.3             5eb2d3fc7a44        4 weeks ago         207MB
k8s.gcr.io/kube-scheduler                                                     v1.15.3             703f9c69a5d5        4 weeks ago         81.1MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler            v1.15.3             703f9c69a5d5        4 weeks ago         81.1MB
k8s.gcr.io/kube-controller-manager                                            v1.15.3             e77c31de5547        4 weeks ago         159MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager   v1.15.3             e77c31de5547        4 weeks ago         159MB
k8s.gcr.io/coredns                                                            1.3.1               eb516548c180        8 months ago        40.3MB
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns                   1.3.1               eb516548c180        8 months ago        40.3MB
hello-world                                                                   latest              fce289e99eb9        8 months ago        1.84kB
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd                      3.3.10              2c4adeb21b4f        9 months ago        258MB
k8s.gcr.io/etcd                                                               3.3.10              2c4adeb21b4f        9 months ago        258MB
registry.cn-shanghai.aliyuncs.com/gcr-k8s/flannel                             v0.10.0-amd64       b949a39093d6        17 months ago       44.6MB
quay.io/coreos/flannel                                                        v0.10.0-amd64       f0fad859c909        20 months ago       44.6MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64               3.1                 da86e6ba6ca1        21 months ago       742kB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause                     3.1                 da86e6ba6ca1        21 months ago       742kB
k8s.gcr.io/pause                                                              3.1                 da86e6ba6ca1        21 months ago       742kB
[root@master ~]# 

这个命令是把镜像整行显示,中间留空格,然后粘贴复制

[root@master ~]# docker images | grep -v REPOSITORY | awk 'BEGIN{OFS=":";ORS=" "}{print $1,$2}'
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.15.3 k8s.gcr.io/kube-proxy:v1.15.3 k8s.gcr.io/kube-apiserver:v1.15.3 registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.15.3 k8s.gcr.io/kube-scheduler:v1.15.3 registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.15.3 k8s.gcr.io/kube-controller-manager:v1.15.3 registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.15.3 k8s.gcr.io/coredns:1.3.1 registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.3.1 hello-world:latest k8s.gcr.io/etcd:3.3.10 registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.10 registry.cn-shanghai.aliyuncs.com/gcr-k8s/flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.10.0-amd64 k8s.gcr.io/pause:3.1 registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1

然后按以下格式进行打包

docker save -o 包名.tar 镜像1 镜像2 镜像3 …

你可能感兴趣的:(Docker)