Last active
June 25, 2025 08:14
-
-
Save monarchmaisuriya/f4afc69f277f4e7883918340fa392359 to your computer and use it in GitHub Desktop.
Simple automation script to orchestrate open webui instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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