You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建Bun项目Docker镜像时遇Couchbase原生构建缺失问题求助

Bun项目Docker构建Couchbase原生依赖缺失问题解决

问题详情

在构建Bun项目的Docker镜像时,触发以下错误:

2025-05-07 15:22:07 error: Could not find native build for platform=linux, arch=arm64, runtime=napi, nodeVersion=22.6.0, sslType=openssl3, libc=linux loaded from /home/bun/app/node_modules/couchbase.
2025-05-07 15:22:07       at resolvePrebuild (/$bunfs/root/server:2:15727)
2025-05-07 15:22:07       at loadPrebuild (/$bunfs/root/server:2:12998)
2025-05-07 15:22:07       at <anonymous> (/$bunfs/root/server:2:26050)
2025-05-07 15:22:07       at <anonymous> (/$bunfs/root/server:2:535)
2025-05-07 15:22:07       at <anonymous> (/$bunfs/root/server:2:313722)
2025-05-07 15:22:07       at <anonymous> (/$bunfs/root/server:2:535)
2025-05-07 15:22:07       at /$bunfs/root/server:531:24107
2025-05-07 15:22:07 
2025-05-07 15:22:07 Bun v1.2.12 (Linux arm64)

使用的Dockerfile内容如下:

FROM oven/bun AS builder
ADD package.json bun.lockb ./
RUN bun install
ADD tsconfig.json ./
ADD src src
# patch on couchbase for bun
RUN sed -i -E 's/^([A-Z_]+) =/const \1 =/' node_modules/couchbase/scripts/prebuilds.js

RUN bun build \
    --compile \
    --minify-whitespace \
    --minify-syntax \
    --target bun \
    --outfile server \
    ./src/index.ts

FROM debian:bullseye
ENTRYPOINT ["tini", "--"]
RUN groupadd -g 1001 appuser && useradd -m -u 1001 -g appuser -s /bin/bash appuser
RUN  apt update && apt install tini && apt clean
USER appuser
COPY --from=builder /home/bun/app/server server
ENV API_URL=http://api/checkid/v1
ENV SMS_SERVICE_URL=http://cloud/sms
CMD ["./server"]

本地环境运行正常,仅Docker构建后出现该问题。

核心原因

  • 构建阶段(oven/bun镜像)下载的Couchbase预编译原生文件,与运行阶段(debian:bullseye镜像)的系统库、环境不匹配
  • bun build --compile仅打包业务代码,未包含Couchbase的原生依赖文件

解决方案

方案1:在构建阶段重新编译Couchbase原生模块

修改Dockerfile的builder阶段,添加编译依赖后重新构建Couchbase,确保生成适配运行环境的原生文件:

FROM oven/bun AS builder
# 安装Couchbase原生模块编译所需依赖
RUN apt update && apt install -y build-essential cmake libssl-dev
ADD package.json bun.lockb ./
RUN bun install
# 重新构建Couchbase原生模块
RUN bun run node_modules/couchbase/scripts/build.js
ADD tsconfig.json ./
ADD src src
# 保留原有的Couchbase patch
RUN sed -i -E 's/^([A-Z_]+) =/const \1 =/' node_modules/couchbase/scripts/prebuilds.js

RUN bun build \
    --compile \
    --minify-whitespace \
    --minify-syntax \
    --target bun \
    --outfile server \
    ./src/index.ts

FROM debian:bullseye
ENTRYPOINT ["tini", "--"]
RUN groupadd -g 1001 appuser && useradd -m -u 1001 -g appuser -s /bin/bash appuser
# 运行阶段需要安装libssl依赖
RUN  apt update && apt install tini libssl-dev && apt clean
USER appuser
COPY --from=builder /home/bun/app/server server
ENV API_URL=http://api/checkid/v1
ENV SMS_SERVICE_URL=http://cloud/sms
CMD ["./server"]

方案2:统一构建与运行的基础镜像

将运行阶段的镜像改为oven/bun,保证环境与构建阶段完全一致,避免依赖不兼容:

FROM oven/bun AS builder
ADD package.json bun.lockb ./
RUN bun install
ADD tsconfig.json ./
ADD src src
# 保留原有的Couchbase patch
RUN sed -i -E 's/^([A-Z_]+) =/const \1 =/' node_modules/couchbase/scripts/prebuilds.js

RUN bun build \
    --compile \
    --minify-whitespace \
    --minify-syntax \
    --target bun \
    --outfile server \
    ./src/index.ts

# 直接使用oven/bun作为运行镜像
FROM oven/bun
ENTRYPOINT ["tini", "--"]
RUN groupadd -g 1001 appuser && useradd -m -u 1001 -g appuser -s /bin/bash appuser
RUN apt update && apt install tini && apt clean
USER appuser
COPY --from=builder /home/bun/app/server server
ENV API_URL=http://api/checkid/v1
ENV SMS_SERVICE_URL=http://cloud/sms
CMD ["./server"]

方案3:复制Couchbase预编译文件到运行镜像

在构建阶段完成后,将Couchbase的原生预编译文件复制到运行镜像的对应路径,并设置环境变量让Bun能找到:

FROM oven/bun AS builder
ADD package.json bun.lockb ./
RUN bun install
ADD tsconfig.json ./
ADD src src
# 保留原有的Couchbase patch
RUN sed -i -E 's/^([A-Z_]+) =/const \1 =/' node_modules/couchbase/scripts/prebuilds.js

RUN bun build \
    --compile \
    --minify-whitespace \
    --minify-syntax \
    --target bun \
    --outfile server \
    ./src/index.ts

FROM debian:bullseye
ENTRYPOINT ["tini", "--"]
RUN groupadd -g 1001 appuser && useradd -m -u 1001 -g appuser -s /bin/bash appuser
RUN apt update && apt install tini libssl-dev && apt clean
USER appuser
# 复制Couchbase的原生预编译文件到运行镜像的node_modules路径
COPY --from=builder /home/bun/app/node_modules/couchbase/prebuilds/linux-arm64-napi-v8-openssl3 /home/appuser/node_modules/couchbase/prebuilds/linux-arm64-napi-v8-openssl3
COPY --from=builder /home/bun/app/server server
# 设置NODE_PATH让Bun定位到依赖
ENV NODE_PATH=/home/appuser/node_modules
ENV API_URL=http://api/checkid/v1
ENV SMS_SERVICE_URL=http://cloud/sms
CMD ["./server"]

内容的提问来源于stack exchange,提问作者user1445685

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 05:19:57