#!/usr/bin/env bash # Start / stop the local AI container. # ./run-ai-image.sh start (default) # ./run-ai-image.sh stop stop and remove # ./run-ai-image.sh logs follow container logs # ./run-ai-image.sh status show state set -euo pipefail cd "$(dirname "$0")" [ -f .ai-config ] || { echo "No .ai-config yet - run ./setup.sh first." >&2; exit 1; } # shellcheck disable=SC1091 source .ai-config PORT=${AI_PORT:-$PORT} # override for one run: AI_PORT=11500 ./run-ai-image.sh running() { [ "$(docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null)" = "true" ]; } exists() { docker inspect "$CONTAINER_NAME" >/dev/null 2>&1; } case "${1:-start}" in stop) exists && docker rm -f "$CONTAINER_NAME" >/dev/null && echo "Stopped." || echo "Not running."; exit 0 ;; logs) exec docker logs -f "$CONTAINER_NAME" ;; status) running && echo "Running: http://127.0.0.1:$PORT (model $MODEL)" || echo "Not running."; exit 0 ;; start) ;; *) echo "Usage: $0 [start|stop|logs|status]" >&2; exit 1 ;; esac docker image inspect "$IMAGE_NAME" >/dev/null 2>&1 || { echo "Image missing - run ./build-ai-image.sh first." >&2; exit 1; } if running; then echo "Already running on http://127.0.0.1:$PORT" else exists && docker rm -f "$CONTAINER_NAME" >/dev/null if (exec 3<>"/dev/tcp/127.0.0.1/$PORT") 2>/dev/null; then echo "Port $PORT is already in use by something else (a host Ollama, another container?):" >&2 docker ps --filter "publish=$PORT" --format ' container {{.Names}}' >&2 || true echo "Re-run ./setup.sh to pick a free port, or use: AI_PORT= ./run-ai-image.sh" >&2 exit 1 fi args=(-d --name "$CONTAINER_NAME" -p "127.0.0.1:$PORT:11434" --restart unless-stopped) [ "$USE_GPU" = "1" ] && args+=(--gpus all) docker run "${args[@]}" "$IMAGE_NAME" >/dev/null printf "Waiting for the model server" for _ in $(seq 1 60); do curl -fs "http://127.0.0.1:$PORT/api/tags" >/dev/null 2>&1 && break printf "."; sleep 1 done echo curl -fs "http://127.0.0.1:$PORT/api/tags" >/dev/null 2>&1 || { echo "Server did not come up; see: ./run-ai-image.sh logs" >&2; exit 1; } echo "Running on http://127.0.0.1:$PORT" fi echo echo "Now, in your project directory:" echo " OLLAMA_API_BASE=http://127.0.0.1:$PORT aider --model ollama_chat/$MODEL" echo "(see RUN_AIDER.md for details)"