API calls for auth not working in docker container
I am using SvelteKit and I do have the hooks.server.ts file, and it works just fine using
pnpm run dev
and even when I build it and run pnpm run preview
. When I build a docker image however, the app loads just fine and everything works except for the /api/auth calls. I try with curl even and I just get the 404 page response. I have the base url set to localhost:3000, I am exposing the 3000:3000 port so everything matches and still nothing. Any ideas why?9 Replies
I am having the same trouble right now. I am currently investigating and will post any ideas here. Glad if anyone with knowledge could hop in!
I tested this stuff on my host system without containers now and came to the conclusion that it is indeed just a port collision.
A simple "npm run build" + "npm run preview", which uses vite under the hood in my setup, binds succesfully to default port 3000 on my system. App working and healthy. The by sveltekit suggested "node build" , for starting the real node server, yields a EADDRINUSE. Port occupation is visible in the image below. Indeed something is bound to these port the same way node tries to do it. But why is "npm run preview" which binds to the same port working? The next image shows how vite does this.
I am not firm in how port binding works in detail. But it is at least clear to me that vite does it in a way which does not collide with the background process on my machine. (Will investigate here some more)
Following the guide on sveltekit https://svelte.dev/docs/kit/adapter-node i will now try to change the Port that node tries to use.
Node servers • Docs • Svelte
Node servers • Svelte documentation
Got an answer to the question why preview works and node build fails: Vite binds to IPv6 interface ( the [::1] indicates that) while the process that blocks node is bound to all IPv4 interfaces.
And why the basic site works in docker but routing for better-auth not: If i try to start the node server manually in the container i get the same EADDRINUSE error as on my host. i cannot confirm right now that it is related, but i will try to fix the issue now on the host first, then move it into a container.
lsof indicates that a node process occupies the container port 3000.

So this is how npm run preview, aka vite preview, binds to the interfaces (the state that works) vs. how node build binds (which does not work for the default routing that better-auth establishes)
I think there is something going on with the IPv4 and IPv6 interfaces that are both occupied on the same port.

Have a new lead maybe. Under vite preview all requests to the better-auth endpoint paths are issued as http request. Under node they are somehow https.
Problem solved, there is a concept at work which i was unaware of: reverse proxy support.
the solution was to simulate the proxy headers. What i dont get right now is why sveltekit assumes that the target node server will be behind a proxy by default.
like always, read the docs to get enlightement!
https://svelte.dev/docs/kit/adapter-node
under the ORIGIN, PROTOCOL_HEADER,... section is a statement that makes a lot of sense now 🙂
Node servers • Docs • Svelte
Node servers • Svelte documentation