source ../environment.sh

if [ -z "$NETWORK" ]; then
echo "错误: 未找到 NETWORK 环境变量"
exit 1
fi
if [ -z "$ES_NAME" ]; then
echo "错误: 未找到 ES_NAME 环境变量"
exit 1
fi
if [ -z "$ES_IMAGE" ]; then
echo "错误: 未找到 ES_IMAGE 环境变量"
exit 1
fi
if [ -z "$ES_MEMORY" ]; then
echo "错误: 未找到 ES_MEMORY 环境变量"
exit 1
fi
if [ -z "$ES_PORT" ]; then
echo "错误: 未找到 ES_PORT 环境变量"
exit 1
fi
if [ -z "$ES_DATA" ]; then
echo "错误: 未找到 ES_DATA 环境变量"
exit 1
fi

if docker ps -a | grep -q ${ES_NAME}; then
    echo "停止并删除已存在的 Elasticsearch 容器..."
    docker stop ${ES_NAME} >/dev/null 2>&1
    docker rm ${ES_NAME} >/dev/null 2>&1
fi

# 启动Elasticsearch容器
docker run -d \
    --name ${ES_NAME} \
    --network $NETWORK \
    --memory $ES_MEMORY \
    --restart unless-stopped \
    -e "discovery.type=single-node" \
    -e "xpack.security.enabled=false" \
    -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
    -v "${ES_DATA}:/usr/share/elasticsearch/data" \
    -v "${ES_LOG_DATA}:/usr/share/elasticsearch/logs" \
    $ES_IMAGE