基于node:20-alpine镜像安装aws-lambda-ric失败求助
在Node.js 20 Alpine镜像中安装aws-lambda-ric失败的解决办法
问题场景
为Node.js代码编写Dockerfile部署至AWS Lambda时,使用node:20-alpine作为基础镜像无法安装aws-lambda-ric,切换到node:16-alpine可正常完成安装。
我的Dockerfile
FROM node:20-alpine RUN apk add --no-cache --update --repository=https://dl-cdn.alpinelinux.org/alpine/v3.16/main/ libexecinfo-dev RUN apk add --no-cache \ bash \ git \ build-base \ cmake \ make\ autoconf\ libtool \ libexecinfo-dev \ automake\ python3 WORKDIR /app COPY package.json ./ RUN npm install RUN npm install aws-lambda-ric COPY ./src/* . CMD ["index.handler"]
安装报错信息
[7/8] RUN npm install aws-lambda-ric: 39.09 npm error code 2 39.09 npm error path /app/node_modules/aws-lambda-ric 39.09 npm error command failed 39.09 npm error command sh -c ./scripts/preinstall.sh 39.09 npm error libtoolize: putting auxiliary files in '.'. 39.09 npm error libtoolize: copying file './ltmain.sh' 39.09 npm error libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'm4'. 39.09 npm error libtoolize: copying file 'm4/libtool.m4' 39.09 npm error libtoolize: copying file 'm4/ltoptions.m4' 39.09 npm error libtoolize: copying file 'm4/ltsugar.m4' 39.09 npm error libtoolize: copying file 'm4/ltversion.m4' 39.09 npm error libtoolize: copying file 'm4/lt~obsolete.m4' 39.09 npm error libtoolize: Remember to add 'LT_INIT' to configure.ac. 39.10 npm error *** Do not use buildconf. Instead, just use: autoreconf -fi 39.10 npm error configure.ac:120: installing './compile' 39.10 npm error configure.ac:317: installing './config.guess' 39.10 npm error configure.ac:317: installing './config.sub' 39.10 npm error configure.ac:120: installing './install-sh' 39.10 npm error configure.ac:125: installing './missing' 39.10 npm error docs/examples/Makefile.am: installing './depcomp' 39.10 npm error parallel-tests: installing './test-driver' 39.10 npm error ./configure: line 6673: syntax error: unexpected ";;" (expecting "fi") 39.10 npm error A complete log of this run can be found in: /root/.npm/_logs/2024-08-05T10_37_40_947Z-debug-0.log
问题原因
node:20-alpine基于Alpine 3.18,而aws-lambda-ric的预安装脚本依赖的旧版autotools组件(autoconf、automake等)与新版Alpine环境存在语法兼容性问题,具体表现为configure脚本中的;;语法在新版shell环境中解析失败。
解决办法
方案1:使用AWS官方Lambda Node.js镜像(推荐)
AWS提供了预配置好的Node.js Lambda镜像,已内置aws-lambda-ric及所有必要依赖,无需手动安装,直接使用即可避免兼容性问题:
FROM public.ecr.aws/lambda/nodejs:20 WORKDIR ${LAMBDA_TASK_ROOT} COPY package.json package-lock.json ./ RUN npm install --production COPY src/* . CMD ["index.handler"]
方案2:锁定Alpine依赖版本
若必须使用node:20-alpine,可通过添加Alpine 3.16仓库源,安装与aws-lambda-ric兼容的旧版autotools组件:
FROM node:20-alpine # 添加Alpine 3.16仓库源 RUN echo "http://dl-cdn.alpinelinux.org/alpine/v3.16/main" >> /etc/apk/repositories RUN echo "http://dl-cdn.alpinelinux.org/alpine/v3.16/community" >> /etc/apk/repositories # 安装指定版本的兼容依赖 RUN apk add --no-cache --update \ bash \ git \ build-base=0.5-r3 \ cmake \ make \ autoconf=2.69-r2 \ libtool=2.4.6-r7 \ libexecinfo-dev=1.1-r1 \ automake=1.16.5-r0 \ python3 WORKDIR /app COPY package.json ./ RUN npm install RUN npm install aws-lambda-ric COPY ./src/* . CMD ["index.handler"]
方案3:无需安装aws-lambda-ric的场景
若你的Lambda函数无需自定义Runtime,仅需使用Lambda原生Node.js Runtime,可直接跳过aws-lambda-ric的安装,但容器部署场景仍推荐使用官方Lambda镜像。
内容的提问来源于stack exchange,提问作者Raj Maurya
相关产品推荐
相关产品推荐

