#!/bin/bash
set -e
# Usage
usage() {
echo "Usage: $0
[VER] [save]"
echo ""
echo " DIR - Directory path containing Dockerfile (required)"
echo " VER - Image version (optional, default: 1.0.0)"
echo " save - Save image as tar file (optional)"
exit 1
}
# Check DIR parameter
if [ -z "$1" ]; then
echo "Error: Missing DIR parameter"
usage
fi
DIR="$1"
VER="${2:-1.0.0}"
SAVE="$3"
# Check if directory exists
if [ ! -d "$DIR" ]; then
echo "Error: Directory not found: $DIR"
exit 1
fi
# Extract folder name from directory path as image name
DIR_NAME=$(basename "$DIR")
NAME="timi/${DIR_NAME}"
echo "==> Building image: ${NAME}:${VER}"
echo "==> Dockerfile path: ${DIR}"
# Remove existing image with same name (ignore errors)
if docker image inspect "${NAME}:${VER}" > /dev/null 2>&1; then
echo "==> Removing existing image: ${NAME}:${VER}"
docker rmi "${NAME}:${VER}" || true
fi
# Build image
echo "==> Starting build..."
docker build -t "${NAME}:${VER}" "$DIR"
echo "==> Build completed: ${NAME}:${VER}"
# If save parameter specified, save image as tar
if [ "$SAVE" = "save" ]; then
TAR_FILE="${DIR_NAME}.tar"
echo "==> Saving image as: ${TAR_FILE}"
docker save -o "$TAR_FILE" "${NAME}:${VER}"
echo "==> Save completed: ${TAR_FILE}"
fi