Files
dokku-ui/build_and_push.sh
2025-10-16 16:34:46 +08:00

58 lines
1.4 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Docker 镜像构建和推送脚本
# 使用方法: ./build_and_push.sh [镜像标签] [推送标志]
set -e # 遇到错误立即退出
# 配置变量
IMAGE_NAME="shokku"
REGISTRY="ccr.ccs.tencentyun.com" # 替换为您的仓库地址
NAMESPACE="miaogai" # 替换为您的命名空间
DOCKERFILE="Dockerfile"
BUILD_CONTEXT="."
# 参数处理
TAG=${1:-"latest"}
PUSH=${2:-"false"}
# 完整的镜像地址
FULL_IMAGE_NAME="${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${TAG}"
echo "=== Docker 镜像构建和推送 ==="
echo "镜像名称: ${FULL_IMAGE_NAME}"
echo "Dockerfile: ${DOCKERFILE}"
echo "构建上下文: ${BUILD_CONTEXT}"
# 检查 Docker 是否安装
if ! command -v docker &> /dev/null; then
echo "错误: 未找到 Docker请先安装 Docker"
exit 1
fi
# 构建镜像
echo "开始构建镜像..."
docker build -t ${IMAGE_NAME}:${TAG} -f ${DOCKERFILE} ${BUILD_CONTEXT}
# 标记镜像
echo "标记镜像..."
docker tag ${IMAGE_NAME}:${TAG} ${FULL_IMAGE_NAME}
# 如果需要推送
if [ "$PUSH" = "true" ]; then
echo "登录到镜像仓库..."
# 如果使用 Docker Hub
# docker login
# 如果使用私有仓库
# docker login ${REGISTRY}
echo "推送镜像到仓库..."
docker push ${FULL_IMAGE_NAME}
echo "✅ 镜像推送完成: ${FULL_IMAGE_NAME}"
else
echo " 镜像构建完成,未推送 (使用 'true' 作为第二个参数来推送)"
fi
echo "=== 完成 ==="