我在 Docker 容器中安装了 Debian,并且像这样安装 NodeJS:
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash ;\
apt-get update; \
apt-get install -y nodejs
它已经运行了一年之久(依赖项在此步骤之前已安装,但为了简洁起见未在引文中提及)。
现在 Node 已经更新了版本,官方推荐的安装方式也发生了改变:
mkdir -p /etc/apt/keyrings; \
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg; \
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list; \
apt-get update && apt-get install -y nodejs;
这在容器内和 Dockerfile 内有效 - 但在容器内无效。
RUN rm -rf /var/lib/postgresql/data/*; \
chown postgres /var/log/postgresql; \
touch /foresight/postgres.log; \
chown postgres /foresight/postgres.log; \
apt-get update; \
apt-get install -y s3cmd; \
apt-get install -y cron; \
apt-get install -y nginx software-properties-common curl ca-certificates gnupg procps vim gdal-bin unzip; \
apt-get install -y gcc libc6-dev make --no-install-recommends; \
apt-get install -y locales && rm -rf /var/lib/apt/lists/* && localedef -i en_GB -c -f UTF-8 -A /usr/share/locale/locale.alias en_GB.UTF-8; \
apt-get remove nodejs; \
mkdir -p /etc/apt/keyrings; \
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg; \
ls -la /etc/apt/keyrings/; \
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list; \
apt-get update && apt-get install -y nodejs; \
echo "Dockerfile: Node.js version: $(node -v)"; \
echo "Dockerfile: npm version: $(npm -v)";
以上记录没有错误,并记录预期版本。
CMD
但是Dockerfile 中的最终是一个脚本,运行时echo $(node -v)
会输出node -v v12.22.12
。
登录到容器也显示节点 12。
#9 [ 5/12] RUN mkdir -p /etc/apt/keyrings; curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg; echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list; apt-get update && apt-get install -y nodejs; echo "Node.js version: $(node -v)"; echo "npm version: $(npm -v)";
#9 0.519 deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main
#9 0.562 Get:1 http://deb.debian.org/debian bullseye InRelease [116 kB]
#9 0.573 Get:2 http://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB]
#9 0.573 Get:3 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
#9 0.613 Get:4 http://apt.postgresql.org/pub/repos/apt bullseye-pgdg InRelease [123 kB]
#9 0.639 Get:5 https://deb.nodesource.com/node_20.x nodistro InRelease [12.1 kB]
#9 0.694 Get:6 http://deb.debian.org/debian bullseye/main amd64 Packages [8183 kB]
#9 0.806 Get:7 http://deb.debian.org/debian-security bullseye-security/main amd64 Packages [254 kB]
#9 0.917 Get:8 http://deb.debian.org/debian bullseye-updates/main amd64 Packages [17.4 kB]
#9 0.980 Get:9 https://deb.nodesource.com/node_20.x nodistro/main amd64 Packages [3278 B]
#9 1.064 Get:10 http://apt.postgresql.org/pub/repos/apt bullseye-pgdg/main amd64 Packages [301 kB]
#9 2.017 Fetched 9101 kB in 1s (6213 kB/s)
#9 2.017 Reading package lists...
#9 2.635 Reading package lists...
#9 3.243 Building dependency tree...
#9 3.381 Reading state information...
#9 3.586 The following NEW packages will be installed:
#9 3.586 nodejs
#9 3.684 0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
#9 3.684 Need to get 31.1 MB of archives.
#9 3.684 After this operation, 195 MB of additional disk space will be used.
#9 3.684 Get:1 https://deb.nodesource.com/node_20.x nodistro/main amd64 nodejs amd64 20.8.0-1nodesource1 [31.1 MB]
#9 4.179 perl: warning: Setting locale failed.
#9 4.179 perl: warning: Please check that your locale settings:
#9 4.179 LANGUAGE = (unset),
#9 4.179 LC_ALL = (unset),
#9 4.179 LANG = "en_US.utf8"
#9 4.179 are supported and installed on your system.
#9 4.179 perl: warning: Falling back to the standard locale ("C").
#9 4.265 debconf: delaying package configuration, since apt-utils is not installed
#9 4.307 Fetched 31.1 MB in 0s (62.5 MB/s)
#9 4.343 Selecting previously unselected package nodejs.
(Reading database ... 25270 files and directories currently installed.)
#9 4.360 Preparing to unpack .../nodejs_20.8.0-1nodesource1_amd64.deb ...
#9 4.365 Unpacking nodejs (20.8.0-1nodesource1) ...
#9 8.037 Setting up nodejs (20.8.0-1nodesource1) ...
#9 8.098 Node.js version: v20.8.0
#9 8.437 npm version: 10.1.0
#9 DONE 9.0s
答案1
问题是 curl 和 gpg 未安装在默认的 debian 映像上,因此下载签名密钥的命令失败并且存储库无效。
在运行该命令之前安装必要的软件包:
apt-get update; apt-get install curl gpg -y; \
mkdir -p /etc/apt/keyrings; \
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg; \
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list; \
apt-get update && apt-get install -y nodejs;