18.5 批量管理镜像

在之前章节中,笔者介绍了如何对单个镜像进行上传、下载的操作。有时候,本地镜像很多,逐个打标记进行操作将十分浪费时间。这里将以批量上传镜像为例,介绍如何利用脚本实现对镜像的批量化处理。

1.批量上传指定镜像

可以使用下面的push_images.sh脚本,批量上传本地的镜像到注册服务器中,默认是本地注册服务器127.0.0.1:5000,用户可以通过修改registry=127.0.0.1:5000这行来指定目标注册服务器:


  1. #!/bin/sh
  2. # This script will upload the given local images to a registry server ($registry
  3. is the default value).
  4. # See: https://github.com/yeasy/docker_practice/blob/master/_local/push_images.sh
  5. # Usage: push_images image1 [image2...]
  6. # Author: yeasy@github
  7. # Create: 2014-09-23
  8. #The registry server address where you want push the images into
  9. registry=127.0.0.1:5000
  10. ### DO NOT MODIFY THE FOLLOWING PART, UNLESS YOU KNOW WHAT IT MEANS ###
  11. echo_r () {
  12. [ $# -ne 1 ] && return 0
  13. echo -e "\033[31m$1\033[0m"
  14. }
  15. echo_g () {
  16. [ $# -ne 1 ] && return 0
  17. echo -e "\033[32m$1\033[0m"
  18. }
  19. echo_y () {
  20. [ $# -ne 1 ] && return 0
  21. echo -e "\033[33m$1\033[0m"
  22. }
  23. echo_b () {
  24. [ $# -ne 1 ] && return 0
  25. echo -e "\033[34m$1\033[0m"
  26. }
  27. usage() {
  28. docker images
  29. echo "Usage: $0 registry1:tag1 [registry2:tag2...]"
  30. }
  31. [ $# -lt 1 ] && usage && exit
  32. echo_b "The registry server is $registry"
  33. for image in "$@"
  34. do
  35. echo_b "Uploading $image..."
  36. docker tag $image $registry/$image
  37. docker push $registry/$image
  38. docker rmi $registry/$image
  39. echo_g "Done"
  40. done

建议把脚本存放到本地的可执行路径下,例如/usr/local/bin/下面。然后添加可执行权限,就可以使用该脚本了:


  1. $ sudo chmod a+x /usr/local/bin/push_images.sh

例如,推送本地的ubuntu:latest和centos:centos7两个镜像到本地仓库:


  1. $ ./push_images.sh ubuntu:latest centos:centos7
  2. The registry server is 127.0.0.1
  3. Uploading ubuntu:latest...
  4. The push refers to a repository [127.0.0.1:5000/ubuntu] (len: 1)
  5. Sending image list
  6. Pushing repository 127.0.0.1:5000/ubuntu (1 tags)
  7. Image 511136ea3c5a already pushed, skipping
  8. Image bfb8b5a2ad34 already pushed, skipping
  9. Image c1f3bdbd8355 already pushed, skipping
  10. Image 897578f527ae already pushed, skipping
  11. Image 9387bcc9826e already pushed, skipping
  12. Image 809ed259f845 already pushed, skipping
  13. Image 96864a7d2df3 already pushed, skipping
  14. Pushing tag for rev [96864a7d2df3] on {http://127.0.0.1:5000/v1/repositories/
  15. ubuntu/tags/latest}
  16. Untagged: 127.0.0.1:5000/ubuntu:latest
  17. Done
  18. Uploading centos:centos7...
  19. The push refers to a repository [127.0.0.1:5000/centos] (len: 1)
  20. Sending image list
  21. Pushing repository 127.0.0.1:5000/centos (1 tags)
  22. Image 511136ea3c5a already pushed, skipping
  23. 34e94e67e63a: Image successfully pushed
  24. 70214e5d0a90: Image successfully pushed
  25. Pushing tag for rev [70214e5d0a90] on {http://127.0.0.1:5000/v1/repositories/
  26. centos/tags/centos7}
  27. Untagged: 127.0.0.1:5000/centos:centos7
  28. Done

上传后,查看本地镜像,会发现上传中创建的临时标签也同时被清理了。

2.上传本地所有镜像

在push_images工具的基础上,还可以进一步的创建push_all工具,来上传本地所有镜像:


  1. #!/bin/sh
  2. # This script will upload all local images to a registry server ($registry is
  3. the default value).
  4. # This script requires the push_images, which can be found at https://github.com/
  5. yeasy/docker_practice/blob/master/_local/push_images.sh
  6. # Usage: push_all
  7. # Author: yeasy@github
  8. # Create: 2014-09-23
  9. for image in `docker images|grep -v "REPOSITORY"|grep -v "<none>"|awk '{print $1":"$2}'`
  10. do
  11. push_images.sh $image
  12. done

另外,推荐读者把它放在/usr/local/bin/下面,并添加可执行权限。这样就可以通过push_all命令来同步本地所有镜像到本地私有仓库了。

同样,读者可以试着修改脚本,实现批量化下载镜像、删除镜像、更新镜像标签等更多的操作。