-
Notifications
You must be signed in to change notification settings - Fork 3
/
mirror.sh
executable file
·68 lines (58 loc) · 1.14 KB
/
mirror.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
usage() {
cat <<EOF
usage: $0 -r <registry_host> -i <images>
docker-image-mirror copy docker images to your private docker registry.
OPTIONS:
-h Show this message
-i Docker images to mirror: "library/ubuntu library/redis"
-r Docker registry to mirror: "example.com"
-t Docker image tag (fetch only this tag)
EOF
}
type docker >/dev/null 2>&1 || {
echo >&2 "Docker required ;-)";
exit 1
}
IMAGES=
REGISTRY=
TAG=
while getopts "h:r:i:t:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
r)
REGISTRY=$OPTARG
;;
t)
TAG=$OPTARG
;;
i)
IMAGES=$OPTARG
;;
?)
usage
exit
;;
esac
done
if [[ -z $REGISTRY ]] || [[ -z $IMAGES ]]; then
usage
exit 1
fi
for IMAGE in $IMAGES; do
if ! [[ -z $TAG ]]; then
docker pull $IMAGE:$TAG
else
docker pull -a $IMAGE
fi
TRUNCIMAGE=$(echo $IMAGE|sed 's/library\///')
TAGS=$(docker images|grep -G "^${TRUNCIMAGE}[[:blank:]]"|awk '{print $2}'|sort|uniq)
for TAG in $TAGS; do
docker tag $IMAGE:$TAG $REGISTRY/$IMAGE:$TAG
docker push $REGISTRY/$IMAGE:$TAG
done
done