oven-sh / bun

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

`bun install --frozen-lockfile` is exceptionally slow in docker [bun 1.1.4] #10371

Open 0tii opened 6 months ago

0tii commented 6 months ago

What version of Bun is running?

1.1.4+fbe2fe0c3

What platform is your computer?

Linux 5.15.146.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

Use this minimal Dockerfile with a nestjs project

FROM oven/bun:1.0.14 as base
WORKDIR /usr/src/app
COPY package.json bun.lockb ./

FROM base as dev
RUN bun i --frozen-lockfile
COPY . .
CMD ["bun", "start"]

FROM base as prod
RUN bun i --production --frozen-lockfile
RUN bun i -D @nestjs/cli
COPY . .
RUN bun run build

EXPOSE 3000
CMD ["bun", "start:prod"]

And see a fresh build take 5 minutes for a bun ci

=> [app internal] load build definition from Dockerfile                                                                                                                                                                0.0s
 => => transferring dockerfile: 359B                                                                                                                                                                                    0.0s
 => [app internal] load metadata for docker.io/oven/bun:1.0.14                                                                                                                                                          4.2s
 => [app internal] load .dockerignore                                                                                                                                                                                   0.0s
 => => transferring context: 74B                                                                                                                                                                                        0.0s
 => [app base 1/3] FROM docker.io/oven/bun:1.0.14@sha256:1e7e5dd2a5403b91714d0fc1b229da085f460a632f0c79037cbb0b60238c6434                                                                                               0.0s
 => [app internal] load build context                                                                                                                                                                                   0.0s
 => => transferring context: 41.50kB                                                                                                                                                                                    0.0s
 => CACHED [app base 2/3] WORKDIR /usr/src/app                                                                                                                                                                          0.0s
 => CACHED [app base 3/3] COPY package.json bun.lockb ./                                                                                                                                                                0.0s
 => [app dev 1/2] RUN bun i --frozen-lockfile                                                                                                                                                                         300.8s
 => [app dev 2/2] COPY . .                                                                                                                                                                                              0.2s
 => [app] exporting to image                                                                                                                                                                                            2.3s 
 => => exporting layers                                                                                                                                                                                                 2.3s 
 => => writing image sha256:2424dcd80587dcf05e0fccbad2eb51877edf6bbbe977a0e093a8730620178e3e                                                                                                                            0.0s 
 => => naming to docker.io/library/cannahub-backend-app         

What is the expected behavior?

Fast install, doing the same on my host machine takes a few seconds max even without cache

What do you see instead?

300+ second install

Additional information

No response

Jarred-Sumner commented 6 months ago

FROM oven/bun:1.0.14 as base

This is using Bun v1.0.14

Please try with Bun v1.1.4

0tii commented 6 months ago

Please try with Bun v1.1.4

[+] Building 256.1s (7/9)                                                                                                                                                                                     docker:default
 => [app internal] load build definition from Dockerfile                                                                                                                                                                0.1s
 => => transferring dockerfile: 364B                                                                                                                                                                                    0.0s
 => [app internal] load metadata for docker.io/oven/bun:1.1.4                                                                                                                                                           2.1s
 => [app internal] load .dockerignore                                                                                                                                                                                   0.1s
 => => transferring context: 74B                                                                                                                                                                                        0.0s
 => [app base 1/3] FROM docker.io/oven/bun:1.1.4@sha256:ea572eace71acadb17ea5c408550eafd5ab82f2f6f48c04b906a3091e017cf35                                                                                                0.0s
 => [app internal] load build context                                                                                                                                                                                   0.3s
 => => transferring context: 50.98kB                                                                                                                                                                                    0.2s
 => CACHED [app base 2/3] WORKDIR /usr/src/app                                                                                                                                                                          0.0s
 => [app base 3/3] COPY package.json bun.lockb ./                                                                                                                                                                       0.2s
 => [app dev 1/2] RUN bun i --frozen-lockfile                                                                                                                                                                         253.3s
 => => # bun install v1.1.4 (fbe2fe0c)     
AmrSaber commented 6 months ago

I am having the same issue in my docker image, I tried building from base image oven/bun:1-alpine and it takes forever to install my svelte app dependencies.

If anyone is stuck at this, as a work-around for this, I install the dependencies using yarn, then build and run using bun.

I added

[install.lockfile]
print = "yarn"

to bunfig.toml file in project's root, this makes bun create yarn.lock file as well as bun.lockb file.

Then I update my docker file to look like this:

# Install using yarn, as bun install has problems with docker
FROM node:lts-alpine AS install

WORKDIR /app

COPY package.json .
COPY yarn.lock .

RUN --mount=type=cache,target=/usr/local/share/.cache yarn install --freeze-lockfile --verbose

# Build and run using bun
FROM oven/bun:1-alpine

WORKDIR /app

COPY --from=install /app /app
COPY . .

RUN bun run build

CMD bun ./build/index.js

The --mount part in the install command is used to mount yarn cache as a cache-mount, this greatly reduces resulting image size (effectively removes cache from the image), and makes subsequent builds a lot faster.

The end result is that docker build now finishes in a couple of minutes (from a clean state without any cached images).

yyuemii commented 5 months ago

Having this too on Bun 1.1.12 (43f0913c). It's inconsistent as sometimes a simple restart of the build will resolve the issue (only taking about 5s instead of 270s).

=> [sync build 4/5] RUN bun install --frozen-lockfile --production               210.5s
=> => # [0.02ms] ".env"
=> => # bun install v1.1.12 (43f0913c)
0tii commented 5 months ago

Very odd, given since 1.1.10 (have not dared upgrading further) the issue is gone for me and I was already about to praise the update to have fixed it for good

ventsislav-georgiev commented 2 months ago

Having the same issue, still happening on 1.1.27. Easily reproducible when starting on clean Docker cache.

Starting a VM and running build will constantly freeze for more than 8 min.

image image Screenshot 2024-09-08 at 14 45 54

Hitting Ctrl+C and reruning the build command is forcing it to complete faster, but again gets stuck and doing that a couple of times (Ctrl+C, build) bun install completes in about < 40s (Including the restart time of Docker checking the previous layers).

Any new installs after that, when a dependency is changed are fast. Then recreating the VM (colima delete/colima start) hits the same issue. This is on an M1 mac.

NPM completes the same operation (full install without cache) in about 20s.

As seen in the screenshots, verbose logs don't help much as it just prints waiting for N tasks. @Jarred-Sumner are there any other logs I can enable here to get more insights on what goes wrong the first time?