Skip to content

OpenStock is an open-source alternative to expensive market platforms. Track real-time prices, set personalized alerts, and explore detailed company insights — built openly, for everyone, forever free.

fix(docker): build without a database or secrets, read keys at runtime

ravixalgorithm531fd90ee90a
diff --git a/Dockerfile b/Dockerfile
index 6072473..0ea471b 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -17,11 +17,8 @@ RUN npm install
# Copy all project files
COPY . .
-# NEXT_PUBLIC_* variables are inlined at build time, so they must be available
-# during `npm run build` (Railway passes service variables as build args)
-ARG NEXT_PUBLIC_FINNHUB_API_KEY
-ENV NEXT_PUBLIC_FINNHUB_API_KEY=$NEXT_PUBLIC_FINNHUB_API_KEY
-
+# The build needs no secrets or database: pass every variable at runtime (FINNHUB_API_KEY, not NEXT_PUBLIC_*,
+# which Next.js freezes at build time and would record in the image layers).
# Build the Next.js application
RUN npm run build
# Or if using pnpm:
diff --git a/README.md b/README.md
index 54239e7..7ec43d6 100644
--- a/README.md
+++ b/README.md
@@ -267,15 +267,15 @@ The one-click template provisions OpenStock and a MongoDB database and prompts f
OpenStock ships with a [`railway.json`](railway.json) so it can be deployed on [Railway](https://railway.com) using the existing `Dockerfile`, with a healthcheck on `/sign-in` and automatic restarts on failure.
-1) Create a new project on Railway and add a **MongoDB** database (`+ New → Database → MongoDB`). Make sure its TCP Proxy is enabled so `MONGO_PUBLIC_URL` is available.
+1) Create a new project on Railway and add a **MongoDB** database (`+ New → Database → MongoDB`).
2) Add a service from this GitHub repository (`+ New → GitHub Repo`). Railway detects `railway.json` and builds with the `Dockerfile`.
3) In the service **Variables**, set:
```env
-MONGODB_URI=${{MongoDB.MONGO_PUBLIC_URL}}
+MONGODB_URI=${{MongoDB.MONGO_URL}}
BETTER_AUTH_SECRET=${{secret(32)}}
BETTER_AUTH_URL=https://${{RAILWAY_PUBLIC_DOMAIN}}
-NEXT_PUBLIC_FINNHUB_API_KEY=your_finnhub_key
+FINNHUB_API_KEY=your_finnhub_key
GEMINI_API_KEY=your_gemini_api_key
INNGEST_SIGNING_KEY=your_inngest_signing_key
@@ -285,8 +285,8 @@ NODEMAILER_PASSWORD=your_gmail_app_password
4) Under **Settings → Networking**, generate a public domain, then redeploy.
Notes
-- `NEXT_PUBLIC_FINNHUB_API_KEY` is inlined at build time; Railway passes it to the Docker build as a build arg, so redeploy after changing it.
-- Use `MONGO_PUBLIC_URL` (not `MONGO_URL`): `next build` connects to MongoDB while collecting page data, and Railway's private network is not available during builds.
+- All variables are read when the container starts; nothing is passed to the Docker build, so no key ends up in the image. `NEXT_PUBLIC_FINNHUB_API_KEY` still works as a fallback name.
+- The build needs no database or secrets, so the private `MONGO_URL` works and the MongoDB TCP proxy can stay off.
- For scheduled jobs (news summaries, alerts), add your Railway domain as an app in the [Inngest dashboard](https://app.inngest.com) pointing to `https://<your-domain>/api/inngest`.
- See [Environment Variables](#environment-variables) for the optional variables (Adanos, MiniMax, Kit, etc.).
diff --git a/lib/actions/finnhub.actions.ts b/lib/actions/finnhub.actions.ts
index e53c875..42c5a87 100644
--- a/lib/actions/finnhub.actions.ts
+++ b/lib/actions/finnhub.actions.ts
@@ -9,7 +9,10 @@ import { getSession } from '@/lib/better-auth/auth';
const FINNHUB_BASE_URL = 'https://finnhub.io/api/v1';
// Key pool: each free key has its own 60 req/min, so N keys = N x the quota. Rotated per request.
-const FINNHUB_KEYS = (process.env.FINNHUB_API_KEYS || process.env.NEXT_PUBLIC_FINNHUB_API_KEY || process.env.FINNHUB_API_KEY || '')
+// Read through an alias: `next build` freezes a literal process.env.NEXT_PUBLIC_* into the bundle, so Docker and
+// Railway images (built without secrets) would ignore a NEXT_PUBLIC_FINNHUB_API_KEY set at runtime.
+const env = process.env;
+const FINNHUB_KEYS = (env.FINNHUB_API_KEYS || env.NEXT_PUBLIC_FINNHUB_API_KEY || env.FINNHUB_API_KEY || '')
.split(',')
.map((k) => k.trim())
.filter(Boolean);
diff --git a/lib/better-auth/auth.ts b/lib/better-auth/auth.ts
index 03afe76..a6d6ba3 100644
--- a/lib/better-auth/auth.ts
+++ b/lib/better-auth/auth.ts
@@ -83,7 +83,19 @@ export const getAuth = async () => {
return authInstance;
}
-export const auth = await getAuth();
+type Auth = ReturnType<typeof createAuth>;
+
+// Connects on first use, not at import: `next build` imports this module while collecting page data, and a
+// top-level `await getAuth()` made every build (Docker, Railway, CI) need a reachable MongoDB.
+export const auth = {
+ api: new Proxy({} as Auth['api'], {
+ get: (_, method) => async (...args: unknown[]) => {
+ const { api } = await getAuth();
+ return (api[method as keyof Auth['api']] as (...a: unknown[]) => unknown)(...args);
+ },
+ }),
+ handler: async (request: Request) => (await getAuth()).handler(request),
+};
// One session lookup per request, shared by layouts, pages and actions
export const getSession = cache(async () => auth.api.getSession({ headers: await headers() }));