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

回答 5 浏览 1.3万 2023-09-01

我曾经使用 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 

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

    => [base 3/7] RUN curl -sL https://deb.nodesource.com/setup_18.x | bash -
                               SCRIPT DEPRECATION WARNING
    ================================================================================ 
TO AVOID THIS WAIT MIGRATE THE SCRIPT Continuing in 60 seconds (press Ctrl-C to abort) ...

我如何解决它?

Jesús López 提问于2023-09-01
查看用于构建官方镜像的 Node 官方 Dockerfile,该镜像可从 docker-hub 获取。github.com/nodejs/docker-node/blob/main/20/bullseye/DockerfileJorge Ruiz Gómez 2023-09-01
5 个回答
#1楼 已采纳
得票数 42

来自 脚本 的通知是

  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

github 上的说明相当于一个 Dockerfile RUN

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;

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

FROM docker.io/node:18-bookworm-slim
Matt 提问于2023-09-01
Matt 修改于2023-10-30
&& 而不是 ; 来串联这些命令不是更好吗?因为除非前面的命令都成功执行,否则我们不想让命令运行?drmrbrewer 2023-10-14
set -e 也达到了同样的效果。Matt 2023-10-16
啊,今天学到了东西,谢谢。drmrbrewer 2023-10-16
#2楼
得票数 11

根据 nodesource/distributions GitHub 存储库

Installation Scripts: The installation scripts setup_XX.x are no longer supported and are not needed anymore, as the installation process is straightforward for any RPM and DEB distro.

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

Installation Instructions Node.js

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

Download and import the Nodesource GPG key

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

Create deb repository

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

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

NODE_MAJOR=16  
NODE_MAJOR=18  
NODE_MAJOR=20

Run Update and Install

sudo apt-get update sudo apt-get install nodejs -y

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

Emanuele Scarabattoli 提问于2023-09-01
Emanuele Scarabattoli 修改于2023-09-01
#3楼
得票数 1

根据这里说明

 # updating nodejs
    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" \
         | sudo tee /etc/apt/sources.list.d/nodesource.list \
        && apt-get update \
        && apt-get install nodejs -y;
Garibaldy Cramer 提问于2023-10-21
#4楼
得票数 1

假设您想要版本 18 并且您的系统使用 DEB 软件包,您可以像这样安装它:

curl -L https://deb.nodesource.com/nsolid_setup_deb.sh | bash -s -- 18
apt-get install nodejs -y

或者,如果您的系统使用RPM包:

curl -L https://rpm.nodesource.com/nsolid_setup_rpm.sh | bash -s -- 18
yum install nodejs -y --setopt=nodesource-nodejs.module_hotfixes=1

来源:https://github.com/nodesource/distributions#installation-scripts

Kiruahxh 提问于2023-10-31
Kiruahxh 修改于2023-10-31
#5楼
得票数 0

只需使用官方节点映像并避免从包管理器重新安装包。

使用官方节点镜像如下。 (对于节点 18.x)

FROM node:18-bookworm-slim
Kavindu Chamith 提问于2023-11-23
如果您同时需要 .NET 和 Nodejs 怎么办?没有任何官方图片同时具备这两者。Jesús López 2023-11-23