Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nextjs 13.4.13+ broke self-hosted docker setup #54133

Closed
1 task done
domosedov opened this issue Aug 16, 2023 · 60 comments · Fixed by #54445
Closed
1 task done

nextjs 13.4.13+ broke self-hosted docker setup #54133

domosedov opened this issue Aug 16, 2023 · 60 comments · Fixed by #54445
Labels
bug Issue was opened via the bug report template. locked Output (export/standalone) Related to the the output option in `next.config.js`. please add a complete reproduction Please add a complete reproduction.

Comments

@domosedov
Copy link

domosedov commented Aug 16, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.5.0: Thu Jun  8 22:22:19 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T8103
    Binaries:
      Node: 18.17.1
      npm: 9.6.7
      Yarn: N/A
      pnpm: 8.6.12
    Relevant Packages:
      next: 13.4.16
      eslint-config-next: 13.4.16
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: standalone

Which area(s) of Next.js are affected? (leave empty if unsure)

Standalone mode (output: "standalone")

Link to the code that reproduces this issue or a replay of the bug

https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile

To Reproduce

See description

Describe the Bug

I'm using dockerfile from the examples.
After upgrading to version 13.4.13-13.4.16, my application stopped working properly.
calling the 'router.push' function results in hard navigation

in middleware redirect
before: https://my-site.com/some-path
now: http://172.18.0.6:3000/some-path

Expected Behavior

13.4.12 behavior

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

self-hosted

@domosedov domosedov added the bug Issue was opened via the bug report template. label Aug 16, 2023
@github-actions github-actions bot added the Output (export/standalone) Related to the the output option in `next.config.js`. label Aug 16, 2023
@balazsorban44 balazsorban44 added the please add a complete reproduction Please add a complete reproduction. label Aug 16, 2023
@github-actions
Copy link
Contributor

We cannot recreate the issue with the provided information. Please add a reproduction in order for us to be able to investigate.

Why was this issue marked with the please add a complete reproduction label?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository (template for App Router, template for Pages Router), but you can also use these templates: CodeSandbox: App Router or CodeSandbox: Pages Router.

To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue. Ensure your reproduction does not depend on secrets, 3rd party registries, private dependencies, or any other data that cannot be made public. Avoid a reproduction including a whole monorepo (unless relevant to the issue). The easier it is to reproduce the issue, the quicker we can help.

Please test your reproduction against the latest version of Next.js (next@canary) to make sure your issue has not already been fixed.

If you cannot create a clean reproduction, another way you can help the maintainers' job is to pinpoint the canary version of next that introduced the issue. Check out our releases, and try to find the first canary release that introduced the issue. This will help us narrow down the scope of the issue, and possibly point to the PR/code change that introduced it. You can install a specific version of next by running npm install next@<version>.

I added a link, why was it still marked?

Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.

What happens if I don't provide a sufficient minimal reproduction?

Issues with the please add a complete reproduction label that receives no meaningful activity (e.g. new comments with a reproduction link) are automatically closed and locked after 30 days.

If your issue has not been resolved in that time and it has been closed/locked, please open a new issue with the required reproduction.

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the 👍 reaction on the topmost comment (please do not comment "I have the same issue" without reproduction steps). Then, we can sort issues by votes to prioritize.

I think my reproduction is good enough, why aren't you looking into it quicker?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources

@domosedov

This comment was marked as off-topic.

@MaikuMori
Copy link

Very likely related to: #53367

@mjad218

This comment was marked as spam.

@mjad218

This comment was marked as spam.

@domosedov

This comment was marked as spam.

@domosedov

This comment was marked as spam.

@balazsorban44
Copy link
Member

balazsorban44 commented Aug 17, 2023

Please read #54133 (comment)

"same" and similar comments are unhelpful and we cannot investigate. We need to see your application code to be able to debug further.

At the very least, paste in your Dockerfile

@domosedov
Copy link
Author

Please read #54133 (comment)

"same" and similar comments are unhelpful and we cannot investigate. We need to see your application code to be able to debug further.

At the very least, paste in your Dockerfile

// Dockerfile

FROM node:18-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY .npmrc package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i; \
  else echo "Lockfile not found." && exit 1; \
  fi

FROM node:18-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED 1

RUN npm run build

FROM node:18-alpine AS runner
WORKDIR /app

ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/sentry.properties ./
COPY --from=builder /app/.sentryclirc ./
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000

CMD ["node", "server.js"]

@domosedov
Copy link
Author

Pages Router

next.js 13.4.12
Снимок экрана 2023-08-17 в 12 30 42

next.js 13.4.13-13.4.17
Снимок экрана 2023-08-17 в 12 26 11

I tried setting "HOSTNAME" in "Dockerfile", if I set "localhost", I get a "50*" error, if I set "0.0.0.0", I get hard navigation when using "router.push()"

@mjad218
Copy link

mjad218 commented Aug 17, 2023

Dockerfile

FROM node:alpine AS base
 
FROM base AS builder
RUN apk add --no-cache libc6-compat
RUN apk update

WORKDIR /app
RUN yarn global add turbo
COPY . .
RUN turbo prune --scope=main --docker
 
FROM base AS installer
RUN apk add --no-cache libc6-compat
RUN apk update
WORKDIR /app
 
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/yarn.lock ./yarn.lock
RUN yarn install

COPY --from=builder /app/out/full/ .
RUN yarn turbo run build --filter=main...
 
FROM base AS runner
WORKDIR /app
 
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
 
COPY --from=installer /app/apps/main/next.config.js .
COPY --from=installer /app/apps/main/package.json .
 
COPY --from=installer --chown=nextjs:nodejs /app/apps/main/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/main/.next/static ./apps/main/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/main/public ./apps/main/public
 
CMD node apps/main/server.js


@mjad218
Copy link

mjad218 commented Aug 17, 2023

Logs

DEFAULT 2023-08-17T15:06:09.147493Z - ready started server on 127.0.0.1:8080, url: http://127.0.0.1:8080
DEFAULT 2023-08-17T15:06:10.014579Z - �[36minfo�[39m Loaded env from /app/apps/main/.env.production
DEFAULT 2023-08-17T15:06:10.452856Z - �[36minfo�[39m Loaded env from /app/apps/main/.env.production
DEFAULT 2023-08-17T15:08:41.319421Z - ready started server on 127.0.0.1:8080, url: http://127.0.0.1:8080
DEFAULT 2023-08-17T15:08:42.110014Z - �[36minfo�[39m Loaded env from /app/apps/main/.env.production
DEFAULT 2023-08-17T15:08:42.571185Z - �[36minfo�[39m Loaded env from /app/apps/main/.env.production
ERROR 2023-08-17T15:10:08.382230Z Default STARTUP TCP probe failed 1 time consecutively for container "frontend-main-monafiz-1" on port 8080. The instance was not started.
ERROR 2023-08-17T15:12:39.963337Z Default STARTUP TCP probe failed 1 time consecutively for container "frontend-main-monafiz-1" on port 8080. The instance was not started.
DEFAULT 2023-08-17T15:13:31.197730Z - ready started server on 127.0.0.1:8080, url: http://127.0.0.1:8080
DEFAULT 2023-08-17T15:13:31.923767Z - �[36minfo�[39m Loaded env from /app/apps/main/.env.production
DEFAULT 2023-08-17T15:13:32.367534Z - �[36minfo�[39m Loaded env from /app/apps/main/.env.production
DEFAULT 2023-08-17T15:16:03.022532Z - ready started server on 127.0.0.1:8080, url: http://127.0.0.1:8080
DEFAULT 2023-08-17T15:16:03.814854Z - �[36minfo�[39m Loaded env from /app/apps/main/.env.production
DEFAULT 2023-08-17T15:16:04.264939Z - �[36minfo�[39m Loaded env from /app/apps/main/.env.production
ERROR 2023-08-17T15:17:30.354029Z Default STARTUP TCP probe failed 1 time consecutively for container "frontend-main-monafiz-1" on port 8080. The instance was not started.

@mjad218
Copy link

mjad218 commented Aug 17, 2023

@balazsorban44
Here is a reproduction
https://github.com/mjad218/next-js-cloud-run

just a simple app created with create-next-app

Build logs :

Step #2 - "Deploy": Deployment failed
Step #2 - "Deploy": ERROR: (gcloud.run.services.update) Revision 'next-js-cloud-run-00003-yiy' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.
Step #2 - "Deploy": 
Step #2 - "Deploy": Logs URL: https://console.cloud.google.com/logs/viewer?project=mobtakr-386219&resource=cloud_run_revision/service_name/next-js-cloud-run/revision_name/next-js-cloud-run-00003-yiy&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22next-js-cloud-run%22%0Aresource.labels.revision_name%3D%22next-js-cloud-run-00003-yiy%22 
Step #2 - "Deploy": For more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start
Finished Step #2 - "Deploy"
ERROR
ERROR: build step 2 "gcr.io/google.com/cloudsdktool/cloud-sdk:slim" failed: step exited with non-zero status: 1


@mjad218
Copy link

mjad218 commented Aug 17, 2023

Logs

NOTICE 2023-08-17T16:16:40.481718Z [protoPayload.serviceName: run.googleapis.com] [protoPayload.methodName: google.cloud.run.v1.Services.CreateService] [protoPayload.resourceName: namespaces/mobtakr-386219/services/next-js-cloud-run] [protoPayload.authenticationInfo.principalEmail: [] audit_log, method: "google.cloud.run.v1.Services.CreateService", principal_email: ""
NOTICE 2023-08-17T16:16:43.186355Z [protoPayload.serviceName: run.googleapis.com] [protoPayload.methodName: google.cloud.run.v1.Services.ReplaceService] [protoPayload.resourceName: namespaces/mobtakr-386219/services/next-js-cloud-run] [protoPayload.authenticationInfo.principalEmail:  audit_log, method: "google.cloud.run.v1.Services.ReplaceService", principal_email: ""
DEFAULT 2023-08-17T16:16:48.573365Z 2023/08/17 16:16:48 Hello from Cloud Run! The container started successfully and is listening for HTTP requests on $PORT
INFO 2023-08-17T16:16:48.584791Z Default STARTUP TCP probe succeeded after 1 attempt for container "placeholder-1" on port 8080.
INFO 2023-08-17T16:16:48.705211Z [protoPayload.serviceName: run.googleapis.com] [protoPayload.methodName: v1] [protoPayload.resourceName: namespaces/mobtakr-386219/revisions/next-js-cloud-run-00001-9mg] Ready condition status changed to True for Revision next-js-cloud-run-00001-9mg.
INFO 2023-08-17T16:16:49.088717Z [protoPayload.serviceName: run.googleapis.com] [protoPayload.methodName: v1] [protoPayload.resourceName: namespaces/mobtakr-386219/services/next-js-cloud-run] Ready condition status changed to True for Service next-js-cloud-run.

@garythung
Copy link

garythung commented Aug 17, 2023

I had a similar issue on 13.4.17. It would start the nextjs server on a host that wasn't 127.0.0.1. Specifying HOSTNAME env var in my Dockerfile fixed it for me.

# Adapted from https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile

# Install dependencies only when needed
FROM node:16-alpine AS deps
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json package-lock.json* ./
RUN npm ci

# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# env vars
RUN rm .env*
COPY .env.production ./.env

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED 1

RUN npm run build

# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV HOSTNAME 127.0.0.1
ENV PORT 3000

CMD ["node", "server.js"]

@domosedov
Copy link
Author

I had a similar issue on 13.4.17. It would start the nextjs server on a host that wasn't 127.0.0.1. Specifying HOSTNAME env var in my Dockerfile fixed it for me.

# Adapted from https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile

# Install dependencies only when needed
FROM node:16-alpine AS deps
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json package-lock.json* ./
RUN npm ci

# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# env vars
RUN rm .env*
COPY .env.production ./.env

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED 1

RUN npm run build

# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV HOSTNAME 127.0.0.1
ENV PORT 3000

CMD ["node", "server.js"]

doesn't work for me
i get 502 Bad gateway if i set ENV HOSTNAME 127.0.0.1

@Baukaalm
Copy link
Contributor

Baukaalm commented Aug 18, 2023

I have the same issue. After updating next.js to version 13.4.13, the Docker build stopped working, and the staging environment went down. I changed the HOSTNAME to 0.0.0.0. The build started working again, but dynamic routes with the following syntax stopped functioning: pages/blog/[slug].tsx

My dockerfile

FROM node:18-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Install dependencies based on the preferred package manager (Yarn 2)
COPY package.json yarn.lock* .yarnrc.yml ./
COPY .yarn ./.yarn
RUN yarn install --immutable

# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1


RUN yarn build

# If using npm comment out above and use below instead
# RUN npm run build

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

# ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000

CMD ["node", "server.js"]

Logs
2023-08-18 11 24 25

@Baukaalm
Copy link
Contributor

Could this be related to these changes #53131 ?

@domosedov

This comment was marked as off-topic.

@hungcrush
Copy link

Any urls with [] (%5B, %5D) return a 404 error which includes the generated js files for any dynamic page

@EmilioHerreraSoukup
Copy link

Having the same issues with ../../[slug].tsx route, tried specifying HOSTNAME in docker file but no success yet.

@hungcrush
Copy link

No one care about this :(

@mjad218
Copy link

mjad218 commented Aug 22, 2023

I get your point. You need to stay within a working version and don't update next until you make sure the issues are solved and you test your app locally and maybe have a staging environment.

Yes that's another issue for me Mohamed. Because the version that works for me is 13.4.12 and it's working in a bad way. We get a lot hydration error from Radix libraries saying id mismatch from the server, It does work though but it breaks another thing thats working with "react-use-gesture" library. And the page I am working on is in production yet we continue to wait ofc.

I currently don't have problems with 13.4.12. Actually, you may be able to solve the hydration errors without updating next js.

I managed to solve most of the hydration issues by client rendering the Date components and actually using a useEffect hook to set the date after the initial render.

I don't know the libraries you are using but I assume updating next won't solve the hydration errors as these libraries are the ones that should adopt the breaking changes of next 13.

One way to solve the hydration issues is to dynamic import the components that have these issues and set ssr: false.

you may need to dig deeper and solve the hydration issues

@mjad218
Copy link

mjad218 commented Aug 22, 2023

and some extensions may cause such hydration problems, So you may disable all extensions while testing/developing

@marwinlewis
Copy link

The issue fixed for me after downgrading nextjs to 13.3.4

@Abdelhadi92
Copy link

Abdelhadi92 commented Aug 24, 2023

The issue fixed for me after downgrading nextjs to 13.4.9 and set HOSTNAME in env variables.

The new versions not reading the HOSTNAME variable.

Before downgrading:
Screenshot 2023-08-24 at 1 32 50 PM

After downgrading:
Screenshot 2023-08-24 at 1 32 58 PM

@8lane
Copy link

8lane commented Aug 24, 2023

Fixed my API route redirect for now by building the URL using the request headers (which I think was mentioned in a different thread)

const url = new URL(req.headers.get('origin') || '')
NextResponse.redirect(url, 302)

@dm1nh
Copy link

dm1nh commented Aug 25, 2023

I have to downgrade to next@13.4.12 and set ENV HOSTNAME localhost in Dockerfile to make my app run normally. Just wait for fixing this bug on the next stable release.

karl-run added a commit to navikt/syfosmmanuell that referenced this issue Aug 25, 2023
karl-run added a commit to navikt/syfosmmanuell that referenced this issue Aug 25, 2023
@LukeSchlangen
Copy link
Contributor

LukeSchlangen commented Aug 25, 2023

I had a similar issue and posted this question and answer on Stack Overflow

Due to recent changes, you now need to add this line to your Dockerfile:

ENV HOSTNAME "0.0.0.0"

I don't know for sure, but if I had to guess, I think it's related to the change on this line of the pull request: https://github.com/vercel/next.js/pull/53131/files#diff-3a7de5f9ca3729cdc0a041e9d4c22391754e702ba49ba230daf55730bdc70d99R1975

I noticed that 13.4.13 was working, but 13.4.15 was not. This code was a part of the 13.4.15 release.

@m4salah made a pull request that fixed the issue in the docs for new projects using the with-docker example. Projects already using Docker and NextJS will need to add that line.

The full, working Dockerfile example now includes that as the second-to-last line. This is the current snapshot of that example:

FROM node:18-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
  else echo "Lockfile not found." && exit 1; \
  fi


# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN yarn build

# If using npm comment out above and use below instead
# RUN npm run build

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000
# set hostname to localhost
ENV HOSTNAME "0.0.0.0"

CMD ["node", "server.js"]

Hope this helps others!

@nick-cheatwood7
Copy link

@LukeSchlangen this fixed it for me on 13.4.19. You're a life saver!

@LukeSchlangen
Copy link
Contributor

Thanks! It was a team effort. I don't think I would have figured it out without @m4salah's pull request. I hope this works for most people!

@LukeSchlangen
Copy link
Contributor

LukeSchlangen commented Aug 25, 2023

As a note, downgrading to version 13.4.13 should still work as a quick fix.

If you want to keep your packages up to date, updating the Dockerfile is a solution that should allow you to stay up to date with new releases.

@dm1nh
Copy link

dm1nh commented Aug 25, 2023

I still have the problem with next-auth. It redirects to Docker internal IP address or to HOSTNAME (if specified) when I try to sign in.

@schimi-dev
Copy link

schimi-dev commented Aug 29, 2023

Reproduction attempt

Below, I repost two reproduction scenarios I described in a comment that I made in the context of another issue regarding problems for self-hosted apps introduced by 13.4.13+.

My original comment: #53171 (comment)

Altough that issue was stated to be solved both problems I described are still present in 13.4.19. At the time of writing the original comment I assumed these aspects to be IPv6 related. However, they can also be reproduced when using IPv4. Thus, I slightly changed these scenarios.

Unfortunately, both of the scenarios can only be reproduced when hosting the app on a machine that is not your local computer.

Below is the next.config.js file:

/** @type {import('next').NextConfig} */
const nextConfig = {
    output: 'standalone',
    serverActions: true,
}

module.exports = nextConfig

For reproduction do the following:

  1. Build an app where output: standalone is configured.
  2. For easier reproduction copy the static and public folder as described here: https://nextjs.org/docs/app/api-reference/next-config-js/output#automatically-copying-traced-files
  3. Start the app with node server.js on a server that is not your local machine.

In such an environment, the following 2 scenarios lead to problems:

1. Scenario - Internationalization middleware redirects to localhost

Reproduction

  1. Use the example from the Next.js repository: https://github.com/vercel/next.js/tree/canary/examples/app-dir-i18n-routing
  2. Configure output: standalone.
  3. Increase Next.js version to 13.4.19.
  4. Build the app and host the app on a different server (not your local machine).
  5. Visit the corresponding url in the browser, e.g.: http://my.test.server:3000

Result: The internationalization middleware redirects to: http://localhost:3000/en
Expected: The internationalization middleware should redirect to: http://my.test.server:3000/en

2. Scenario - Redirects in Server Actions force a hard reload of the whole page in the browser when the app is hosted behind a Reverse Proxy

Environment

Assume the app is hosted on an internal server e.g.: http://my.test.server:3000
Publicly the app is accessed via a Reverse Proxy e.g.: https://myappname.com

The requests are forwarded from https://myappname.com/... to http://my.test.server:3000/...
We only have one instance of the Next.js app, so no BUILD_ID mismatch or other problems that can occurr with Load Balancing should interfer.

When directly accessing the hosted app via http://my.test.server:3000 everything works as expected.

However, when accessing the app via https://myappname.com and a Server Action uses redirect("/some-other-path") the redirect is enforced via a hard page reload in the browser. No error message is logged to the server or the browser console.

Reproduction

  1. Use any simple service where Server Actions are enabled.
  2. Call a Server Action via a form's action or inside startTransition that does a redirect.

Result: The new page is visited via a hard reload in the web browser. No error message is logged by Next.js.
Expected: Navigation should not reload the whole page. If a misconfiguration of the Reverse Proxy leads to such a problem, at least an error/warning message should be logged by Next.js.

@dm1nh
Copy link

dm1nh commented Aug 30, 2023

This is my day. I declare ENV HOSTNAME 0.0.0.0 in Dockerfile with nextjs v13.4.19 then my production deploy run perfectly. I don't know what magic happened. You can check my public repo at https://github.com/crate-box/crate.

gustavovalverde added a commit to ogticrd/cuenta-unica-registry that referenced this issue Aug 31, 2023
This is an issue caused on a recent NextJS version upgrade: vercel/next.js#54133
@matmannion
Copy link

We tried ENV HOSTNAME 0.0.0.0 and updating to v13.4.19, which ran fine, but caused a memory leak in production - we've reverted back to v13.4.12 for now.

image (28)

Would be happy to provide more debugging info if someone can point me in the right direction; running node servers is not my forté.

@frasergr
Copy link

frasergr commented Sep 1, 2023

...but caused a memory leak in production

Ensure you're using node >=18.17.1 as there was a memory leak with fetch in lower versions

@matmannion
Copy link

...but caused a memory leak in production

Ensure you're using node >=18.17.1 as there was a memory leak with fetch in lower versions

Ah, thank you; we're basing off node:18-alpine3.16 at the moment which it turns out isn't being updated anymore so was stick on node 18.16.0. Will see if that makes a difference

marluanespiritusanto added a commit to ogticrd/cuenta-unica-registry that referenced this issue Sep 7, 2023
* refactor: nextjs 13 migration

* fix: nextjs13 migrations

* fix(build): use correct reference to variable

* fix(styles): use MUI recommended Nextjs13 approach

As specified here: https://mui.com/material-ui/guides/next-js-app-router/

* fix(linter): add missing definitions from plugins

* fix(ci): allow building with latest node

* fix(linter): use general eslint disable rule

* fix: tests

* imp(build): enable `yarn` cache

* build(docker): use correct `$PORT` and `$HOSTNAME` handling

* fix(deploy): use `0.0.0.0` as hostname

This is an issue caused on a recent NextJS version upgrade: vercel/next.js#54133

* fix(build): use a newer Nextjs version to fix route handlers

* build(deps): update app dependencies

* fix(styles): loader positioning

* fix(ux): use numeric input for cedula field

* feat: edge-csrf middleware applied

* imp(csrf): just use type

* feat(core): cookie middleware implementation

Validate cookie on all api request

* fix(api): getting `validated` query params

Extracting `validated` queryparams in the Nextjs 13 way

---------

Co-authored-by: Jeffrey Mesa <JeffreyArt@hotmail.com>
Co-authored-by: Marluan Espiritusanto <marluanespiritusantoguerrero@gmail.com>
@kodiakhq kodiakhq bot closed this as completed in #54445 Sep 14, 2023
kodiakhq bot pushed a commit that referenced this issue Sep 14, 2023
By default Next.js listens on localhost, platforms like Google Cloud Run and fly.io will fail to deploy apps that listen on `localhost`, Next.js should bind to `0.0.0.0` instead

Related to #54342
Fixes #49777
Fixes #54133
@GrantASL19
Copy link

Any urls with [] (%5B, %5D) return a 404 error which includes the generated js files for any dynamic page

In case anyone looking at this thread is suffering from this, removing a trailing slash from our Nginx proxy_pass config fixed the issue for us (thanks @beverloo!): #54008 (comment).

@github-actions
Copy link
Contributor

github-actions bot commented Oct 4, 2023

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot added the locked label Oct 4, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Output (export/standalone) Related to the the output option in `next.config.js`. please add a complete reproduction Please add a complete reproduction.
Projects
None yet