(16)docker自动镜像打包脚本
注:
1.将写好的Dockerfile文件、脚本、前端包或后端包放在同一目录下;
2.基础镜像提前导入;
-----------------------------------
#!/bin/bash
# 定义脚本参数和变量
IMAGE_NAME="lisp-bpms-web" # 替换为实际的镜像名称
DOCKERFILE_PATH="Dockerfile" # 如果Dockerfile位置与脚本在同一目录,无需修改
CONTEXT_PATH="." # 同上
SAVE_AS_TAR=true # 根据需求决定是否保存为 tar 文件
# 获取当前时间戳作为镜像标签
TIMESTAMP=$(date +%Y%m%d%H%M%S)
# 构建带有时间戳的镜像
echo "Building Docker image with tag ${IMAGE_NAME}:${TIMESTAMP}"
docker build --tag "${IMAGE_NAME}:${TIMESTAMP}" -f "${DOCKERFILE_PATH}" "${CONTEXT_PATH}"
# 检查构建状态
if [ $? -ne 0 ]; then
echo "Docker image build failed."
exit 1
fi
# 保存镜像为 tar 文件(如果 SAVE_AS_TAR 设置为 true)
if [ "$SAVE_AS_TAR" = true ]; then
echo "Saving Docker image as tar file"
IMAGE_FILE="${IMAGE_NAME}_${TIMESTAMP}.tar"
docker save "${IMAGE_NAME}:${TIMESTAMP}" -o "${IMAGE_FILE}"
if [ $? -ne 0 ]; then
echo "Failed to save Docker image as tar file."
exit 1
fi
echo "Docker image saved as ${IMAGE_FILE}"
fi
echo "Docker image build and (optionally) save completed successfully."