Foliver57

OpenWebUI-Script

Apr 5th, 2024
2,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.73 KB | Source Code | 0 0
  1. # This script is used to manage the open-webui Docker container.
  2. # It performs the following steps:
  3. #
  4. # Verifies the existence of the specified local directory for mounting to the container.
  5. # Verifies the existence of the specified container name and image name.
  6. # Retrieves the latest image from the Docker registry, if not already present or if an update is available.
  7. # If no container with the specified name is running, it launches a new container using the latest image.
  8. # If a container with the specified name is running and the image has been updated, it stops and removes the existing container, then launches a new container using the latest image.
  9.  
  10. # ANSI escape codes for colored output
  11. RED='\033[0;31m'
  12. GREEN='\033[0;32m'
  13. YELLOW='\033[0;33m'
  14. NC='\033[0m' # No Color
  15.  
  16.  
  17.  
  18. # Configuration Variables
  19. OBSIDIAN_FOLDER_PATH="/mnt/c/Users/MyWindowsUser/Documents/Obsidian Vault/"
  20. INTERNAL_SCAN_FOLDER_PATH="/app/backend/data/docs/obsidian-vault"
  21. CONTAINER_NAME="open-webui"
  22. IMAGE_NAME="ghcr.io/open-webui/open-webui:main"
  23. OPENAI_API_KEY="sk-YOUR-KEY-HERE"
  24. # Default open-webui docker run command
  25. # docker run -d -p 3000:8080 --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:main
  26.  
  27. # device type for whisper tts and embbeding models - "cpu" (default), "cuda" (nvidia gpu and CUDA required) or "mps" (apple silicon) - choosing this right can lead to better performance
  28. # We must pass this as an environment variable to the container
  29. RAG_EMBEDDING_MODEL_DEVICE_TYPE="cpu"
  30.  
  31. # Check if docker is installed
  32. if ! [ -x "$(command -v docker)" ]; then
  33.   echo -e "${RED}Error: docker is not installed.${NC}" >&2
  34.   exit 1
  35. fi
  36.  
  37. # Check if the local directory exists
  38. if [ ! -d "$OBSIDIAN_FOLDER_PATH" ]; then
  39.     echo -e "${RED}Directory $OBSIDIAN_FOLDER_PATH does not exist.${NC}"
  40.     exit 1
  41. fi
  42.  
  43. # Check if Docker is installed and running
  44. if ! docker info >/dev/null 2>&1; then
  45.     echo -e "${RED}Docker is not installed or not running.${NC}"
  46.     exit 1
  47. fi
  48.  
  49. # Check if the user has the necessary permissions to interact with Docker
  50. if ! docker ps >/dev/null 2>&1; then
  51.     echo -e "${RED}You do not have the necessary permissions to interact with Docker.${NC}"
  52.     exit 1
  53. fi
  54.  
  55. echo -e "${YELLOW}Pulling the latest image...${NC}"
  56. docker pull $IMAGE_NAME
  57.  
  58. # Get the ID of the latest image
  59. LATEST_IMAGE_ID=$(docker inspect --format="{{.Id}}" $IMAGE_NAME)
  60.  
  61. # Check if a container with the specified name exists
  62. if [ "$(docker ps -aq -f name=$CONTAINER_NAME)" ]; then
  63.     # Get the ID of the image used by the existing container
  64.     CONTAINER_IMAGE_ID=$(docker inspect --format="{{.Image}}" $CONTAINER_NAME)
  65.  
  66.     # If the existing container is not using the latest image, or if it's not currently running, stop and remove it
  67.     if [ "$CONTAINER_IMAGE_ID" != "$LATEST_IMAGE_ID" ] || ! docker ps -q -f name=$CONTAINER_NAME >/dev/null 2>&1; then
  68.         echo -e "${YELLOW}Stopping and removing existing container...${NC}"
  69.         docker stop $CONTAINER_NAME
  70.         docker rm $CONTAINER_NAME
  71.     else
  72.         # If the existing container is using the latest image and is currently running, there's nothing more to do
  73.         echo -e "${GREEN}Container is up to date and running.${NC}"
  74.         exit 0
  75.     fi
  76. fi
  77.  
  78. # Launch a new container
  79. echo -e "${YELLOW}Launching a new container...${NC}"
  80. docker run -d -p 3000:8080 --gpus all --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data -v "$OBSIDIAN_FOLDER_PATH":"$INTERNAL_SCAN_FOLDER_PATH" --name open-webui --restart always -e OPENAI_API_KEY="$OPENAI_API_KEY" -e RAG_EMBEDDING_MODEL_DEVICE_TYPE="$RAG_EMBEDDING_MODEL_DEVICE_TYPE" $IMAGE_NAME
  81. echo -e "${GREEN}Container launched successfully.${NC}"
Tags: OpenWeb UI
Advertisement
Add Comment
Please, Sign In to add comment