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

Could not load the "sharp" module using the linuxmusl-x64 runtime using bun & alpine #4317

Open
marcoheinrich-sii opened this issue Jan 22, 2025 · 4 comments
Labels

Comments

@marcoheinrich-sii
Copy link

marcoheinrich-sii commented Jan 22, 2025

UPDATE

Repo to reproduce: https://github.com/marcoheinrich-sii/sharp-bun-error-demo

I am trying to integrate sharp with bun and Alpine linux, but I am constantly running into runtime errors, when trying to actually run the container. My first goal is to get it running at all. The endgame should be to get it building for x64 as well as arm

The development environment is Ubuntu 22.04 running inside WSL2. Here's the redacted dockerfile, without the authentication biuts and bobs on our private registry:

FROM oven/bun:alpine AS build-stage

WORKDIR /app

COPY package.json ./
COPY bun.lockb ./

RUN bun install
# manually installing this makes no difference
# RUN bun install @img/sharp-linuxmusl-x64 @img/sharp-libvips-linuxmusl-x64
COPY . .

RUN bun build.ts

FROM oven/bun:alpine AS runtime-stage

RUN apk update && apk upgrade

RUN mkdir -p /usr/src/app

# Create a group named appusers and add a user called app to it. 
RUN addgroup -S appusers && adduser -S app -G appusers

RUN chown -R app:appusers /usr/src/app

RUN mkdir -p /home/app
RUN chown -R app:appusers /home/app


COPY --from=build-stage /app/dist /usr/src/app
# not copying this yields in 
# ERR_DLOPEN_FAILED: Error loading shared library libvips-cpp.so.42: No such file or directory (needed by /home/app/.bun/install/cache/@img/[email protected]@@@1/lib/sharp-linuxmusl-x64.node)
COPY --from=build-stage /app/node_modules/sharp /usr/src/app/node_modules/sharp
COPY --from=build-stage /app/node_modules/semver /usr/src/app/node_modules/semver

USER app

WORKDIR /usr/src/app
COPY --chown=app:appusers package.json ./

EXPOSE 4000
CMD [ "bun" ,"run", "main.js" ]

build.ts

await Bun.build({
	entrypoints: ["src/server/main.tsx"],
	outdir: "./dist",
	target: "bun",
	external: ["sharp"],
});

export {};

Any combination I try, it always resolves in an error not being able to find libvips-cpp.so.42 or not being able to load sharp using teh linuxmusl-x64 runtime.

What am I doing wrong here?

@lovell
Copy link
Owner

lovell commented Jan 22, 2025

It looks like you're selectively copying only a couple of dependencies from the node_modules created during build-stage to the runtime-stage. You'll probably want to copy all the dependencies, something like:

COPY --from=build-stage /app/node_modules /usr/src/app/node_modules

@marcoheinrich-sii
Copy link
Author

Copying the whole node_modules folder is a bit of overkill, isn't ? The whole application is 116 MB (inlcuding sharp + deps) in size, while the node_modules folder sits at 294 MB. This is all the copying that's required to make it work:

COPY --from=build-stage /app/node_modules/sharp /usr/src/app/node_modules/sharp
COPY --from=build-stage /app/node_modules/semver /usr/src/app/node_modules/semver
COPY --from=build-stage /app/node_modules/color /usr/src/app/node_modules/color
COPY --from=build-stage /app/node_modules/detect-libc /usr/src/app/node_modules/detect-libc
COPY --from=build-stage /app/node_modules/@img /usr/src/app/node_modules/@img

@lovell
Copy link
Owner

lovell commented Jan 22, 2025

The following will remove devDependencies and peerDependencies that are presumably not required at runtime. This should be lower maintenance than selectively copying dependencies.

RUN bun install --omit dev --omit peer

@marcoheinrich-sii
Copy link
Author

The isse is not installing unwanted sharp dependencies. I was expecting not having to copy anything over after actually building the application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants