ls#!/bin/bash

# 定义变量
APP_NAME="hot-platform"
IMAGE_NAME="hot-platform"
PORT_HOST=50004
PORT_CONTAINER=19090
LOG_DIR="/opt/hot-platform/logs"

# 停止并删除旧容器
if [ "$(docker ps -q -f name=$APP_NAME)" ]; then
    echo "Stopping existing container..."
    docker stop $APP_NAME
fi

if [ "$(docker ps -aq -f name=$APP_NAME)" ]; then
    echo "Removing existing container..."
    docker rm $APP_NAME
fi

# 删除旧镜像（可选，如果每次都构建新镜像的话建议开启）
# if [ "$(docker images -q $IMAGE_NAME)" ]; then
#     echo "Removing old image..."
#     docker rmi $IMAGE_NAME
# fi

# 构建新镜像
echo "Building new image..."
docker build -t $IMAGE_NAME .

# 启动新容器
echo "Starting new container..."
docker run -d \
  --name $APP_NAME \
  --restart always \
  --add-host=host.docker.internal:host-gateway \
  -p $PORT_HOST:$PORT_CONTAINER \
  -v $LOG_DIR:/hot-platform/server/logs \
  -e JAVA_OPTS="-Dspring.profiles.active=test" \
  $IMAGE_NAME

echo "Container started successfully!"
docker logs -f --tail 100 $APP_NAME
