Skip to content

A self-hosted media server and file management system

feat: move shared env setup to script and add user config for domain

aminnausinfc6f2291799b
diff --git a/docker-compose.yaml b/docker-compose.yaml
index 8aed4a8a..cc5b9408 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -1,17 +1,4 @@
services:
- volume-prep:
- image: alpine
- command: >
- sh -c "
- mkdir -p /shared &&
- chown -R 9999:9999 /shared &&
- chmod 775 /shared &&
- echo 'Volume ready'"
- volumes:
- - shared-env:/shared
- networks:
- - mediaServer-db
- restart: on-failure
app: &app
image: aminnausin/mediaserver:${DOCKER_RELEASE:-latest}
container_name: MediaServer
@@ -166,7 +153,8 @@ volumes:
db-data:
driver: local
shared-env:
- driver: local
+ external: true
+ name: mediaserver-shared-env
networks:
default:
diff --git a/startDocker.bat b/startDocker.bat
index ca66ced1..c86488c6 100644
--- a/startDocker.bat
+++ b/startDocker.bat
@@ -1,12 +1,35 @@
@echo off
+SET "SHARED_VOLUME_NAME=mediaserver-shared-env"
+SET "VOLUME_UID=9999"
+SET "VOLUME_GID=9999"
+SET "ENV_FILE=./.env"
+SET "FALLBACK_DEFAULT_DOMAIN=app.test"
+SET "NGINX_CONF_FILE=docker\etc\nginx\conf.d\default.conf"
echo.
echo ============================================
echo mediaServer Docker Setup
echo ============================================
echo.
-call :ColorText "[STEP 1/5] " Yellow
+SET "IS_CI_MODE=false"
+IF /I "%~1"=="--auto-default" (
+ SET "IS_CI_MODE=true"
+ call :ColorText "[INFO] " BLUE
+ echo "Auto-default mode enabled via command-line argument.""
+ SHIFT /1
+) ELSE IF DEFINED CI (
+ SET "IS_CI_MODE=true"
+ call :ColorText "[INFO] " BLUE
+ echo "Auto-default mode enabled (CI/CD environment detected - CI variable)."
+) ELSE IF DEFINED GITHUB_ACTIONS (
+ SET "IS_CI_MODE=true"
+ call :ColorText "[INFO] " BLUE
+ echo "Auto-default mode enabled (CI/CD environment detected - GITHUB_ACTIONS variable)."
+)
+echo.
+
+call :ColorText "[STEP 1/6] " Yellow
echo Verifying required files and folders...
echo.
@@ -47,8 +70,7 @@ if not exist "docker/etc/caddy/Caddyfile" (
call :ColorText "[FOUND]" Green
echo 'Caddyfile' configuration file.
echo NOTE: Make sure to replace 'app.test' with your website URL in:
- echo - '/docker/etc/caddy/Caddyfile'
- echo - '.env'
+ echo - '/docker/etc/caddy/Caddyfile' or wherever your reverse proxy is
)
echo.
@@ -148,8 +170,145 @@ if not exist "caddy/data" (
)
echo.
-call :ColorText "[STEP 2/5] " Yellow
+call :ColorText "[STEP 2/6] " Yellow
+echo Setting up user config...
+echo.
+
+setlocal ENABLEDELAYEDEXPANSION
+SET "CURRENT_APP_HOST="
+IF EXIST "%ENV_FILE%" (
+ FOR /F "tokens=1* delims==" %%a IN ('findstr /b "APP_HOST=" "%ENV_FILE%"') DO (
+ SET "CURRENT_APP_HOST=%%b"
+ )
+)
+
+SET "CURRENT_DOMAIN_DEFAULT=%FALLBACK_DEFAULT_DOMAIN%"
+
+IF NOT "!CURRENT_APP_HOST!"=="" (
+ REM Remove quotes
+ SET "TEMP_DOMAIN_FROM_HOST=!CURRENT_APP_HOST:"=!"
+ IF NOT "!TEMP_DOMAIN_FROM_HOST!"=="" (
+ SET "CURRENT_DOMAIN_DEFAULT=!TEMP_DOMAIN_FROM_HOST!"
+ )
+)
+ENDLOCAL & SET "CURRENT_DOMAIN_DEFAULT=%CURRENT_DOMAIN_DEFAULT%"
+
+REM Ask for domain name, or use default in CI/CD mode
+IF "%IS_CI_MODE%"=="true" (
+ SET "USER_DOMAIN=%CURRENT_DOMAIN_DEFAULT%"
+ call :ColorText "[INFO] " BLUE
+ echo Automatically using default domain: %USER_DOMAIN%
+) ELSE (
+ call :ColorText "[CONFIG] " YELLOW
+ echo "Enter your app domain (URL)..."
+ echo "Current value: %CURRENT_DOMAIN_DEFAULT% (Press Enter to use this)..."
+ echo.
+ set /p USER_DOMAIN="Domain: "
+)
+echo.
+
+REM Use current default if user input is empty
+IF "%USER_DOMAIN%"=="" (
+ SET "APP_HOST=%CURRENT_DOMAIN_DEFAULT%"
+) ELSE (
+ SET "APP_HOST=%USER_DOMAIN%"
+)
+
+call :ColorText "[INFO] " BLUE
+echo Setting APP_HOST to: %APP_HOST%
+echo.
+
+REM Update APP_HOST in .env file
+
+SET "TEMP_ENV_FILE=%ENV_FILE%.tmp"
+(
+ setlocal ENABLEDELAYEDEXPANSION
+ for /f "delims=" %%a in ('findstr /n "^" "%ENV_FILE%"') do (
+ set "line=%%a"
+ set "line=!line:*:=!"
+ if "!line:~0,9!"=="APP_HOST=" (
+ echo APP_HOST="%APP_HOST%"
+ ) else (
+ echo(!line!
+ )
+ )
+ endlocal
+) > "%TEMP_ENV_FILE%"
+
+IF %ERRORLEVEL% NEQ 0 (
+ echo.
+ call :ColorText "[ERROR]" RED
+ echo Failed to create temporary .env file.
+ echo Please check script permissions.
+ pause
+ goto :end
+)
+
+MOVE /Y "%TEMP_ENV_FILE%" "%ENV_FILE%" >nul
+
+IF %ERRORLEVEL% NEQ 0 (
+ echo.
+ call :ColorText "[ERROR]" RED
+ echo Failed to replace original .env file.
+ echo Please check script permissions.
+ pause
+ goto :end
+) ELSE (
+ call :ColorText "[SUCCESS] " GREEN
+ echo APP_HOST updated in %ENV_FILE%.
+)
+echo.
+
+REM Update Nginx configuration
+
+SET "SCRIPT_DIR=%~dp0"
+SET "NGINX_CONF_FILE=%SCRIPT_DIR%%NGINX_CONF_FILE%"
+
+SET "TEMP_NGINX_CONF_FILE=%NGINX_CONF_FILE%.tmp"
+
+setlocal ENABLEDELAYEDEXPANSION
+(
+ for /f "delims=" %%a in ('findstr /n "^" "%NGINX_CONF_FILE%"') do (
+ set "line=%%a"
+ set "line=!line:*:=!"
+
+ echo !line! | findstr /i "valid_referers 127.0.0.1," >nul
+ if !errorlevel! == 0 (
+ echo valid_referers 127.0.0.1, %APP_HOST%;
+ ) else (
+ echo(!line!
+ )
+ )
+) > "%TEMP_NGINX_CONF_FILE%"
+endlocal
+
+ver >nul
+
+IF NOT EXIST "%TEMP_NGINX_CONF_FILE%" (
+ call :ColorText "[ERROR]" RED
+ echo Temp Nginx config file not created.
+ pause
+ goto :end
+)
+
+MOVE /Y "%TEMP_NGINX_CONF_FILE%" "%NGINX_CONF_FILE%" >nul
+
+IF %ERRORLEVEL% NEQ 0 (
+ call :ColorText "[ERROR]" RED
+ echo Failed to replace original Nginx config file.
+ echo Please check script permissions.
+ pause
+ goto :end
+) ELSE (
+ call :ColorText "[SUCCESS] " GREEN
+ echo Nginx valid_referers updated to include: %APP_HOST%
+)
+echo.
+
+call :ColorText "[STEP 3/6] " Yellow
echo Stopping and cleaning up Existing mediaServer Docker containers...
+echo .
+
docker compose down
if errorlevel 1 (
echo.
@@ -164,7 +323,7 @@ call :ColorText "[SUCCESS]" Green
echo Docker containers stopped and cleaned up.
echo.
-call :ColorText "[STEP 3/5] " Yellow
+call :ColorText "[STEP 4/6] " Yellow
echo Pruning Docker volumes...
echo.
docker volume prune -f
@@ -181,7 +340,7 @@ call :ColorText "[SUCCESS]" Green
echo Docker volumes pruned.
echo.
-call :ColorText "[STEP 4/5] " Yellow
+call :ColorText "[STEP 5/6] " Yellow
echo Pulling latest Docker images...
echo.
docker compose pull
@@ -199,9 +358,55 @@ if errorlevel 1 (
echo.
)
-call :ColorText "[STEP 5/5] " Yellow
+call :ColorText "[STEP 6/6] " Yellow
echo Building docker compose...
echo.
+
+call :ColorText "[INFO] " BLUE
+echo Checking for shared Docker volume '%SHARED_VOLUME_NAME%'...
+docker volume inspect %SHARED_VOLUME_NAME%
+echo.
+IF %ERRORLEVEL% NEQ 0 (
+ call :ColorText "[INFO] " BLUE
+ echo Shared volume '%SHARED_VOLUME_NAME%' does not exist. Creating it...
+ echo.
+ docker volume create "%SHARED_VOLUME_NAME%"
+ IF %ERRORLEVEL% NEQ 0 (
+ call :ColorText "[ERROR]" RED
+ echo Failed to create shared volume '%SHARED_VOLUME_NAME%'.
+ echo Please check Docker daemon status and permissions.
+ pause
+ goto :end
+ ) ELSE (
+ call :ColorText "[SUCCESS] " GREEN
+ echo Shared volume '%SHARED_VOLUME_NAME%' created.
+ )
+) ELSE (
+ call :ColorText "[FOUND] " GREEN
+ echo Shared volume '%SHARED_VOLUME_NAME%' exists.
+)
+echo.
+
+call :ColorText "[INFO] " BLUE
+echo Setting permissions on shared volume '%SHARED_VOLUME_NAME%'...
+docker run --rm ^
+ -v "%SHARED_VOLUME_NAME%:/shared" ^
+ alpine sh -c "mkdir -p /shared && chown -R %VOLUME_UID%:%VOLUME_GID% /shared && chmod 775 /shared"
+
+IF %ERRORLEVEL% NEQ 0 (
+ echo.
+ call :ColorText "[ERROR]" RED
+ echo Failed to set shared volume permissions.
+ echo Please check your Docker volume and user/group ID configurations.
+ pause
+ goto :end
+)
+echo.
+
+call :ColorText "[SUCCESS] " GREEN
+echo Shared volume permissions set.
+echo.
+
docker compose up -d
if errorlevel 1 (
@@ -212,13 +417,14 @@ if errorlevel 1 (
pause
goto :end
)
+echo.
echo ============================================
echo SETUP COMPLETED SUCCESSFULLY!
echo ============================================
echo.
-echo Your mediaServer will be available at https://app.test OR https://YOUR.URL
+echo Your mediaServer will be available at https://%APP_HOST%
echo.
-echo To add audio or video to your server, put the files in ./data/media organised by /CATEGORY/FOLDER/VIDEO.mp4
+echo To add audio or video to your server, put the files in ./data/media organised by /LIBRARY/FOLDER/VIDEO.mp4
:end
diff --git a/startDocker.sh b/startDocker.sh
index 6242c041..874f2d41 100644
--- a/startDocker.sh
+++ b/startDocker.sh
@@ -1,11 +1,21 @@
#!/bin/bash
-# Define color variables
+# --- Configuration ---
+SHARED_VOLUME_NAME="mediaserver-shared-env"
+VOLUME_UID="9999"
+VOLUME_GID="9999"
+ENV_FILE="./.env" # Path to your .env file
+FALLBACK_DEFAULT_DOMAIN="app.test" # Default if APP_URL is not found in .env
+NGINX_CONF_FILE="docker/etc/nginx/conf.d/default.conf" # Path to Nginx config
+# --- End Configuration ---
+
+# --- Define color variables ---
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
RESET='\033[0m' # Reset color
+# --- End color variables ---
echo
echo "============================================"
@@ -13,7 +23,20 @@ echo " mediaServer Docker Setup "
echo "============================================"
echo
-echo -e "${YELLOW}[STEP 1/5]${RESET} Verifying required files and folders..."
+# --- Check for CI/CD or auto-default mode ---
+IS_CI_MODE=false
+if [[ "$1" == "--auto-default" ]]; then
+ IS_CI_MODE=true
+ echo "${BLUE}[INFO]${RESET} Auto-default mode enabled via command-line argument."
+ shift # Consume the argument if it's explicitly passed
+elif [[ -n "$CI" || -n "$GITHUB_ACTIONS" || -n "$GITLAB_CI" || -n "$JENKINS_URL" ]]; then
+ IS_CI_MODE=true
+ echo "${BLUE}[INFO]${RESET} Auto-default mode enabled (CI/CD environment detected)."
+fi
+echo
+# --- End CI/CD check ---
+
+echo -e "${YELLOW}[STEP 1/6]${RESET} Verifying required files and folders..."
echo
# Check for docker-compose.yaml
@@ -27,7 +50,7 @@ fi
echo
# Check for nginx configuration
-if [ ! -f "docker/etc/nginx/conf.d/default.conf" ]; then
+if [ ! -f "$NGINX_CONF_FILE" ]; then
echo -e "${RED}[ERROR]${RESET} Missing 'docker/etc/nginx/conf.d/default.conf' file."
echo "Please ensure this file is present in the correct directory."
exit 1
@@ -44,8 +67,7 @@ if [ ! -f "docker/etc/caddy/Caddyfile" ]; then
else
echo -e "${GREEN}[FOUND]${RESET} 'Caddyfile' configuration file."
echo "NOTE: Make sure to replace 'app.test' with your website URL in:"
- echo " - '/docker/etc/caddy/Caddyfile'"
- echo " - '.env'"
+ echo " - '/docker/etc/caddy/Caddyfile' or wherever your reverse proxy is"
fi
echo
@@ -57,9 +79,9 @@ if [ ! -f "docker/.env.docker" ]; then
fi
# Create .env if it doesn't exist
-if [ ! -f ".env" ]; then
+if [ ! -f "$ENV_FILE" ]; then
echo -e "${BLUE}[INFO]${RESET} '.env' file not found. Creating from '.env.docker'..."
- cp "docker/.env.docker" ".env"
+ cp "docker/.env.docker" "$ENV_FILE"
if [ $? -ne 0 ]; then
echo -e "${RED}[ERROR]${RESET} Failed to create '.env' file."
exit 1
@@ -126,51 +148,135 @@ else
fi
echo
-echo -e "${YELLOW}[STEP 2/5]${RESET} Stopping and cleaning up Existing mediaServer Docker containers..."
+echo -e "${YELLOW}[STEP 2/6]${RESET} Setting up user config..."
+echo
+
+# Get existing APP_HOST or use default
+CURRENT_APP_HOST=""
+if [ -f "$ENV_FILE" ]; then
+ CURRENT_APP_HOST=$(grep "^APP_HOST=" "$ENV_FILE" | sed -E 's/^APP_HOST="?([^"]*)"?/\1/')
+fi
+
+CURRENT_DOMAIN_DEFAULT="${FALLBACK_DEFAULT_DOMAIN}"
+if [[ -n "$CURRENT_APP_HOST" ]]; then
+ CURRENT_DOMAIN_DEFAULT="$CURRENT_APP_HOST"
+fi
+
+if $IS_CI_MODE; then
+ USER_DOMAIN="$CURRENT_DOMAIN_DEFAULT"
+ echo "${BLUE}[INFO]${RESET} Automatically using default domain: ${USER_DOMAIN}"
+ echo
+else
+ # Ask for domain name with a default
+ echo -e "${YELLOW}[CONFIG]${RESET} Enter your app domain (URL)..."
+ echo -e "${YELLOW}[CONFIG]${RESET} Current value: ${CURRENT_DOMAIN_DEFAULT} (Press enter to use this)..."
+ echo
+ read -p "Domain: " USER_DOMAIN
+ echo
+fi
+
+APP_HOST="${USER_DOMAIN:-$CURRENT_DOMAIN_DEFAULT}" # Use current default if user input is empty
+
+echo -e "${BLUE}[INFO]${RESET} Setting APP_HOST to: ${APP_HOST}"
+echo
+
+# Update APP_HOST in .env file using sed
+sed -i "s|^APP_HOST=.*|APP_HOST=\"${APP_HOST}\"|" "$ENV_FILE"
+if [ $? -ne 0 ]; then
+ echo -e "${RED}[ERROR]${RESET} Failed to update APP_HOST in ${ENV_FILE}."
+ echo "Please check the .env file format or script permissions."
+ exit 1
+else
+ echo -e "${GREEN}[SUCCESS]${RESET} APP_HOST updated in ${ENV_FILE}."
+fi
+echo
+
+ESCAPED_APP_HOST=$(echo "$APP_HOST" | sed 's/\./\\./g') # Escape dots
+
+sed -i "s#valid_referers 127.0.0.1, .*;#valid_referers 127.0.0.1, ${ESCAPED_APP_HOST};#" "$NGINX_CONF_FILE"
+
+if [ $? -ne 0 ]; then
+ echo -e "${RED}[ERROR]${RESET} Failed to update valid_referers in ${NGINX_CONF_FILE}."
+ echo "Please check the Nginx config file format or script permissions."
+ exit 1
+else
+ echo -e "${GREEN}[SUCCESS]${RESET} Nginx valid_referers updated to include: ${APP_HOST}"
+fi
+echo
+
+echo -e "${YELLOW}[STEP 3/6]${RESET} Stopping and cleaning up Existing mediaServer Docker containers..."
+echo
+
docker compose down
if [ $? -ne 0 ]; then
- echo
echo -e "${RED}[ERROR]${RESET} Failed to bring down Docker containers."
echo "Please check your Docker setup and try again."
exit 1
fi
-echo
echo -e "${GREEN}[SUCCESS]${RESET} Docker containers stopped and cleaned up."
echo
-echo -e "${YELLOW}[STEP 3/5]${RESET} Pruning Docker volumes..."
+echo -e "${YELLOW}[STEP 4/6]${RESET} Pruning Docker volumes..."
echo
docker volume prune -f
if [ $? -ne 0 ]; then
- echo
echo -e "${RED}[ERROR]${RESET} Failed to prune Docker volumes."
echo "Please check your Docker setup and try again."
exit 1
fi
-echo
echo -e "${GREEN}[SUCCESS]${RESET} Docker volumes pruned."
echo
-echo -e "${YELLOW}[STEP 4/5]${RESET} Pulling latest Docker images..."
+echo -e "${YELLOW}[STEP 5/6]${RESET} Pulling latest Docker images..."
echo
docker compose pull
if [ $? -ne 0 ]; then
- echo
echo -e "${RED}[ERROR]${RESET} Failed to pull Docker images."
echo "Please check your internet connection and Docker setup."
exit 1
else
- echo
echo -e "${GREEN}[SUCCESS]${RESET} Docker images pulled successfully."
echo
fi
-echo -e "${YELLOW}[STEP 5/5]${RESET} Building docker compose..."
+echo -e "${YELLOW}[STEP 6/6]${RESET} Building docker compose..."
echo
+
+# Check for shared docker volume
+if ! docker volume inspect "$SHARED_VOLUME_NAME" &>/dev/null; then
+ echo -e "${BLUE}[INFO]${RESET} Shared volume '$SHARED_VOLUME_NAME' does not exist. Creating it..."
+ docker volume create "$SHARED_VOLUME_NAME"
+ if [ $? -ne 0 ]; then
+ echo -e "${RED}[ERROR]${RESET} Failed to create shared volume '$SHARED_VOLUME_NAME'."
+ echo "Please check Docker daemon status and permissions."
+ exit 1
+ else
+ echo -e "${GREEN}[SUCCESS]${RESET} Shared volume '$SHARED_VOLUME_NAME' created."
+ fi
+else
+ echo -e "${GREEN}[FOUND]${RESET} Shared volume '$SHARED_VOLUME_NAME' exists."
+fi
+echo
+
+
+# Create a temporary container to set permissions on shared volume
+echo -e "${BLUE}[INFO]${RESET} Setting permissions on shared volume '$SHARED_VOLUME_NAME'..."
+echo
+docker run --rm \
+ -v "${SHARED_VOLUME_NAME}:/shared" \
+ alpine sh -c "mkdir -p /shared && chown -R ${VOLUME_UID}:${VOLUME_GID} /shared && chmod 775 /shared && echo 'Volume permissions set.'"
+
+if [ $? -ne 0 ]; then
+ echo -e "${RED}[ERROR]${RESET} Failed to set shared volume permissions."
+ echo "Please check your Docker volume and user/group ID configurations."
+ exit 1
+fi
+echo -e "${GREEN}[SUCCESS]${RESET} Shared volume permissions set."
+echo
+
docker compose up -d
if [ $? -ne 0 ]; then
- echo
echo -e "${RED}[ERROR]${RESET} Failed to build docker compose."
echo "Please check your configuration."
exit 1
@@ -180,6 +286,6 @@ echo "============================================"
echo -e "${GREEN} SETUP COMPLETED SUCCESSFULLY!${RESET}"
echo "============================================"
echo
-echo "Your mediaServer will be available at https://app.test OR https://YOUR.URL"
+echo "Your mediaServer will be available at https://$APP_HOST"
echo
-echo "To add audio or video to your server, put the files in ./data/media organised by /CATEGORY/FOLDER/VIDEO.mp4"
+echo "To add audio or video to your server, put the files in ./data/media organised by /LIBRARY/FOLDER/VIDEO.mp4"