在线下载Docker Hub镜像,打成tar包下载

在线下载Docker Hub镜像,打成tar包下载

演示站: http://8.130.31.155:5000/

需要有docker环境

import docker
import io
import logging
from flask import Flask, request, send_file, render_template

app = Flask(__name__)
logging.basicConfig(level=logging.INFO)

@app.route('/')
def home():
    return render_template("index.html")

@app.route('/download', methods=['POST'])
def download_image():
    # Get input parameters
    image_name_all = request.form.get('image_name', '')
    if "docker pull " in image_name_all:
      image_name_all = image_name_all.replace("docker pull ","")
    if ":" in str(image_name_all):
      image_name_all = image_name_all.split(":")
      image_name = image_name_all[0]
      tag = image_name_all[1]
    else:
      image_name = image_name_all
      tag = "latest"
    #image_name= image_name.replace("%2F","/")
    print(image_name)
    #logging.info(image_name,tag)

    # Connect to Docker engine
    client = docker.from_env()

    # Pull image from Docker Hub
    try:
        progress = client.api.pull(f'{image_name}:{tag}')
    except docker.errors.APIError as e:
        logging.error(f"Failed to pull image {image_name}:{tag}: {e}")
        return "Failed to download the image. Please try again later."

    # Save image to a file-like object
    image_file = io.BytesIO()
    for chunk in client.images.get(f"{image_name}:{tag}").save():
        image_file.write(chunk)
    image_file.seek(0)

    # Return the image file as a download attachment
    attachment_filename = f'{image_name}_{tag}.tar'
    mimetype = 'application/tar'
    return send_file(
        image_file,
        as_attachment=True,
        attachment_filename=attachment_filename,
        mimetype=mimetype
    )

if __name__ == '__main__':
    app.run(host="0.0.0.0",debug=True)

完整的代码在github上面
https://github.com/chenyouxiu/docker_image_download
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值