docker镜像文件导入与导出 , 支持批量

 

 

1. 查看镜像id

sudo docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
quay.io/calico/node      v1.0.1              c70511a49fa1        6 weeks ago         257 MB
hello-world              latest              48b5124b2768        2 months ago        1.84 kB
quay.io/coreos/flannel   v0.7.0              63cee19df39c        2 months ago        73.8 MB
quay.io/calico/cni       v1.5.5              ada87b3276f3        2 months ago        67.1 MB

 

2. 选择要打包的镜像,执行打包命令

sudo docker save -o quay.io-calico-node-1.tar quay.io/calico/node 

会在当前目录下生成导出文件xxx.tar,然后将此文件下载到本地

 

3. 在开发环境导入上述打包的镜像

docker load -i quay.io-calico-node-1.tar
复制代码
0a43edc59c00: Loading layer 27.59 MB/27.59 MB
69a5574b2581: Loading layer 3.636 MB/3.636 MB
fb0933709f36: Loading layer 3.913 MB/3.913 MB
7384abd120f5: Loading layer 3.859 MB/3.859 MB
e34911610de0: Loading layer 27.06 MB/27.06 MB
d6ec327c8cbe: Loading layer 6.656 kB/6.656 kB
Loaded image ID: sha256:ada87b3276f307a6b1b1ada15820b6c9842fd839fe5cc46ad5db8af81f7fd271
复制代码

 

至此,可以使用本地镜像了!

4. 批量导入导出镜像工具

①导出镜像:

import re
import os
import subprocess

if __name__ == "__main__":
    os.system('rm -rf /tmp/xfleet')
    os.system('mkdir -p /tmp/xfleet')
    p = subprocess.Popen('docker images', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    for line in p.stdout.readlines():

        # 此处的正则表达式是为了匹配镜像名以ufleet为开头的镜像
        # 实际使用中根据需要自行调整
        m = re.match(r'(^ufleet[^\s]*\s*)\s([^\s]*\s)', line.decode("utf-8"))
        if not m:
            continue

        # 镜像名
        iname = m.group(1).strip(' ')
        # tag
        itag = m.group(2)

        # tar包的名字
        tarname = iname.split('/')[-1]
        print(tarname)
        tarball = tarname + '.tar'
        ifull = iname + ':' + itag
        #save
        cmd = 'docker save -o ' + tarball + ' ' + ifull
        os.system(cmd)

        # 将tar包放在临时目录
        os.system('mv %s /tmp/xfleet/'%tarball)


    retval = p.wait()

  ②导入镜像:

import os
import sys

if __name__ == "__main__":
    tarball = sys.argv[1]
    print(tarball)

    workdir = '/tmp/xfleet-images'
    os.system('rm -rf %s'%workdir)
    os.system('mkdir -p %s'%workdir)
    os.system('tar -zxvf %s -C %s'%(tarball, workdir))

    os.chdir(workdir)
    files = os.listdir(workdir)
    for filename in files:
        print(filename)
        os.system('docker load -i %s'%filename)

  

小工具参考资料:http://www.cnblogs.com/ksir16/p/8865525.html

转载于:https://www.cnblogs.com/cpw6/p/9845559.html

你可能感兴趣的:(docker镜像文件导入与导出 , 支持批量)