在 dockerfile 问题中安装 nodejs

installing nodejs in dockerfile issue

我正在尝试使用 pyenv 在 dockerfile 中安装 nodejs,但是当我 运行 通过我的 gitlab 运行ner 时,我一直收到此错误。我正在尝试安装版本 16.12.0。这个问题有更好的解决方案吗?

Docker 文件

#install npm
ENV NODE_VERSION=16.12.0
RUN apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN node --version
RUN npm --version

输出

Step 15/25 : ENV NODE_VERSION=16.12.0
 ---> Running in 7623dfe4669c
Removing intermediate container 7623dfe4669c
 ---> c1486340596a
Step 16/25 : RUN apt install -y curl
 ---> Running in a4661b68566b
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Reading package lists...
Building dependency tree...
Reading state information...
curl is already the newest version (7.68.0-1ubuntu2.7).
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
Removing intermediate container a4661b68566b
 ---> d727779ba39b
Step 17/25 : RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
 ---> Running in c3661e8eead3
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (60) SSL certificate problem: self signed certificate in certificate chain
More details here: https://curl.haxx.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
Removing intermediate container c3661e8eead3
 ---> beffd784c86b
Step 18/25 : ENV NVM_DIR=/root/.nvm
 ---> Running in 22b69a1563b2
Removing intermediate container 22b69a1563b2
 ---> 821b73dfd5fa
Step 19/25 : RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
 ---> Running in 54c168c88ec7
/bin/sh: 1: .: Can't open /root/.nvm/nvm.sh
The command '/bin/sh -c . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}' returned a non-zero code: 127
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 127

一般来说,您应该避免使用像 Docker 中的 nvm 这样的版本管理器。您一次不应该需要一个以上版本的语言解释器,并且版本管理器通常需要 shell 点文件设置,这在 Docker.

中配置起来很复杂

你不说你为什么需要 Node。最简单的方法是使用 Docker Hub node image

FROM node:lts
# and none of what you show in the question

如果您使用它来构建其他 Python 应用程序的前端,您可以为此使用 multi-stage build

FROM node:lts AS frontend
WORKDIR /frontend
COPY frontend/package*.json ./
RUN npm ci
COPY frontend/ ./
RUN npm build

FROM python:3.10
...
COPY --from=frontend /frontend/dist ./static/

如果您的 Ubuntu-based 映像中确实需要 Node,那么接下来要做的最简单的事情就是安装它。 default Ubuntu nodejs package 应该可以很好地满足大多数实际用途。

FROM ubuntu:20.04
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive \
    apt-get --no-install-recomends --assume-yes \
      nodejs

如果你真的需要在自定义镜像中使用它,并且它确实需要一个超级特定版本的 Node,你应该可以 download Node and install it.

FROM ubuntu:20.04
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive \
    apt-get --no-install-recomends --assume-yes \
      curl \
      xz-utils
ARG node_version=v16.14.2
RUN cd /opt \
 && curl -LO https://nodejs.org/dist/${node_version}/node-${node_version}-linux-x64.tar.xz \
 && tar xJf node-${node_version}-linux-x64.tar.xz \
 && rm node-${node_version}-linux-x64.tar.xz
ENV PATH=/opt/node-${node_version}-linux-x64/bin:${PATH}