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

Docker镜像构建失败:sharp@0.30.7安装报错求助

Docker构建失败:sharp@0.30.7安装脚本执行错误(ELIFECYCLE、errno 1)

之前可正常构建的Docker镜像目前无法成功构建,执行docker build -t passio:dev . --no-cache时,出现sharp@0.30.7安装脚本执行失败的错误(错误码ELIFECYCLE、errno 1)。已尝试将Node.js版本从14.17.5升级至16,以及在DockerFile中添加RUN npm install sharp命令单独安装该包,但均未解决问题。

当前Dockerfile内容

# syntax=docker/dockerfile:1

FROM node:14.17.5
ENV NODE_ENV=development

#ENV NPM_CONFIG_PREFIX=/home/node/passio.npm-global
#ENV PATH=$PATH:/home/node/passio.npm-global/bin

RUN apt-get update \
  && apt-get install -y sudo \
  && apt-get install -y systemd \
  && apt-get install -y dbus-user-session

# RUN adduser --disabled-password --gecos '' docker
# RUN adduser docker sudo
# RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# USER docker

# Create app directory
RUN mkdir -p /home/docker/passio
# RUN chown docker /home/docker/passio
WORKDIR /home/docker/passio

RUN npm config rm proxy
RUN npm config rm https-proxy --tried removing npm proxy

COPY package.json .
# COPY package-lock.json .
RUN npm install

COPY . ./

# RUN sudo /etc/init.d/dbus start

RUN node systemd install
RUN node systemd enable

# RUN node db/sync
# RUN node browserify --reset

EXPOSE 80
CMD ["npm", "run", "restart" ]

具体报错信息

#16 123.6 npm ERR! code ELIFECYCLE
#16 123.6 npm ERR! errno 1
#16 123.7 npm ERR! sharp@0.30.7 install: `(node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy)`
#16 123.7 npm ERR! Exit status 1
#16 123.7 npm ERR! 
#16 123.7 npm ERR! Failed at the sharp@0.30.7 install script.
#16 123.7 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
#16 123.7 
#16 123.7 npm ERR! A complete log of this run can be found in:
#16 123.7 npm ERR!     /root/.npm/_logs/2022-11-25T15_08_51_150Z-debug.log

排查与修复方案

  • 安装sharp编译依赖:sharp在编译时需要libvips及node-gyp编译工具,修改apt-get安装步骤,补充必要依赖:

    RUN apt-get update \
      && apt-get install -y sudo systemd dbus-user-session \
      && apt-get install -y build-essential libvips-dev python3 \
      && apt-get clean && rm -rf /var/lib/apt/lists/*
    
  • 优化网络下载配置:如果是预构建包下载失败导致的编译问题,可添加环境变量指定国内镜像源加速:

    ENV SHARP_DIST_BASE_URL=https://npmmirror.com/mirrors/sharp
    RUN npm config set registry https://registry.npmmirror.com
    
  • 恢复非root用户构建:sharp在root权限下构建可能存在权限异常,取消原Dockerfile中用户配置的注释,并修正目录权限:

    RUN adduser --disabled-password --gecos '' docker
    RUN adduser docker sudo
    RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
    
    USER docker
    
    RUN mkdir -p /home/docker/passio
    RUN chown docker:docker /home/docker/passio
    WORKDIR /home/docker/passio
    
  • 清理npm缓存:在npm install前添加缓存清理命令,避免缓存损坏导致的安装失败:

    RUN npm cache clean --force
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 21:01:22