规则

  1. 以下4种格式实际下载的是同一个内容,并且都会被tag为 nginx
    • nginx:alpine
    • library/nginx:alpine
    • [docker.io/nginx:alpine](http://docker.io/nginx:alpine)
    • docker.io/library/nginx:alpine

本规则中的镜像,可以在 dockerhub.azk8s.cn/library 下载到

  1. istio/mixer:latestdocker.io/istio/mixer:latest 下载到相同的内容,会被tag为 istio/mixer
    本规则中的镜像,可以在 dockerhub.azk8s.cn/istio/mixer:latest 下载到
  2. gcr.io/pause:latest 下载不到内容, k8s.gcr.io/pause:latestgcr.io/google_containers/pause:latest 可以下载到,并且会按完整的名称被tag
    本规则中的镜像,可以在 gcr.azk8s.cn/google_containers/pause:latest 下载到

代码

将以下代码保存为docker.py,然后运行docker.py pull <image>即可

  1. #!/usr/bin/python
  2. # coding=utf8
  3. import os
  4. import sys
  5. DEFAULT_DOCKER_HUB_URL = "docker.io"
  6. GCR_URL = "k8s.gcr.io"
  7. mirror_map = {
  8. "default": "dockerhub.azk8s.cn/library",
  9. DEFAULT_DOCKER_HUB_URL: "dockerhub.azk8s.cn",
  10. "gcr.io": "gcr.azk8s.cn",
  11. GCR_URL: "gcr.azk8s.cn"
  12. }
  13. url_default_namespace_map = {
  14. DEFAULT_DOCKER_HUB_URL: "library",
  15. GCR_URL: "google_containers"
  16. }
  17. def execute_sys_cmd(cmd):
  18. result = os.system(cmd)
  19. if result != 0:
  20. print(cmd + " failed.")
  21. sys.exit(-1)
  22. def usage():
  23. print("Usage: " + sys.argv[0] + " pull ${image}")
  24. print("Support the following image spec:")
  25. print("\tdocker.py pull nginx:alpine")
  26. print("\tdocker.py pull library/nginx:alpine")
  27. print("\tdocker.py pull docker.io/nginx:alpine")
  28. print("\tdocker.py pull docker.io/library/nginx:alpine")
  29. print("\tdocker.py pull docker.io/istio/mixer")
  30. print("\tdocker.py pull k8s.gcr.io/pause:latest")
  31. print("\tdocker.py pull gcr.io/google_containers/pause:latest")
  32. if __name__ == "__main__":
  33. if len(sys.argv) != 3 or sys.argv[1] != "pull":
  34. usage()
  35. sys.exit(-1)
  36. image = sys.argv[2]
  37. elements = image.split("/")
  38. URL = ""
  39. namespace = ""
  40. imageName = ""
  41. # nginx:alpine
  42. if len(elements) == 1:
  43. URL = DEFAULT_DOCKER_HUB_URL
  44. namespace = url_default_namespace_map[URL]
  45. imageName = elements[0]
  46. if len(elements) == 2:
  47. if "." in elements[0]:
  48. # docker.io/nginx:alpine
  49. # k8s.gcr.io/pause:latest
  50. URL = elements[0]
  51. if URL not in url_default_namespace_map:
  52. raise "Cannot find default namespace for " + image
  53. namespace = url_default_namespace_map[URL]
  54. imageName = elements[1]
  55. else:
  56. # library/nginx:alpine
  57. # istio/mixer
  58. URL = DEFAULT_DOCKER_HUB_URL
  59. namespace = elements[0]
  60. imageName = elements[1]
  61. if len(elements) == 3:
  62. URL = elements[0]
  63. namespace = elements[1]
  64. imageName = elements[2]
  65. if URL not in mirror_map:
  66. raise "Cannot find mirror for " + image
  67. newImage = mirror_map[URL] + "/" + namespace + "/" + imageName
  68. targetTagName = image
  69. if URL == DEFAULT_DOCKER_HUB_URL:
  70. if namespace == url_default_namespace_map[URL]:
  71. targetTagName = imageName
  72. else:
  73. targetTagName = namespace + "/" + imageName
  74. print("-- pulling image %s --" % image)
  75. cmd = "docker pull {image}".format(image=newImage)
  76. print("\t" + cmd)
  77. execute_sys_cmd(cmd)
  78. cmd = "docker tag {newImage} {image}".format(newImage=newImage, image=targetTagName)
  79. print("\t" + cmd)
  80. execute_sys_cmd(cmd)
  81. cmd = "docker rmi {newImage}".format(newImage=newImage)
  82. print("\t" + cmd)
  83. execute_sys_cmd(cmd)
  84. print("-- {image} pulling done --".format(image=image))
  85. sys.exit(0)

```