如何在 Dockerfile 中使用 yarn 包管理器安装 Node.js

问题描述

我想在我的 Dockerfile 中安装带有 yarn.pkg 的 Node.js。
Dockerfile 用于 Laravel8/PHP8 项目。

这是我的实际尝试:

RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list
RUN apt-get update -y \
    && apt-get install -y --no-install-recommends \
        nodejs \
        yarn \
        MysqL-client

如果我以这种方式尝试,我会在我的 shell 上得到这个输出

 => CACHED [ 8/20] RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -                                                                                                                                                                                      0.0s
 => ERROR [ 9/20] RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -                                                                                                                                                                             0.4s
------
 > [ 9/20] RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -:
#12 0.331 Warning: apt-key output should not be parsed (stdout is not a terminal)
#12 0.384 gpg: Segmentation fault
#12 0.384 no valid OpenPGP data found.
------
Failed to solve: rpc error: code = UnkNown desc = executor Failed running [/bin/sh -c curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -]: exit code: 2

有没有办法解决这个问题?
如果您需要更多相关信息,请告诉我。

问候,
曼尼

解决方法

嗯... 3D Bounding Box(节点包管理器)与 check whether his box fits the expected/Allowed size 一起安装,而 NPM 只是一个像 Node.js 一样安装的模块

只需编辑和使用 another post mentions 喜欢的内容:

Yarn

注意,这是我所知道的管理npm install -g yarn版本的最佳方式。

,

Top-Master,非常感谢您的意见。它对我找到解决方案有很大帮助。

这是……
我使用了您在上面发布的 Dockerfile 并进行了一些小的更改:

ENV NODE_VERSION=16.5
ENV NVM_DIR=/root/.nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
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 npm install -g yarn

问题是,RUN npm install -g yarn 命令遇到错误并且构建失败。经过一番谷歌搜索后,我找到了这个网页 https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
他们建议,由于目录结构和用户权限,应单独安装 npm。如果将 npm 与 Node.js 一起安装,它们就不一样了。

所以我这样做了:

ENV NODE_VERSION=16.5
ENV NVM_DIR=/root/.nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
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 apt-get update \
    && apt-get install -y --no-install-recommends \
        npm
RUN npm install -g yarn

对我(我也希望对许多其他人)它现在有效:)

再次感谢!
玩得开心!