#!/bin/bash

# Docker镜像构建脚本
# 用于构建Laravel知识库系统的Docker镜像

set -e

# 配置变量
IMAGE_NAME="knowledge-base-app"
IMAGE_TAG="latest"
PLATFORM="linux/amd64"

echo "开始构建Docker镜像..."
echo "镜像名称: ${IMAGE_NAME}:${IMAGE_TAG}"
echo "目标平台: ${PLATFORM}"

# 检查Docker是否运行
if ! docker info > /dev/null 2>&1; then
    echo "错误: Docker未运行或无法访问"
    exit 1
fi

# 构建镜像
echo "正在构建镜像..."
docker build \
    --platform ${PLATFORM} \
    --tag ${IMAGE_NAME}:${IMAGE_TAG} \
    --file Dockerfile \
    .

# 验证构建结果
if [ $? -eq 0 ]; then
    echo "✅ 镜像构建成功!"
    
    # 显示镜像信息
    echo ""
    echo "镜像信息:"
    docker images ${IMAGE_NAME}:${IMAGE_TAG}
    
    # 检查镜像架构
    echo ""
    echo "镜像架构信息:"
    docker inspect ${IMAGE_NAME}:${IMAGE_TAG} | grep -A 5 "Architecture"
    
    echo ""
    echo "构建完成! 可以使用以下命令运行容器:"
    echo "docker run -d -p 8000:8000 ${IMAGE_NAME}:${IMAGE_TAG}"
else
    echo "❌ 镜像构建失败!"
    exit 1
fi