Docker构建Jenkins镜像时npm安装失败求助
问题描述
我尝试编写Dockerfile,在容器中安装Jenkins、Chrome、Node.js和npm,基于Jenkins官方基础镜像构建,添加了Chrome和Node.js的安装步骤,Dockerfile内容如下:
FROM jenkins/jenkins:2.440.1-jdk17 USER root # Update and install necessary packages RUN apt-get update && \ apt-get install -y lsb-release wget gnupg2 apt-transport-https curl # Add Docker official GPG key RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \ https://download.docker.com/linux/debian/gpg # Set up Docker repository RUN echo "deb [arch=$(dpkg --print-architecture) \ signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \ https://download.docker.com/linux/debian \ $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list # Install Docker CLI RUN apt-get update && apt-get install -y docker-ce-cli # Add Google Chrome repository and install Chrome RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \ echo "deb [arch=$(dpkg --print-architecture)] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list && \ apt-get update && apt-get install -y google-chrome-stable # Install Node.js and npm RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash - && \ apt-get update && apt-get install -y nodejs && \ npm install -g npm@latest # Verify Node.js and npm installation RUN node --version && npm --version # Clean up RUN apt-get clean && rm -rf /var/lib/apt/lists/* USER jenkins # Install Jenkins plugins RUN jenkins-plugin-cli --plugins "blueocean docker-workflow"
执行构建命令 docker build -t my-jenkins . 时出现错误:
91.92 /bin/sh: 1: npm: not found ------ Dockerfile:27 -------------------- 26 | # Install Node.js and npm 27 | >>> RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash - && \ 28 | >>> apt-get update && apt-get install -y nodejs && \ 29 | >>> npm install -g npm@latest 30 | -------------------- ERROR: failed to solve: process "/bin/sh -c curl -fsSL https://deb.nodesource.com/setup_14.x | bash - && apt-get update && apt-get install -y nodejs && npm install -g npm@latest" did not complete successfully: exit code: 127
问题分析与解决
原因
同一个RUN指令中的所有命令在同一个shell进程中执行。通过apt-get install -y nodejs安装npm后,当前shell的PATH环境变量并未刷新,无法识别新安装的npm可执行文件路径,因此执行npm命令时提示"not found"。
另外,Node.js 14已处于生命周期结束(End-of-Life)状态,存在安全隐患,建议升级至长期支持(LTS)版本(如20.x)。
修复步骤
拆分npm升级命令到单独的RUN指令
将安装Node.js和升级npm的操作拆分为两个独立的RUN,新的shell进程会重新读取系统环境变量,从而识别npm的路径:# Install Node.js and npm(升级到20.x LTS版本) RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \ apt-get update && apt-get install -y nodejs # Upgrade npm to latest version RUN npm install -g npm@latest可选:使用npm绝对路径
如果不想拆分RUN指令,可以直接使用npm的绝对路径(通常为/usr/bin/npm或/usr/local/bin/npm):RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \ apt-get update && apt-get install -y nodejs && \ /usr/bin/npm install -g npm@latest验证安装
原有的验证命令RUN node --version && npm --version可以保留,用于确认Node.js和npm安装成功。
修改后的完整Dockerfile
FROM jenkins/jenkins:2.440.1-jdk17 USER root # Update and install necessary packages RUN apt-get update && \ apt-get install -y lsb-release wget gnupg2 apt-transport-https curl # Add Docker official GPG key RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \ https://download.docker.com/linux/debian/gpg # Set up Docker repository RUN echo "deb [arch=$(dpkg --print-architecture) \ signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \ https://download.docker.com/linux/debian \ $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list # Install Docker CLI RUN apt-get update && apt-get install -y docker-ce-cli # Add Google Chrome repository and install Chrome RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \ echo "deb [arch=$(dpkg --print-architecture)] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list && \ apt-get update && apt-get install -y google-chrome-stable # Install Node.js and npm(使用20.x LTS版本) RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \ apt-get update && apt-get install -y nodejs # Upgrade npm to latest version RUN npm install -g npm@latest # Verify Node.js and npm installation RUN node --version && npm --version # Clean up RUN apt-get clean && rm -rf /var/lib/apt/lists/* USER jenkins # Install Jenkins plugins RUN jenkins-plugin-cli --plugins "blueocean docker-workflow"
内容的提问来源于stack exchange,提问作者Jeff
相关产品推荐
相关产品推荐

