API route fetch fail when running on dockerized production build image
Answered
stefnto posted this in #help-forum
stefntoOP
I have dockerized my next 14 frontend service and when an api route is called I get this error
⨯ TypeError: fetch failed
2024-06-04 12:21:38 at node:internal/deps/undici/undici:12345:11
2024-06-04 12:21:38 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
2024-06-04 12:21:38 at async l (/app/.next/server/app/api/auth/route.js:1:3057)
2024-06-04 12:21:38 at async /app/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:34666
2024-06-04 12:21:38 at async eS.execute (/app/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:25813)
2024-06-04 12:21:38 at async eS.handle (/app/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:35920)
2024-06-04 12:21:38 at async doRender (/app/node_modules/next/dist/server/base-server.js:1377:42)
2024-06-04 12:21:38 at async cacheEntry.responseCache.get.routeKind (/app/node_modules/next/dist/server/base-server.js:1599:28)
2024-06-04 12:21:38 at async NextNodeServer.renderToResponseWithComponentsImpl (/app/node_modules/next/dist/server/base-server.js:1507:28)
2024-06-04 12:21:38 at async NextNodeServer.renderPageComponent (/app/node_modules/next/dist/server/base-server.js:1924:24) {
2024-06-04 12:21:38 cause: Error: connect ECONNREFUSED 127.0.0.1:8000
2024-06-04 12:21:38 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16)
2024-06-04 12:21:38 at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
2024-06-04 12:21:38 errno: -111,
2024-06-04 12:21:38 code: 'ECONNREFUSED',
2024-06-04 12:21:38 syscall: 'connect',
2024-06-04 12:21:38 address: '127.0.0.1',
2024-06-04 12:21:38 port: 8000
2024-06-04 12:21:38 }
2024-06-04 12:21:38 }
Answered by stefnto
Apparently when running the docker the --network="host" option should be set for the docker and machine running networks to be the same. Sadly this option is available only on linux (WSL supports this as well)
2 Replies
stefntoOP
Basically its the login client component which calls the api/auth, where in it there is a fetch to the backend where it runs on localhost:8000.
In the backend I've whitelisted localhost, 0.0.0.0 asnd 127.0.0.1.
If I rewrite the backend fetch call to happen inside my Login client component and not inside the api route, I dont get any error, so I suspect there is something going on inside the api route. Anyone had a similar error?
In the backend I've whitelisted localhost, 0.0.0.0 asnd 127.0.0.1.
If I rewrite the backend fetch call to happen inside my Login client component and not inside the api route, I dont get any error, so I suspect there is something going on inside the api route. Anyone had a similar error?
stefntoOP
Apparently when running the docker the --network="host" option should be set for the docker and machine running networks to be the same. Sadly this option is available only on linux (WSL supports this as well)
Answer