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

M1 Mac下arm64v8 Docker镜像执行npm install架构兼容报错排查

问题描述

我使用M1 Mac,基于arm64v8/php:8.1-apache镜像构建Docker容器,构建过程无任何问题,但尝试执行npm install安装依赖时,出现如下报错:

root@456d8199a91d:/var/www/html# npm install  
npm ERR! code 1
npm ERR! path /var/www/html/node_modules/optipng-bin
npm ERR! command failed
npm ERR! command sh -c node lib/install.js
npm ERR! compiling from source
npm ERR! Command failed: /var/www/html/node_modules/optipng-bin/vendor/optipng --version
npm ERR! qemu-x86_64: Could not open '/lib64/ld-linux-x86-64.so.2': No such file or directory
npm ERR! 
npm ERR! 
npm ERR! optipng pre-build test failed
npm ERR! Error: Command failed: /bin/sh -c make install
npm ERR! pngrtran.c:99:1: warning: 'png_rtran_ok' defined but not used [-Wunused-function]
npm ERR!    99 | png_rtran_ok(png_structrp png_ptr, int need_IHDR)
npm ERR!       | ^~~~~~~~~~~~
npm ERR! ar: `u' modifier ignored since `D' is the default (see `U')
npm ERR! ar: `u' modifier ignored since `D' is the default (see `U')
npm ERR! ar: `u' modifier ignored since `D' is the default (see `U')
npm ERR! ar: `u' modifier ignored since `D' is the default (see `U')
npm ERR! pngxmem.c: In function 'pngx_malloc_rows_extended':
npm ERR! pngxmem.c:38:34: warning: comparison is always false due to limited range of data type [-Wtype-limits]
npm ERR!    38 |        (pngx_alloc_size_t)height > (pngx_alloc_size_t)(-1) / sizeof(png_bytep))
npm ERR!       |                                  ^
npm ERR! ar: `u' modifier ignored since `D' is the default (see `U')
npm ERR! /usr/bin/ld: ../libpng/libpng.a(pngrutil.o): in function `png_read_filter_row':
npm ERR! pngrutil.c:(.text+0x1f64): undefined reference to `png_init_filter_functions_neon'
npm ERR! collect2: error: ld returned 1 exit status
npm ERR! make[1]: *** [Makefile:100: optipng] Error 1
npm ERR! make: *** [Makefile:14: install] Error 2

我注意到报错中提到了/lib64/ld-linux-x86-64.so.2,此前仅在使用amd64架构镜像时遇到过该问题。我的Dockerfile配置如下:

FROM arm64v8/php:8.1-apache

RUN apt-get update

EXPOSE 80

RUN apt-get install -y \
    git \
    zip \
    curl \
    sudo \
    unzip \
    libicu-dev \
    libbz2-dev \
    libpng-dev \
    libjpeg-dev \
    libmcrypt-dev \
    libreadline-dev \
    libfreetype6-dev \
    libonig-dev \
    libzip-dev \
    nodejs \
    npm \
    g++

ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

RUN a2enmod rewrite headers

RUN docker-php-ext-install \
    bz2 \
    intl \
    iconv \
    bcmath \
    opcache \
    calendar \
    mbstring \
    pdo_mysql \
    zip

ADD https://raw.githubusercontent.com/mlocati/docker-php-extension-installer/master/install-php-extensions /usr/local/bin/
RUN chmod uga+x /usr/local/bin/install-php-extensions && sync && \
    install-php-extensions pdo_pgsql

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# This needs to be set in .env as 'UID', must match the host id, attained using 'id -u'
# prevents root ownership on apache + other saved files
ARG uid
RUN useradd -G www-data,root -u $uid -d /home/devuser devuser
RUN mkdir -p /home/devuser/.composer && \
    chown -R devuser:devuser /home/devuser

# Necessary drivers for redis adapter
RUN pecl channel-update pecl.php.net
RUN pecl install -f redis

RUN sudo apt update
RUN yes | apt install vim
 

我已明确指定使用arm64v8架构镜像,却出现了类似amd64架构的报错,且该配置已正常使用数月,请问我忽略了什么明显问题?

解决方案

核心问题分析

optipng-bin默认拉取的预编译二进制文件是x86_64架构的,在arm64容器里执行时会触发qemu-x86_64转译,但容器内缺少x86架构的核心依赖库(即报错中的/lib64/ld-linux-x86-64.so.2)。后续源码编译失败,则是因为编译时找不到arm64特有的Neon优化函数png_init_filter_functions_neon。

具体修复步骤

  • 强制npm拉取arm64预编译包
    在执行npm install前,设置环境变量明确指定架构,让optipng-bin获取对应arm64的预编译文件,跳过源码编译:

    npm_config_arch=arm64 npm_config_platform=linux npm install
    
  • 清理缓存避免残留问题
    之前安装失败可能留下x86架构的缓存文件,先删除再重新安装:

    rm -rf node_modules package-lock.json
    
  • 修复源码编译依赖(可选)
    如果预编译包不可用,需在Dockerfile中添加arm64编译optipng的必要依赖:

    RUN apt-get install -y libpng-dev gcc make
    

    官方源的libpng-dev默认支持arm64 Neon,添加后可解决编译时的函数引用错误。

为什么之前正常现在出问题?

大概率是optipng-bin版本更新或npm架构检测逻辑变动,导致原本的自动适配失效;也可能是Docker缓存残留了x86依赖文件。可以清理Docker镜像缓存后重新构建:

docker builder prune -a

内容的提问来源于stack exchange,提问作者Lo-urc

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 10:51:12