使用Shell脚本实现Docker Push且实现计算推送的速度与花费时间

暗香疏影 创作者

我们使用Docker Push来将镜像推送到仓库,这个脚本是为了看看推送到私有仓库的速度,从而了解并优化全球的推送速度。

默认

本地镜像需要先重新打标签为私有仓库后才可以推送:

1
2
3
docker images
docker tag imageID cr.localrepo.com/namespace/image:tag
docker push cr.localrepo.com/namespace/image:tag

docker-push.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
#!/bin/bash
set -e # Exit on error

# Usage: ./docker-push-timer-realtime.sh your-registry/image:tag
if [ $# -ne 1 ]; then
echo "Usage: $0 <image:tag>"
exit 1
fi

IMAGE=$1

# Get image size (compressed size = what actually gets pushed)
IMAGE_SIZE_BYTES=$(docker image inspect "$IMAGE" --format='{{.Size}}')
IMAGE_SIZE_MB=$(echo "scale=2; $IMAGE_SIZE_BYTES / 1048576" | bc)

echo "📌 Image: $IMAGE"
echo "📦 Compressed size: $IMAGE_SIZE_MB MB"
echo "🚀 Starting push... (timing will begin now)"
echo "--------------------------------------------------"

# Start timer
START_TIME=$(date +%s%3N)

# Run docker push and show real-time output
docker push "$IMAGE"

# Capture exit code and stop timer
PUSH_EXIT_CODE=$?
END_TIME=$(date +%s%3N)

# Calculate duration and speed
DURATION_MS=$((END_TIME - START_TIME))
DURATION_SEC=$(echo "scale=2; $DURATION_MS / 1000" | bc)
SPEED_MBPS=$(echo "scale=2; $IMAGE_SIZE_MB / $DURATION_SEC" | bc)

# Handle errors
if [ $PUSH_EXIT_CODE -ne 0 ]; then
echo -e "\n❌ Push FAILED (exit code $PUSH_EXIT_CODE)"
exit $PUSH_EXIT_CODE
fi

# Final results
echo -e "\n--------------------------------------------------"
echo -e "✅ Push SUCCEEDED!"
echo -e "⏱️ Duration: $DURATION_SEC seconds"
echo -e "📶 Speed: $SPEED_MBPS MB/s"

最后记得chmod +x本脚本再运行

1
./docker-push.sh cr.localrepo.com/namespace/image:tag

全球加速

通常全球加速可以帮助到跨国企业公网推送镜像,我们只需要监听TCP 443,其他默认即可。然后将获得的加速IP通过本地修改Host方式来做,因为用全球加速的域名或者自定义域名都会导致私有镜像仓库校验失败。

1
25.25.25.25 cr.localrepo.com
  • 标题: 使用Shell脚本实现Docker Push且实现计算推送的速度与花费时间
  • 作者: 暗香疏影
  • 创建于 : 2025-08-01 10:24:00
  • 更新于 : 2025-08-01 10:26:00
  • 链接: https://blog.pptcar.com/2025/08/01/2025-08-01-Using-shell-script-Docker-Push/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
使用Shell脚本实现Docker Push且实现计算推送的速度与花费时间