18.5 批量管理镜像
在之前章节中,笔者介绍了如何对单个镜像进行上传、下载的操作。有时候,本地镜像很多,逐个打标记进行操作将十分浪费时间。这里将以批量上传镜像为例,介绍如何利用脚本实现对镜像的批量化处理。
1.批量上传指定镜像
可以使用下面的push_images.sh脚本,批量上传本地的镜像到注册服务器中,默认是本地注册服务器127.0.0.1:5000,用户可以通过修改registry=127.0.0.1:5000这行来指定目标注册服务器:
- #!/bin/sh
- # This script will upload the given local images to a registry server ($registry
- is the default value).
- # See: https://github.com/yeasy/docker_practice/blob/master/_local/push_images.sh
- # Usage: push_images image1 [image2...]
- # Author: yeasy@github
- # Create: 2014-09-23
- #The registry server address where you want push the images into
- registry=127.0.0.1:5000
- ### DO NOT MODIFY THE FOLLOWING PART, UNLESS YOU KNOW WHAT IT MEANS ###
- echo_r () {
- [ $# -ne 1 ] && return 0
- echo -e "\033[31m$1\033[0m"
- }
- echo_g () {
- [ $# -ne 1 ] && return 0
- echo -e "\033[32m$1\033[0m"
- }
- echo_y () {
- [ $# -ne 1 ] && return 0
- echo -e "\033[33m$1\033[0m"
- }
- echo_b () {
- [ $# -ne 1 ] && return 0
- echo -e "\033[34m$1\033[0m"
- }
- usage() {
- docker images
- echo "Usage: $0 registry1:tag1 [registry2:tag2...]"
- }
- [ $# -lt 1 ] && usage && exit
- echo_b "The registry server is $registry"
- for image in "$@"
- do
- echo_b "Uploading $image..."
- docker tag $image $registry/$image
- docker push $registry/$image
- docker rmi $registry/$image
- echo_g "Done"
- done
建议把脚本存放到本地的可执行路径下,例如/usr/local/bin/下面。然后添加可执行权限,就可以使用该脚本了:
- $ sudo chmod a+x /usr/local/bin/push_images.sh
例如,推送本地的ubuntu:latest和centos:centos7两个镜像到本地仓库:
- $ ./push_images.sh ubuntu:latest centos:centos7
- The registry server is 127.0.0.1
- Uploading ubuntu:latest...
- The push refers to a repository [127.0.0.1:5000/ubuntu] (len: 1)
- Sending image list
- Pushing repository 127.0.0.1:5000/ubuntu (1 tags)
- Image 511136ea3c5a already pushed, skipping
- Image bfb8b5a2ad34 already pushed, skipping
- Image c1f3bdbd8355 already pushed, skipping
- Image 897578f527ae already pushed, skipping
- Image 9387bcc9826e already pushed, skipping
- Image 809ed259f845 already pushed, skipping
- Image 96864a7d2df3 already pushed, skipping
- Pushing tag for rev [96864a7d2df3] on {http://127.0.0.1:5000/v1/repositories/
- ubuntu/tags/latest}
- Untagged: 127.0.0.1:5000/ubuntu:latest
- Done
- Uploading centos:centos7...
- The push refers to a repository [127.0.0.1:5000/centos] (len: 1)
- Sending image list
- Pushing repository 127.0.0.1:5000/centos (1 tags)
- Image 511136ea3c5a already pushed, skipping
- 34e94e67e63a: Image successfully pushed
- 70214e5d0a90: Image successfully pushed
- Pushing tag for rev [70214e5d0a90] on {http://127.0.0.1:5000/v1/repositories/
- centos/tags/centos7}
- Untagged: 127.0.0.1:5000/centos:centos7
- Done
上传后,查看本地镜像,会发现上传中创建的临时标签也同时被清理了。
2.上传本地所有镜像
在push_images工具的基础上,还可以进一步的创建push_all工具,来上传本地所有镜像:
- #!/bin/sh
- # This script will upload all local images to a registry server ($registry is
- the default value).
- # This script requires the push_images, which can be found at https://github.com/
- yeasy/docker_practice/blob/master/_local/push_images.sh
- # Usage: push_all
- # Author: yeasy@github
- # Create: 2014-09-23
- for image in `docker images|grep -v "REPOSITORY"|grep -v "<none>"|awk '{print $1":"$2}'`
- do
- push_images.sh $image
- done
另外,推荐读者把它放在/usr/local/bin/下面,并添加可执行权限。这样就可以通过push_all命令来同步本地所有镜像到本地私有仓库了。
同样,读者可以试着修改脚本,实现批量化下载镜像、删除镜像、更新镜像标签等更多的操作。
