使用nodesource安装脚本在docker容器上安装nodejs时出现弃用警告

Jes*_*pez 35 node.js docker deprecation-warning dockerfile

我曾经使用 Dockerfile 中的以下内容在基于 Debian 的容器上安装 Nodejs:

RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl git nano
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash - 
RUN apt-get install nodejs -y 
Run Code Online (Sandbox Code Playgroud)

但我最近开始收到以下消息:

RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl git nano
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash - 
RUN apt-get install nodejs -y 
Run Code Online (Sandbox Code Playgroud)

我如何解决它?

Mat*_*att 47

脚本的通知是

  This script, located at https://deb.nodesource.com/setup_X, used to
  install Node.js is deprecated now and will eventually be made inactive.

  Please visit the NodeSource distributions Github and follow the
  instructions to migrate your repo.
  https://github.com/nodesource/distributions 

  The NodeSource Node.js Linux distributions GitHub repository contains
  information about which versions of Node.js and which Linux distributions
  are supported and how to install it.
  https://github.com/nodesource/distributions
Run Code Online (Sandbox Code Playgroud)

github上的指令相当于一个DockerfileRUN

FROM docker.io/debian:12-slim
RUN set -uex; \
    apt-get update; \
    apt-get install -y ca-certificates curl gnupg; \
    mkdir -p /etc/apt/keyrings; \
    curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key \
     | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg; \
    NODE_MAJOR=18; \
    echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" \
     > /etc/apt/sources.list.d/nodesource.list; \
    apt-get -qy update; \
    apt-get -qy install nodejs;
Run Code Online (Sandbox Code Playgroud)

docker.io/node:18如果您想节省一些时间,Node.js 项目维护的镜像是基于 Debian 的。

FROM docker.io/node:18-bookworm-slim
Run Code Online (Sandbox Code Playgroud)

  • `set -e` 实现了相同的效果。 (3认同)
  • 用“&&”而不是“;”链接这些命令不是更好吗,因为我们不希望命令在所有先前的命令都成功之前运行? (2认同)

Ema*_*oli 12

根据nodesource/distributions GitHub存储库

安装脚本:安装脚本 setup_XX.x 不再受支持,也不再需要,因为任何 RPM 和 DEB 发行版的安装过程都很简单。

因此,要安装node.js,您可以使用此处解释的新方法

Node.js 安装说明

If you're root, you could just ommit the sudo

下载并导入 Nodesource GPG 密钥

sudo apt-get update sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings  
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
Run Code Online (Sandbox Code Playgroud)

创建 deb 存储库

NODE_MAJOR=20  
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
Run Code Online (Sandbox Code Playgroud)

Optional: NODE_MAJOR can be changed depending on the version you need.

NODE_MAJOR=16  
NODE_MAJOR=18  
NODE_MAJOR=20
Run Code Online (Sandbox Code Playgroud)

运行更新并安装

sudo apt-get update sudo apt-get install nodejs -y
Run Code Online (Sandbox Code Playgroud)

或者你可以使用官方的docker镜像https://hub.docker.com/_/node/