博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
查找最快的docker镜像
阅读量:4049 次
发布时间:2019-05-25

本文共 5276 字,大约阅读时间需要 17 分钟。

:https://github.com/silenceshell/docker_mirror

查找国内最快的docker镜像源。

Python编写,需要root权限,支持Ubuntu/Deepin/Centos7/Arch,其他操作系统需要补充。
Ubuntu/CentOS/Deepin/Arch支持docker community.

使用方法非常简单,下载 docker_mirror.py 文件到本地,执行下面的命令即可,脚本会自动从Azure, Tencent, Aliyun, Netease, ustc尝试下载registry:2镜像,并计算使用的时间;按使用时间最少的镜像设置docker配置,并重启docker进程。

sudo apt install python-pipsudo pip install docker -i https://mirrors.aliyun.com/pypi/simplesudo python ./docker_mirror.py

docker_mirror.py

#!/usr/bin/python# coding=utf8from __future__ import print_functionfrom __future__ import unicode_literalsimport platformimport reimport timeimport osimport dockerimport jsonmirror_prefix = "--registry-mirror="mirrors = {
"azure": "http://dockerhub.azk8s.cn", "tencent": "https://mirror.ccs.tencentyun.com", "netease": "http://hub-mirror.c.163.com", "ustc": "https://docker.mirrors.ustc.edu.cn", "aliyun": "https://2h3po24q.mirror.aliyuncs.com" # use your own aliyun mirror url instead.}docker_config_map = {
"Ubuntu": {
"config": "/etc/default/docker", "prefix": "DOCKER_OPTS=" }, "CentOS Linux": {
"config": "/etc/sysconfig/docker", "prefix": "OPTIONS=" }, "Deepin": {
"config": "/etc/default/docker", "prefix": "DOCKER_OPTS=" }}docker_ce_config_map = {
"Ubuntu": {
"config": "/etc/docker/daemon.json", }, "Deepin": {
"config": "/etc/docker/daemon.json", }, "Arch": {
"config": "/etc/docker/daemon.json", }}def get_dist(): return platform.linux_distribution()[0]def get_config(dist): return docker_config_map[dist]["config"]def get_config_ce(dist): return docker_ce_config_map[dist]["config"]def get_prefix(dist): return docker_config_map[dist]["prefix"]def get_new_options(option, mirror): option = option.strip() quota = option[len(option) - 1] if mirror_prefix in option: r1 = re.compile('[\'\"]') results1 = r1.split(option) r2 = re.compile('[\s]') results2 = r2.split(results1[1].strip()) for i in range(len(results2)): if results2[i].startswith(mirror_prefix): results2[i] = mirror_prefix + mirror new_option = results1[0] + quota + " ".join(results2) + quota else: new_option = option[:-1] + " " + mirror_prefix + mirror + quota new_option += "\n" return new_optiondef execute_sys_cmd(cmd): result = os.system(cmd) return resultdef set_docker_config(mirror): dist = get_dist() docker_config = get_config(dist) prefix = get_prefix(dist) new_line = "" options = "" with open(docker_config, "r") as f: for line in f: if line.startswith(prefix): options = get_new_options(line, mirror) else: new_line += line if options == "": options = prefix + "\'" + mirror_prefix + mirror + "\'" with open(docker_config, "w") as f: f.write(new_line) f.writelines(options)def set_docker_config_ce(mirror): dist = get_dist() docker_config = get_config_ce(dist) config_dict={
} if os.path.exists(docker_config) != True: # if file not exist, create it first. os.mknod(docker_config, 0o644) else: with open(docker_config, "r") as f: config_dict = json.load(f) config_dict[u'registry-mirrors'] = [mirror] with open(docker_config, "w") as f: json.dump(config_dict, f)def restart_docker_daemon(): execute_sys_cmd("systemctl restart docker")def get_speed(mirror, mirror_url): client = docker.from_env() version = client.version()[u'Version'] if "ce" in version: set_docker_config_ce(mirror_url) else: set_docker_config(mirror_url) restart_docker_daemon() # try to delete busybox image in case. execute_sys_cmd("docker rmi registry:2 -f 1> /dev/null 2>&1") print("pulling registry:2 from {mirror}".format(mirror=mirror)) begin_time = time.time() execute_sys_cmd("docker pull registry:2 1> /dev/null 2>&1") end_time = time.time() cost_time = end_time - begin_time print("mirror {mirror} cost time: {cost_time}\n".format(mirror=mirror, cost_time=cost_time)) # delete centos images every time. execute_sys_cmd("docker rmi registry:2 -f 1> /dev/null 2>&1") return cost_time #return 204800 / cost_timeif __name__ == "__main__": print("restart docker daemon") restart_docker_daemon() max_speed = 0 total_time = 0 restart_count = 0 best_mirror = "" best_mirror_url = "" for k, v in mirrors.items(): cost_time = get_speed(k, v) speed = 204800 / cost_time if speed > max_speed: max_speed = speed best_mirror = k best_mirror_url = v restart_count += 1 total_time += cost_time if restart_count >= 2 and total_time < 60: #to avoid the error of docker daeom: Start request repeated too quickly print("oh.. docker daemon restart too quickly, have a rest") restart_count = 0 total_time = 0 time.sleep(60-total_time) print("best mirror is: {mirror}, set docker config and restart docker daemon now.".format(mirror=best_mirror)) set_docker_config(best_mirror_url) restart_docker_daemon()

转载地址:http://qinci.baihongyu.com/

你可能感兴趣的文章
java 不用递归写tree
查看>>
springboot2 集成Hibernate JPA 用 声明式事物
查看>>
fhs-framework jetcache 缓存维护之自动清除缓存
查看>>
SpringBoot 动态编译 JAVA class 解决 jar in jar 的依赖问题
查看>>
fhs_framework springcloud使用统一的控制器来接收rpc调用请求教程,无需每个rpc接口都写控制器
查看>>
fhs-framework springboot mybatis 解决表关联查询问题的关键方案-翻译服务
查看>>
Springboot + easyui + mybatis 高级搜索功能实现
查看>>
k8s 踩坑笔记
查看>>
SpringCloud Seata Nacos 整合教程和坑
查看>>
nacos 本地覆盖远程 本地优先
查看>>
java 查询内存泄漏
查看>>
httpclient4.5 绕过ssl证书校验 -看别人文章解决不了的,看下我这个
查看>>
基于webpack的vue语法糖实现思路
查看>>
jenkins流水线脚本 从sonar代码扫描,到maven构建,到docker打包,到k8s发布
查看>>
微服务项目占用内存过多机器扛不住怎么办?服务allinone设计
查看>>
项目快速开发经验
查看>>
LambdaQueryWrapper动态加过滤条件 动态Lambda(首发)
查看>>
javascript 大数值进行按位运算
查看>>
jacob 设置 插入word 的图片大小
查看>>
html2image html转换为图片 生成快照 java
查看>>