clash-linux commited on
Commit
6e430cc
·
verified ·
1 Parent(s): 1f82b95

Upload Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +110 -102
Dockerfile CHANGED
@@ -5,8 +5,9 @@ ARG APP_HOME=/home/node/app
5
  ARG PLUGINS="" # Comma-separated list of plugin git URLs
6
 
7
  # Install system dependencies
8
- # Keep git for cloning
9
- # awk should be available via busybox in alpine
 
10
  RUN apk add --no-cache gcompat tini git unzip wget
11
 
12
  # Create app directory
@@ -40,108 +41,115 @@ RUN \
40
  echo "No package.json found in root, skipping base npm install."; \
41
  fi
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  # Ensure the node user owns the application directory and its contents
44
  RUN chown -R node:node ${APP_HOME}
45
 
46
- # Switch to non-root user
47
- USER node
48
-
49
  EXPOSE 8000
50
 
51
- # Entrypoint: Install plugins, Read config, Configure git, Run server
52
- # Using shell form for ENTRYPOINT to avoid potential JSON parsing issues
53
- ENTRYPOINT tini -- sh -c '
54
- # --- BEGIN: Install Plugins at Runtime ---
55
- if [ -n "$PLUGINS" ]; then
56
- echo "--- Found PLUGINS environment variable, attempting to install plugins ---";
57
- # Ensure plugin dir exists
58
- mkdir -p plugins;
59
- # Use temp var and add trailing comma for loop robustness
60
- temp_plugins_var="$PLUGINS,";
61
- while [ -n "$temp_plugins_var" ]; do
62
- plugin_url="${temp_plugins_var%%,*}"; # Get first element
63
- temp_plugins_var="${temp_plugins_var#*,}"; # Remove first element
64
-
65
- # Trim leading/trailing whitespace using awk
66
- plugin_url=$(echo "$plugin_url" | awk '''{$1=$1;print}''');
67
-
68
- if [ -z "$plugin_url" ]; then continue; fi;
69
-
70
- # Extract plugin name: remove .git suffix, then get basename
71
- plugin_name_tmp="${plugin_url%.git}";
72
- plugin_name="${plugin_name_tmp##*/}";
73
-
74
- if [ -z "$plugin_name" ]; then
75
- echo "WARN: Could not extract plugin name from URL: $plugin_url, skipping.";
76
- continue;
77
- fi;
78
-
79
- plugin_dir="./plugins/$plugin_name";
80
- echo "--- Processing plugin: $plugin_name from $plugin_url ---";
81
-
82
- # Clone (remove existing first)
83
- rm -rf "$plugin_dir";
84
- echo "Cloning into $plugin_dir...";
85
- if git clone --depth 1 "$plugin_url" "$plugin_dir"; then
86
- echo "Cloned successfully.";
87
- # Install dependencies if package.json exists
88
- if [ -f "$plugin_dir/package.json" ]; then
89
- echo "Found package.json, installing dependencies for $plugin_name...";
90
- # Run npm install within the plugin directory
91
- (cd "$plugin_dir" && npm install --no-audit --no-fund --loglevel=error --no-progress --omit=dev --force && npm cache clean --force);
92
- if [ $? -ne 0 ]; then
93
- echo "WARN: Failed to install dependencies for $plugin_name";
94
- else
95
- echo "Dependencies installed for $plugin_name.";
96
- fi;
97
- else
98
- echo "No package.json found for $plugin_name, skipping dependency install.";
99
- fi;
100
- else
101
- echo "WARN: Failed to clone $plugin_name from $plugin_url, skipping further processing.";
102
- fi;
103
- done;
104
- echo "--- Finished processing plugins ---";
105
- else
106
- echo "--- PLUGINS environment variable not set or empty, skipping plugin installation ---";
107
- fi;
108
- # --- END: Install Plugins at Runtime ---
109
-
110
- # --- Original Entrypoint logic ---
111
- echo "--- Checking for CONFIG_YAML environment variable ---";
112
- # Ensure the CWD has correct permissions for writing config.yaml
113
- # mkdir -p ./config && chown node:node ./config; # Removed mkdir
114
- if [ -n "$CONFIG_YAML" ]; then
115
- echo "Environment variable CONFIG_YAML found. Writing to ./config.yaml (root directory)...";
116
- # Write directly to ./config.yaml in the CWD
117
- # Need to ensure node user has write permission here. CWD should be owned by node due to chown and USER node.
118
- printf '%s\n' "$CONFIG_YAML" > ./config.yaml &&
119
- echo "Config written to ./config.yaml successfully.";
120
- # --- BEGIN DEBUG: Print the written config file ---
121
- echo "--- Verifying written ./config.yaml ---";
122
- cat ./config.yaml;
123
- echo "--- End of ./config.yaml ---";
124
- # --- END DEBUG ---
125
- else
126
- echo "Warning: Environment variable CONFIG_YAML is not set or empty. Attempting to copy default config...";
127
- # Copy default if ENV VAR is missing and the example exists
128
- if [ -f "./public/config.yaml.example" ]; then
129
- # Copy default to ./config.yaml in the CWD
130
- cp "./public/config.yaml.example" "./config.yaml" &&
131
- echo "Copied default config to ./config.yaml";
132
- else
133
- echo "Warning: Default config ./public/config.yaml.example not found.";
134
- fi;
135
- fi;
136
-
137
- # --- BEGIN: Configure Git default identity at Runtime ---
138
- echo "--- Configuring Git default user identity at runtime ---";
139
- git config --global user.name "SillyTavern Sync" &&
140
- git config --global user.email "sillytavern-sync@example.com";
141
- echo "--- Git identity configured for runtime user. ---";
142
- # --- END: Configure Git default identity at Runtime ---
143
-
144
- echo "Starting SillyTavern server directly...";
145
- # Execute node server directly. Since we used USER node, this should run as the node user.
146
- exec node server.js;
147
- '
 
5
  ARG PLUGINS="" # Comma-separated list of plugin git URLs
6
 
7
  # Install system dependencies
8
+ # Add unzip for extracting the application code
9
+ # Keep git for potential use by scripts or future plugin updates
10
+ # Add wget to download the zip file
11
  RUN apk add --no-cache gcompat tini git unzip wget
12
 
13
  # Create app directory
 
41
  echo "No package.json found in root, skipping base npm install."; \
42
  fi
43
 
44
+ # Go back to the main app directory (redundant but safe)
45
+ WORKDIR ${APP_HOME}
46
+
47
+ # Create config directory. config.yaml will be handled at runtime by ENTRYPOINT
48
+ RUN mkdir -p config
49
+
50
+ # Pre-compile public libraries (build-lib.js should be in the unzipped structure)
51
+ RUN \
52
+ echo "*** Run Webpack ***" && \
53
+ # Check if build-lib.js exists before running
54
+ if [ -f "./docker/build-lib.js" ]; then \
55
+ node "./docker/build-lib.js"; \
56
+ elif [ -f "./build-lib.js" ]; then \
57
+ node "./build-lib.js"; \
58
+ else \
59
+ echo "build-lib.js not found, skipping Webpack build."; \
60
+ fi
61
+
62
+ # Cleanup unnecessary files (like the docker dir if it exists in the zip) and make entrypoint executable
63
+ # This block is removed as we no longer use docker-entrypoint.sh
64
+ # RUN \
65
+ # echo "*** Cleanup and Permissions ***" && \
66
+ # ...
67
+
68
+ # Fix potential git safe.directory issues if git commands are run later by scripts
69
+ RUN git config --global --add safe.directory "${APP_HOME}"
70
+
71
  # Ensure the node user owns the application directory and its contents
72
  RUN chown -R node:node ${APP_HOME}
73
 
 
 
 
74
  EXPOSE 8000
75
 
76
+ # Entrypoint: Read config from environment variable CONFIG_YAML if set, copy default if not, configure git, then run node server.js directly
77
+ ENTRYPOINT ["tini", "--", "sh", "-c", " \
78
+ echo '--- Checking for CONFIG_YAML environment variable ---'; \
79
+ # Ensure the CWD has correct permissions for writing config.yaml
80
+ # mkdir -p ./config && chown node:node ./config; # Removed mkdir
81
+ if [ -n \"$CONFIG_YAML\" ]; then \
82
+ echo 'Environment variable CONFIG_YAML found. Writing to ./config.yaml (root directory)...'; \
83
+ # Write directly to ./config.yaml in the CWD
84
+ printf '%s\n' \"$CONFIG_YAML\" > ./config.yaml && \
85
+ chown node:node ./config.yaml && \
86
+ echo 'Config written to ./config.yaml and permissions set successfully.'; \
87
+ # --- BEGIN DEBUG: Print the written config file ---
88
+ echo '--- Verifying written ./config.yaml ---'; \
89
+ cat ./config.yaml; \
90
+ echo '--- End of ./config.yaml ---'; \
91
+ # --- END DEBUG ---
92
+ else \
93
+ echo 'Warning: Environment variable CONFIG_YAML is not set or empty. Attempting to copy default config...'; \
94
+ # Copy default if ENV VAR is missing and the example exists
95
+ if [ -f \"./public/config.yaml.example\" ]; then \
96
+ # Copy default to ./config.yaml in the CWD
97
+ cp \"./public/config.yaml.example\" \"./config.yaml\" && \
98
+ chown node:node ./config.yaml && \
99
+ echo 'Copied default config to ./config.yaml'; \
100
+ else \
101
+ echo 'Warning: Default config ./public/config.yaml.example not found.'; \
102
+ fi; \
103
+ fi; \
104
+
105
+ # --- BEGIN: Configure Git default identity at Runtime --- \
106
+ echo '--- Configuring Git default user identity at runtime ---'; \
107
+ git config --global user.name \"SillyTavern Sync\" && \
108
+ git config --global user.email \"sillytavern-sync@example.com\"; \
109
+ echo '--- Git identity configured for runtime user. ---'; \
110
+ # --- END: Configure Git default identity at Runtime --- \
111
+
112
+ # --- BEGIN: Dynamically Install Plugins at Runtime --- \
113
+ echo '--- Checking for PLUGINS environment variable ---'; \
114
+ if [ -n \"$PLUGINS\" ]; then \
115
+ echo \"*** Installing Plugins specified in PLUGINS environment variable: $PLUGINS ***\" && \
116
+ # Ensure plugins directory exists
117
+ mkdir -p ./plugins && chown node:node ./plugins && \
118
+ # Set comma as delimiter
119
+ IFS=',' && \
120
+ # Loop through each plugin URL
121
+ for plugin_url in $PLUGINS; do \
122
+ # Trim leading/trailing whitespace
123
+ plugin_url=$(echo \"$plugin_url\" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') && \
124
+ if [ -z \"$plugin_url\" ]; then continue; fi && \
125
+ # Extract plugin name
126
+ plugin_name_git=$(basename \"$plugin_url\") && \
127
+ plugin_name=${plugin_name_git%.git} && \
128
+ plugin_dir=\"./plugins/$plugin_name\" && \
129
+ echo \"--- Installing plugin: $plugin_name from $plugin_url into $plugin_dir ---\" && \
130
+ # Remove existing dir if it exists
131
+ rm -rf \"$plugin_dir\" && \
132
+ # Clone the plugin (run as root, fix perms later)
133
+ git clone --depth 1 \"$plugin_url\" \"$plugin_dir\" && \
134
+ if [ -f \"$plugin_dir/package.json\" ]; then \
135
+ echo \"--- Installing dependencies for $plugin_name ---\" && \
136
+ (cd \"$plugin_dir\" && npm install --no-audit --no-fund --loglevel=error --no-progress --omit=dev --force && npm cache clean --force) || echo \"WARN: Failed to install dependencies for $plugin_name\"; \
137
+ else \
138
+ echo \"--- No package.json found for $plugin_name, skipping dependency install. ---\"; \
139
+ fi || echo \"WARN: Failed to clone $plugin_name from $plugin_url, skipping...\"; \
140
+ done && \
141
+ # Reset IFS
142
+ unset IFS && \
143
+ # Fix permissions for plugins directory after installation
144
+ echo \"--- Setting permissions for plugins directory ---\" && \
145
+ chown -R node:node ./plugins && \
146
+ echo \"*** Plugin installation finished. ***\"; \
147
+ else \
148
+ echo 'PLUGINS environment variable is not set or empty, skipping runtime plugin installation.'; \
149
+ fi; \
150
+ # --- END: Dynamically Install Plugins at Runtime --- \
151
+
152
+ echo 'Starting SillyTavern server directly...'; \
153
+ # Execute node server directly, bypassing docker-entrypoint.sh
154
+ exec node server.js; \
155
+ "]