Skip to content

Instantly share code, notes, and snippets.

@monarchmaisuriya
Last active June 25, 2025 08:14
Show Gist options
  • Save monarchmaisuriya/f4afc69f277f4e7883918340fa392359 to your computer and use it in GitHub Desktop.
Save monarchmaisuriya/f4afc69f277f4e7883918340fa392359 to your computer and use it in GitHub Desktop.
Simple automation script to orchestrate open webui instance
#!/bin/bash
set -e
# Constants
CONTAINER_NAME="openwebui"
DEFAULT_PORT=1337
DEFAULT_MODEL="llama3.1:8b"
IMAGE_NAME="ghcr.io/open-webui/open-webui:main"
# Functions
function print_menu() {
echo -e "\nπŸ“‹ MENU"
echo "1) Start Open WebUI + Ollama"
echo "2) Stop"
echo "3) Status"
echo "4) Exit"
}
function detect_arch() {
if [[ $(uname -m) == "arm64" ]]; then
echo "🍏 Apple Silicon detected β€” Metal GPU will be used (if supported)."
else
echo "πŸ’» Intel Mac detected β€” will run on CPU."
fi
}
function is_ollama_running() {
pgrep -x "ollama" > /dev/null
}
function start_ollama() {
if is_ollama_running; then
echo "βœ… Ollama already running."
else
echo "🧠 Starting Ollama server..."
ollama serve > /dev/null 2>&1 &
sleep 2
if is_ollama_running; then
echo "βœ… Ollama started."
else
echo "❌ Failed to start Ollama. Please check if it's installed and try again."
exit 1
fi
fi
}
function get_openwebui_port() {
if docker ps --format '{{.Names}}' | grep -q "$CONTAINER_NAME"; then
docker port "$CONTAINER_NAME" 8080/tcp 2>/dev/null | awk -F ':' '{print $2}' | xargs
else
echo ""
fi
}
function wait_for_openwebui() {
local max_attempts=60
local attempt=1
echo -n "⏳ Waiting for Open WebUI to become healthy"
while [[ $attempt -le $max_attempts ]]; do
local status=$(docker inspect --format='{{.State.Health.Status}}' "$CONTAINER_NAME" 2>/dev/null || echo "none")
if [[ "$status" == "healthy" ]]; then
echo ""
return 0
fi
echo -n "."
sleep 1
((attempt++))
done
echo ""
echo "❌ Timed out waiting for Open WebUI to become healthy. Check logs:"
echo " docker logs -f $CONTAINER_NAME"
return 1
}
function start_openwebui() {
local model=$1
local port=$2
echo "πŸ“¦ Pulling latest Open WebUI image..."
docker pull "$IMAGE_NAME"
RUNNING_PORT=$(get_openwebui_port)
if [[ -n "$RUNNING_PORT" ]]; then
if [[ "$RUNNING_PORT" == "$port" ]]; then
echo "βœ… Open WebUI already running on correct port ($port)."
wait_for_openwebui && open "http://localhost:$port" && exit 0
else
echo "⚠️ Open WebUI is running but on port $RUNNING_PORT (expected: $port)"
echo "πŸ” Restarting Open WebUI on port $port..."
docker rm -f "$CONTAINER_NAME" > /dev/null
fi
fi
echo "πŸš€ Starting Open WebUI on port $port..."
docker run -d \
--name "$CONTAINER_NAME" \
--add-host=host.docker.internal:host-gateway \
-e OLLAMA_API_BASE_URL=http://host.docker.internal:11434 \
-e WEBUI_SECRET_KEY=$(openssl rand -hex 32) \
-p "$port:8080" \
-v open-webui:/app/backend/data \
--restart always \
--health-cmd="curl -fs http://localhost:8080/ || exit 1" \
--health-interval=5s \
--health-timeout=3s \
--health-retries=20 \
"$IMAGE_NAME" > /dev/null
wait_for_openwebui && echo "βœ… Open WebUI is now running at: http://localhost:$port" && open "http://localhost:$port"
}
function stop_all() {
echo "πŸ›‘ Stopping Open WebUI..."
docker rm -f "$CONTAINER_NAME" > /dev/null 2>&1 && echo "βœ… Open WebUI stopped." || echo "ℹ️ Open WebUI not running."
if is_ollama_running; then
killall ollama > /dev/null 2>&1 && echo "🧠 Ollama stopped."
else
echo "ℹ️ Ollama not running."
fi
}
function status() {
echo "πŸ“Š Docker containers:"
docker ps --filter "name=$CONTAINER_NAME"
if is_ollama_running; then
echo "βœ… Ollama running."
else
echo "❌ Ollama not running."
fi
}
# Entry point
echo -e "\nπŸš€ Welcome to the Open WebUI + Ollama Setup Script for macOS!"
while true; do
print_menu
read -rp $'\nSelect an option [1-4]: ' opt
case "$opt" in
1)
read -rp "🌐 Enter port (default: $DEFAULT_PORT): " PORT
PORT=${PORT:-$DEFAULT_PORT}
detect_arch
start_ollama
start_openwebui "$MODEL" "$PORT"
exit 0
;;
2)
stop_all
exit 0
;;
3)
status
exit 0
;;
4)
echo "πŸ‘‹ Goodbye!"
exit 0
;;
*)
echo "❌ Invalid option."
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment