oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.14k stars 2.76k forks source link

Bun + Nextjs + Docker: Incomplete .next build output in docker, but works fine locally #4795

Open pdr-999 opened 1 year ago

pdr-999 commented 1 year ago

What version of Bun is running?

1.0.0+822a00c4d508b54f650933a73ca5f4a3af9a7983

What platform is your computer?

Linux 6.2.0-32-generic x86_64 x86_64

What steps can reproduce the bug?

git clone git@github.com:pdr-999/bun-nextjs.git
docker compose up -d
docker compose logs app-bun

What is the expected behavior?

bun-nextjs | - ready started server on [::]:3000, url: http://localhost:3000

What do you see instead?

bun-nextjs | - warn You are using a non-standard "NODE_ENV" value in your environment. This creates inconsistencies in the project and is strongly advised against. Read more: https://nextjs.org/docs/messages/non-standard-node-env bun-nextjs | - ready started server on localhost:3000, url: http://localhost:3000 bun-nextjs | ENOENT: No such file or directory bun-nextjs | path: "/app/.next/BUILD_ID" bun-nextjs | syscall: "open" bun-nextjs | errno: -2

Additional information

Docker version: Docker version 23.0.3, build 3e7cbfd

When running

docker cp node-nextjs:/app ./app-bun
docker cp bun-nextjs:/app ./app-bun

.next contents of docker + nextjs + node image

.next contents of docker + nextjs + bun image

tldr: Running

bun install && bun run build && bun run start
osflash commented 1 year ago

the command bun run build was not executed.

marcfrankel commented 1 year ago

I believe I am having a similar issue

nacimgoura commented 1 year ago

Same problem, bun run build doesn't work correctly inside docker

doguyilmaz commented 1 year ago

Same issue, nextjs + docker build fails. Probably memory issue but could not find any equivalent for --max-old-space-size in Node.js yet. (While running locally, there is no issue with build. v1.0.0)

image

michaelangeloio commented 1 year ago

Why create the build in docker? Why not build it outside of docker, copy it, and then just bun run in docker?

marcfrankel commented 1 year ago

In my specific instance, I am trying to get Next.js 13 to work with Bun on Railway. The build seems to complete successfully with no errors (although the next js file structure output is strangely missing from the console. Could be a red herring tho). However, the problem arises when the command bun run start is executed as I get the following issue:

$ next start

- ready started server on localhost:7988, url: http://localhost:7988/

ENOENT: No such file or directory

path: "/app/.next/BUILD_ID"

syscall: "open"

errno: -2
pdr-999 commented 1 year ago

the command bun run build was not executed.

In my case it did execute successfully but the output was incomplete, from this snippet of docker compose build app-bun --progress=plain --no-cache logs, it finishes before generating any pages. The output is usually Generating static pages (4/4), Finalizing page optimization..... after Creating an optimized production build, etc, instead it finishes.

#9 [5/5] RUN bun run build
#9 0.561 $ next build
#9 0.590 - warn You are using a non-standard "NODE_ENV" value in your environment. This creates inconsistencies in the project and is strongly advised against. Read more: https://nextjs.org/docs/messages/non-standard-node-env
#9 1.229 Attention: Next.js now collects completely anonymous telemetry regarding usage.
#9 1.229 This information is used to shape Next.js' roadmap and prioritize features.
#9 1.229 You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
#9 1.229 https://nextjs.org/telemetry
#9 1.229 
#9 1.378 - info Creating an optimized production build...
#9 DONE 8.3s

Why create the build in docker? Why not build it outside of docker, copy it, and then just bun run in docker?

Most Dockerfile examples with node builds in docker, https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile so I tried replacing node with bun.

Installing outside of docker defeats the purpose of using docker in a server no?

But shouldn't it have the same output either way?

yicru commented 1 year ago

Facing the same issue here. Managed to bypass it by using a Node image in the build stage.

# syntax=docker/dockerfile:1

ARG BUN_VERSION=1.0.0

FROM oven/bun:${BUN_VERSION} as base

WORKDIR /app

ENV NODE_ENV=production

FROM base as deps

COPY package.json bun.lockb ./
RUN bun install

FROM node:18 as build

WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build -- --no-lint

FROM base as final

USER bun

COPY --from=build /app/package.json ./package.json
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/.next ./.next
COPY --from=build /app/public ./public

EXPOSE 3000

CMD ["bun", "run", "start"]
AnatoliyKhaulin commented 1 year ago

Facing the same issue here. Managed to bypass it by using a Node image in the build stage.

# syntax=docker/dockerfile:1

ARG BUN_VERSION=1.0.0

FROM oven/bun:${BUN_VERSION} as base

WORKDIR /app

ENV NODE_ENV=production

FROM base as deps

COPY package.json bun.lockb ./
RUN bun install

FROM node:18 as build

WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build -- --no-lint

FROM base as final

USER bun

COPY --from=build /app/package.json ./package.json
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/.next ./.next
COPY --from=build /app/public ./public

EXPOSE 3000

CMD ["bun", "run", "start"]
Screenshot 2023-09-15 at 10 35 42
aak-lear commented 1 year ago

Same issue

next.js build with bun in Docker fails with following error: script "build" exited with code 9 (SIGKILL)

ericklarsen commented 1 year ago

Same issue here

nuntjw commented 1 year ago

same issue as @aak-lear. any update?

kaumac commented 7 months ago

Also having this issue running bun --bun run next build

akarabach commented 6 months ago

the same issue for me