I have a SvelteKit project where it connects to my MongoDB in the hooks.server.ts
and my utility mongo.ts
file.
During build time for my Docker Compose, it attempts to connect MongoDB when the logs say they build the hooks.server.ts
file but ultimately fails to connect.
However, after it throws the error, the build completes the server ends up connected to the database anyway.
The issue here is that the build time is taking longer than necessary because it will only continue the build process after it times out.
Additionally, if the db is available separately and I build the SvelteKit app individually using docker build .
instead of letting docker compose up --build
handle it, this delay doesn't happen and the build doesn't throw the connection error.
mongo.ts
import { MongoClient } from 'mongodb';
import * as dotenv from 'dotenv';
dotenv.config();
const url: string = process.env.MONGO_URL ?? 'mongodb://localhost:4600/pc-comparison-site';
const client = new MongoClient(url, {
minPoolSize: 10
});
try {
await client.connect();
} catch (e) {
console.error(e);
}
export default client.db();
hooks.server.ts
import type { Handle } from '@sveltejs/kit';
import { Users } from '$db/collections';
export const handle: Handle = async ({ event, resolve }) => {
// get cookies from browser
const session = event.cookies.get('session');
if (!session) {
// if there is no session load page as normal
return await resolve(event);
}
// find the user based on the session
const user = await Users.findOne({ userAuthToken: session });
// if `user` exists set `events.local`
if (user) {
event.locals.user = {
name: user.username
};
}
// load page as normal
return await resolve(event);
};
This is the error that is thrown during docker compose build time:
#11 16.54 .svelte-kit/output/server/chunks/hooks.server.js 0.38 KiB
#11 67.69 MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:4600
#11 67.69 at Timeout._onTimeout (/app/node_modules/.pnpm/mongodb@4.11.0/node_modules/mongodb/lib/sdam/topology.js:293:38)
#11 67.69 at listOnTimeout (node:internal/timers:564:17)
#11 67.69 at process.processTimers (node:internal/timers:507:7) {
#11 67.69 reason: TopologyDescription {
#11 67.69 type: 'Unknown',
#11 67.69 servers: Map(1) { 'localhost:4600' => [ServerDescription] },
#11 67.69 stale: false,
#11 67.69 compatible: true,
#11 67.69 heartbeatFrequencyMS: 10000,
#11 67.69 localThresholdMS: 15,
#11 67.69 setName: null,
#11 67.69 maxElectionId: null,
#11 67.69 maxSetVersion: null,
#11 67.69 commonWireVersion: 0,
#11 67.69 logicalSessionTimeoutMinutes: null
#11 67.69 },
#11 67.69 code: undefined,
#11 67.69 [Symbol(errorLabels)]: Set(0) {}
#11 67.69 }
#11 69.82
Dockerfile
FROM docker.io/library/node:19-alpine
# setup directory for app
WORKDIR /app
# split COPY into two parts to take advantage of layering system
# builder will use cache if there are no changes in dependencies
COPY package.json pnpm-lock.yaml .
# install pnpm
RUN npm install -g pnpm
# install deps
RUN pnpm i
COPY . .
RUN pnpm run build
CMD node ./build/index.js
docker-compose.yml
version: '3.8'
services:
db:
image: mongo
ports:
- '4600:27017'
volumes:
- /pc-comparison-site-volume:/data/db
restart: always
app:
build: .
depends_on:
- db
ports:
- '3500:3000'
environment:
- MONGO_URL=mongodb://db:27017/pc-comparison-site
links:
- db
restart: always
Expectation: It shouldn't try to connect to the DB during build time in Docker. Possibly skip connection attempt if it knows it's during build time?
Result: Attempts to connect to DB during build time in Docker.