Skip to content

Commit 6603533

Browse files
authored
fix: docker prod env variable fix (stackblitz-labs#1170)
* fix: docker prod env variable fix * lint and typecheck * removed hardcoded tag
1 parent 5a0489f commit 6603533

File tree

6 files changed

+63
-20
lines changed

6 files changed

+63
-20
lines changed

app/lib/modules/llm/providers/lmstudio.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class LMStudioProvider extends BaseProvider {
4040
* Running in Server
4141
* Backend: Check if we're running in Docker
4242
*/
43-
const isDocker = process.env.RUNNING_IN_DOCKER === 'true';
43+
const isDocker = process?.env?.RUNNING_IN_DOCKER === 'true' || serverEnv?.RUNNING_IN_DOCKER === 'true';
4444

4545
baseUrl = isDocker ? baseUrl.replace('localhost', 'host.docker.internal') : baseUrl;
4646
baseUrl = isDocker ? baseUrl.replace('127.0.0.1', 'host.docker.internal') : baseUrl;
@@ -58,7 +58,7 @@ export default class LMStudioProvider extends BaseProvider {
5858
}
5959
getModelInstance: (options: {
6060
model: string;
61-
serverEnv: Env;
61+
serverEnv?: Env;
6262
apiKeys?: Record<string, string>;
6363
providerSettings?: Record<string, IProviderSetting>;
6464
}) => LanguageModelV1 = (options) => {
@@ -75,16 +75,17 @@ export default class LMStudioProvider extends BaseProvider {
7575
throw new Error('No baseUrl found for LMStudio provider');
7676
}
7777

78+
const isDocker = process.env.RUNNING_IN_DOCKER === 'true' || serverEnv?.RUNNING_IN_DOCKER === 'true';
79+
7880
if (typeof window === 'undefined') {
79-
const isDocker = process.env.RUNNING_IN_DOCKER === 'true';
8081
baseUrl = isDocker ? baseUrl.replace('localhost', 'host.docker.internal') : baseUrl;
8182
baseUrl = isDocker ? baseUrl.replace('127.0.0.1', 'host.docker.internal') : baseUrl;
8283
}
8384

8485
logger.debug('LMStudio Base Url used: ', baseUrl);
8586

8687
const lmstudio = createOpenAI({
87-
baseUrl: `${baseUrl}/v1`,
88+
baseURL: `${baseUrl}/v1`,
8889
apiKey: '',
8990
});
9091

app/lib/modules/llm/providers/ollama.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default class OllamaProvider extends BaseProvider {
6363
* Running in Server
6464
* Backend: Check if we're running in Docker
6565
*/
66-
const isDocker = process.env.RUNNING_IN_DOCKER === 'true';
66+
const isDocker = process?.env?.RUNNING_IN_DOCKER === 'true' || serverEnv?.RUNNING_IN_DOCKER === 'true';
6767

6868
baseUrl = isDocker ? baseUrl.replace('localhost', 'host.docker.internal') : baseUrl;
6969
baseUrl = isDocker ? baseUrl.replace('127.0.0.1', 'host.docker.internal') : baseUrl;
@@ -83,7 +83,7 @@ export default class OllamaProvider extends BaseProvider {
8383
}
8484
getModelInstance: (options: {
8585
model: string;
86-
serverEnv: Env;
86+
serverEnv?: Env;
8787
apiKeys?: Record<string, string>;
8888
providerSettings?: Record<string, IProviderSetting>;
8989
}) => LanguageModelV1 = (options) => {
@@ -101,7 +101,7 @@ export default class OllamaProvider extends BaseProvider {
101101
throw new Error('No baseUrl found for OLLAMA provider');
102102
}
103103

104-
const isDocker = process.env.RUNNING_IN_DOCKER === 'true';
104+
const isDocker = process?.env?.RUNNING_IN_DOCKER === 'true' || serverEnv?.RUNNING_IN_DOCKER === 'true';
105105
baseUrl = isDocker ? baseUrl.replace('localhost', 'host.docker.internal') : baseUrl;
106106
baseUrl = isDocker ? baseUrl.replace('127.0.0.1', 'host.docker.internal') : baseUrl;
107107

app/routes/api.models.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ function getProviderInfo(llmManager: LLMManager) {
4141
export async function loader({
4242
request,
4343
params,
44+
context,
4445
}: {
4546
request: Request;
4647
params: { provider?: string };
48+
context: {
49+
cloudflare?: {
50+
env: Record<string, string>;
51+
};
52+
};
4753
}): Promise<Response> {
48-
const llmManager = LLMManager.getInstance(import.meta.env);
54+
const llmManager = LLMManager.getInstance(context.cloudflare?.env);
4955

5056
// Get client side maintained API keys and provider settings from cookies
5157
const cookieHeader = request.headers.get('Cookie');
@@ -63,7 +69,7 @@ export async function loader({
6369
if (provider) {
6470
const staticModels = provider.staticModels;
6571
const dynamicModels = provider.getDynamicModels
66-
? await provider.getDynamicModels(apiKeys, providerSettings, import.meta.env)
72+
? await provider.getDynamicModels(apiKeys, providerSettings, context.cloudflare?.env)
6773
: [];
6874
modelList = [...staticModels, ...dynamicModels];
6975
}
@@ -72,7 +78,7 @@ export async function loader({
7278
modelList = await llmManager.updateModelList({
7379
apiKeys,
7480
providerSettings,
75-
serverEnv: import.meta.env,
81+
serverEnv: context.cloudflare?.env,
7682
});
7783
}
7884

bindings.sh

+26-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,32 @@
22

33
bindings=""
44

5-
while IFS= read -r line || [ -n "$line" ]; do
6-
if [[ ! "$line" =~ ^# ]] && [[ -n "$line" ]]; then
7-
name=$(echo "$line" | cut -d '=' -f 1)
8-
value=$(echo "$line" | cut -d '=' -f 2-)
9-
value=$(echo $value | sed 's/^"\(.*\)"$/\1/')
10-
bindings+="--binding ${name}=${value} "
11-
fi
12-
done < .env.local
5+
# Function to extract variable names from the TypeScript interface
6+
extract_env_vars() {
7+
grep -o '[A-Z_]\+:' worker-configuration.d.ts | sed 's/://'
8+
}
9+
10+
# First try to read from .env.local if it exists
11+
if [ -f ".env.local" ]; then
12+
while IFS= read -r line || [ -n "$line" ]; do
13+
if [[ ! "$line" =~ ^# ]] && [[ -n "$line" ]]; then
14+
name=$(echo "$line" | cut -d '=' -f 1)
15+
value=$(echo "$line" | cut -d '=' -f 2-)
16+
value=$(echo $value | sed 's/^"\(.*\)"$/\1/')
17+
bindings+="--binding ${name}=${value} "
18+
fi
19+
done < .env.local
20+
else
21+
# If .env.local doesn't exist, use environment variables defined in .d.ts
22+
env_vars=($(extract_env_vars))
23+
# Generate bindings for each environment variable if it exists
24+
for var in "${env_vars[@]}"; do
25+
if [ -n "${!var}" ]; then
26+
bindings+="--binding ${var}=${!var} "
27+
fi
28+
done
29+
fi
1330

1431
bindings=$(echo $bindings | sed 's/[[:space:]]*$//')
1532

16-
echo $bindings
33+
echo $bindings

docker-compose.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,21 @@ services:
7272
- "5173:5173"
7373
command: pnpm run dev --host 0.0.0.0
7474
profiles: ["development", "default"]
75+
76+
app-prebuild:
77+
image: ghcr.io/stackblitz-labs/bolt.diy:latest
78+
ports:
79+
- "5173:5173"
80+
environment:
81+
- NODE_ENV=production
82+
- COMPOSE_PROFILES=production
83+
# No strictly needed but serving as hints for Coolify
84+
- PORT=5173
85+
- OLLAMA_API_BASE_URL=http://127.0.0.1:11434
86+
- DEFAULT_NUM_CTX=${DEFAULT_NUM_CTX:-32768}
87+
- RUNNING_IN_DOCKER=true
88+
extra_hosts:
89+
- "host.docker.internal:host-gateway"
90+
command: pnpm run dockerstart
91+
profiles:
92+
- prebuilt

worker-configuration.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
interface Env {
2-
DEFAULT_NUM_CTX:Settings;
2+
RUNNING_IN_DOCKER: Settings;
3+
DEFAULT_NUM_CTX: Settings;
34
ANTHROPIC_API_KEY: string;
45
OPENAI_API_KEY: string;
56
GROQ_API_KEY: string;

0 commit comments

Comments
 (0)