Redirect throws error in server action when deployed with docker
Answered
American Crocodile posted this in #help-forum
American CrocodileOP
When attempting to redirect within a server action, I get the following error only if the project is run inside docker. If I run it without docker everything works great. Oddly enough, the redirect still happens in the browser. My guess is that during the redirect, Nextjs is trying to call the endpoint to render the redirect server side and once that fails it tells the client to just do it client side.
Redirect Error
Server Action
Redirect Error
failed to get redirect response TypeError: fetch failed
at node:internal/deps/undici/undici:12618:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async rC (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:15:5118)
at async rR (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:16:756)
at async r7 (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:18:1144)
at async doRender (/app/node_modules/next/dist/server/base-server.js:1438:30)
at async cacheEntry.responseCache.get.routeKind (/app/node_modules/next/dist/server/base-server.js:1599:28)
at async NextNodeServer.renderToResponseWithComponentsImpl (/app/node_modules/next/dist/server/base-server.js:1507:28)
at async NextNodeServer.renderPageComponent (/app/node_modules/next/dist/server/base-server.js:1931:24)
at async NextNodeServer.renderToResponseImpl (/app/node_modules/next/dist/server/base-server.js:1969:32) {
cause: Error: connect ECONNREFUSED ::1:3000
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16)
at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:128:17) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 3000
}
}Server Action
'use server';
import { redirect } from 'next/navigation';
export async function requestPasswordReset() {
// form handling happens here
// this causes error
redirect('/forgot-password/success');
}Answered by American Crocodile
I have found a solution. When hosting NextJS in a docker. The official Dockerfile I had seen included a line at the bottom that says
Solution
Commenting out or removing the line
ENV HOSTNAME="0.0.0.0". However, in my setup, this caused Nextjs to attempt to call it self at the address 0.0.0.0:3000. In reality however my docker container needs to call itself using its own container name,Solution
Commenting out or removing the line
ENV HOSTNAME="0.0.0.0" from my Dockerfile resolved and Nextjs was about to talk to itself without any issues.1 Reply
American CrocodileOP
I have found a solution. When hosting NextJS in a docker. The official Dockerfile I had seen included a line at the bottom that says
Solution
Commenting out or removing the line
ENV HOSTNAME="0.0.0.0". However, in my setup, this caused Nextjs to attempt to call it self at the address 0.0.0.0:3000. In reality however my docker container needs to call itself using its own container name,Solution
Commenting out or removing the line
ENV HOSTNAME="0.0.0.0" from my Dockerfile resolved and Nextjs was about to talk to itself without any issues.Answer