gradio-5.35.0/build-and-run.sh

53 lines
1.4 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

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
set -e
set -o pipefail
# ======== 配置参数 ========
IMAGE_NAME="gradio-local:5.35.0"
CONTAINER_NAME="gradio-container"
PORT=7860
PROXY_URL="http://127.0.0.1:7890"
GIT_DIR="./gradio-5.35.0"
echo "🛠️ 开始构建并部署 Gradio 镜像..."
# ======== 检查旧容器并删除 ========
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo "🛑 停止并删除旧容器 ${CONTAINER_NAME}..."
docker stop "${CONTAINER_NAME}" || true
docker rm "${CONTAINER_NAME}" || true
fi
# ======== 删除旧镜像(如果存在) ========
if docker images --format '{{.Repository}}:{{.Tag}}' | grep -q "^${IMAGE_NAME}$"; then
echo "🧹 删除旧镜像 ${IMAGE_NAME}..."
docker rmi "${IMAGE_NAME}" || true
fi
# ======== 检查当前目录是 Git 仓库 ========
if [ ! -d ".git" ]; then
echo "❌ 当前目录不是 Git 仓库,请在 gradio 仓库根目录运行。"
exit 1
fi
# ======== 拉取 gradio-5.35.0 子目录的代码 ========
echo "📥 拉取最新代码..."
cd "${GIT_DIR}"
git reset --hard
git pull
cd ..
# ======== 构建 Docker 镜像(使用当前目录的 Dockerfile ========
echo "🐳 开始构建 Docker 镜像..."
docker build -t "${IMAGE_NAME}" .
# ======== 启动容器 ========
echo "🚀 启动 Gradio 容器(后台运行)..."
docker run -d \
--name "${CONTAINER_NAME}" \
-p ${PORT}:${PORT} \
"${IMAGE_NAME}"
echo "✅ Gradio 容器已启动成功,监听端口 ${PORT}"