在线下载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