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

Docker Ubuntu环境安装Node.js v20失败,版本显示v12.22.9

Docker Ubuntu 22.04容器Node.js v20安装异常问题分析及解决

问题现象

在Ubuntu 22.04容器中通过Dockerfile安装Node.js v20,但进入容器执行node -v显示版本为v12.22.9,而非预期的v20.x。

原Dockerfile内容

FROM --platform=linux/amd64 ubuntu:22.04
RUN apt-get update
RUN apt-get -y install imagemagick
RUN apt-get -y install software-properties-common
RUN add-apt-repository ppa:fontforge/fontforge
RUN apt-get -y install fontforge
RUN apt-get -y install pandoc
RUN apt-get -y install texlive
RUN apt-get -y install ffmpeg
RUN apt-get -y install rar
RUN apt-get -y install inkscape
RUN apt-get -y install git
RUN apt-get -y install libreoffice
RUN apt-get upgrade
RUN apt-get -y install curl
RUN apt-get -y install gnupg
RUN curl -sL https://deb.nodesource.com/setup_20.x  | bash -
RUN apt-get -y install nodejs
RUN apt-get -y install npm
RUN npm install -g pnpm
RUN bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"

构建及运行日志片段

=> CACHED [13/21] RUN apt-get -y install libreoffice                      0.0s
=> CACHED [14/21] RUN apt-get upgrade                                     0.0s
=> CACHED [15/21] RUN apt-get -y install curl                             0.0s
=> CACHED [16/21] RUN apt-get -y install gnupg                            0.0s
=> [17/21] RUN curl -sL https://deb.nodesource.com/setup_20.x  | bash -   4.4s
=> [18/21] RUN apt-get -y install nodejs                                  6.0s
=> [19/21] RUN apt-get -y install npm                                    47.6s
=> [20/21] RUN npm install -g pnpm                                        2.1s
=> [21/21] RUN bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"        0.1s

$ docker exec -it mycontainer sh
# node -v
v12.22.9

问题原因

  1. 单独安装npm触发版本降级
    NodeSource提供的nodejs包已经包含对应版本的npm,单独执行apt-get -y install npm会从Ubuntu默认软件源拉取旧版本npm,而该版本npm依赖的nodejs是Ubuntu 22.04默认源中的旧版本(v12.22.9),这会自动将已安装的NodeSource v20 nodejs替换为旧版本。

  2. Docker缓存层复用导致旧版本残留
    构建日志中多个步骤显示CACHED,说明Docker复用了之前构建的缓存层。如果之前的构建过程中安装过旧版本nodejs,缓存层中会残留旧的可执行文件或路径配置,即使后续安装了新版本,也可能因为路径优先级问题调用旧版本。

  3. 源更新不及时
    虽然setup_20.x脚本会自动更新源缓存,但如果之前的apt-get update缓存未失效,可能导致安装nodejs时无法获取到NodeSource的最新包信息。

解决办法

1. 修复Dockerfile

移除单独安装npm的步骤,合并相关命令以减少缓存问题,并确保源更新:

FROM --platform=linux/amd64 ubuntu:22.04
# 合并基础依赖安装,减少镜像层数
RUN apt-get update && \
    apt-get -y install --no-install-recommends \
        imagemagick \
        software-properties-common \
        fontforge \
        pandoc \
        texlive \
        ffmpeg \
        rar \
        inkscape \
        git \
        libreoffice \
        curl \
        gnupg && \
    add-apt-repository ppa:fontforge/fontforge && \
    apt-get update && \
    # 执行NodeSource脚本添加v20源
    curl -sL https://deb.nodesource.com/setup_20.x | bash - && \
    # 安装nodejs(已包含对应版本npm)
    apt-get -y install nodejs && \
    # 安装pnpm
    npm install -g pnpm && \
    # 安装LLVM
    bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)" && \
    # 清理apt缓存,减小镜像体积
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

2. 强制清理缓存重新构建

执行构建命令时加上--no-cache参数,彻底避免复用旧缓存层:

docker build --no-cache -t myorg/myimage .

3. 验证安装结果

重新构建完成后进入容器验证:

docker exec -it mycontainer sh
node -v  # 应显示v20.x.x
npm -v   # 应显示对应v20的npm版本

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 15:22:48