脚本将镜像文件推送到 Docker Registry

Import

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
#!/usr/bin/env bash

set -e

REGISTRY="hub.starudream.local"

function load() {
if [ ! -f "$1" ]; then
echo "file not found: $1"
exit 1
fi
res=$(docker image load -i "$1" 2>/dev/null | grep "Loaded image:" || true)
images=${res//Loaded image: /}
for image in $images; do
echo "-> loaded $image"
if [ -z $REGISTRY ]; then
continue
fi
cnt=$(echo "$image" | awk -F/ "{print NF-1}")
name=$image
case $cnt in
0)
name="library/$image"
;;
1) ;;
*)
part=${image%/*}
left=${part%/*}
name=${image/$left\//}
;;
esac
docker image tag "$image" "$REGISTRY/$name"
docker image push "$REGISTRY/$name"
echo "=> pushed $image to $REGISTRY/$name"
docker image rm "$REGISTRY/$name"
docker image rm "$image" || true
done
}

cnt=$(grep -c $REGISTRY /etc/hosts || true)
if [ "$cnt" -eq 0 ]; then
REGISTRY=""
elif [ "$PUSH_REGISTRY" = 0 ]; then
REGISTRY=""
fi

if [ -z "$1" ]; then
files=$(find . -type f -name "*.tar.gz" | sort -u)
for file in $files; do
echo ">> found $file"
load "$file"
done
else
load "$1"
fi

Export

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
#!/usr/bin/env bash

set -e

CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT_DIR=$(dirname "$CUR_DIR")

mkdir -p "$ROOT_DIR"/images

function save() {
if [ ! -f "$1" ]; then
echo "file not found: $1"
exit 1
fi
images=$(docker-compose -f "$1" config --images | sort -u)
for image in $images; do
name=${image////-}
name=${name//:/_}
if [ -f "$ROOT_DIR/images/$name.tar.gz" ]; then
echo "-> exist $name.tar.gz, skip"
continue
fi
docker image pull --platform linux/amd64 "$image"
echo "=> save $image to $name.tar.gz"
docker image save "$image" | gzip > "$ROOT_DIR/images/$name.tar.gz"
done
}

if [ -z "$1" ]; then
files=$(find . -type f -name "docker-compose*.yaml" | sort -u)
for file in $files; do
echo ">> found $file"
save "$file"
done
else
save "$1"
fi