【问题标题】:Installing specific version of node.js and npm in ubuntu image with Dockerfile使用 Dockerfile 在 ubuntu 映像中安装特定版本的 node.js 和 npm
【发布时间】:2016-08-22 14:09:05
【问题描述】:

我想知道如何更新我的自定义 Dockerfile 以安装 Node v6.3.1 和 NPM v3.10.6 而不会破坏已经存在的内容。

目前这是我的自定义文件:

FROM         ubuntu:16.10

MAINTAINER   Fátima Alves

COPY         . /my-software
WORKDIR        /my-software

RUN          apt-get update          \
                                  && \
             apt-get install -y      \
               python-dev            \
             tesseract-ocr

谢谢!


更新

目前我的dockerfile是这样的:

FROM         ubuntu:16.10

MAINTAINER   Fátima Alves

COPY         ./dist            /my-software
COPY         ./s3-config.json  /my-software
COPY         ./_*              /my-software
COPY         ./node_modules    /my-software
WORKDIR                        /dataextractor

RUN          apt-get update          \
                                  && \
             apt-get install -y      \
             curl

RUN         curl -sL https://deb.nodesource.com/setup_6.x | bash - \
            && apt-get install -y nodejs

并且正在返回:

The command '/bin/sh -c curl -sL https://deb.nodesource.com/setup_6.x | bash -             && apt-get install -y nodejs' returned a non-zero code: 1

也许我错过了什么?

【问题讨论】:

    标签: node.js ubuntu docker dockerfile


    【解决方案1】:

    您可以按照通常的 Ubuntu 安装说明进行操作,就在 Dockerfile 中的 RUN 语句中

    RUN curl -sL https://deb.nodesource.com/setup_6.x | bash - \
        && apt-get install -y nodejs
    

    Docs

    【讨论】:

    • 我应该把这个放在第一次运行之后吗?
    • 是的。如果需要,您可以使用 && 将它们放在同一个语句中,但我暂时将它们分开。
    • 对不起,你不需要在curl 命令之后使用管道传送到 sudo。您在 docker 中被设置为 root。已更新上述命令。
    • 等一下,让我试试这个
    • 有没有办法安装 8.9.4 中的确切版本?在上述情况下,我们只要求一个主要版本。我想在我的容器中使用特定的节点版本。
    【解决方案2】:

    为什么

    因为https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions 建议使用“curl pipe bash”反模式,所以让我们试着让它更干净。

    什么

    由于容器是根据确定的操作系统和版本构建的,我们不需要该 bash 脚本的通用性。

    如何

    如果我们仔细检查,我们会发现 https://deb.nodesource.com/setup_6.x 实际上只为 Debian 做了 2 件事

    1. 通过apt-key addtheir public key添加到apt的钥匙串中
    2. 将他们的deb repo 添加到新创建的文件/etc/apt/sources.list.d/nodesource.list

    添加来源

    第二件事我们可以很容易地做到。这只是把它放在你的 Dockerfile 中:

    COPY nodesource.list  /etc/apt/sources.list.d/nodesource.list
    

    当然,您需要创建 nodesource.list,内容如下:

    deb     https://deb.nodesource.com/node_6.x trusty main
    deb-src https://deb.nodesource.com/node_6.x trusty main
    

    添加可信密钥

    第一件事对于新的“干净”来说有点棘手。我宁愿将钥匙串文件添加到/etc/apt/trusted.gpg.d/,也不愿修改现有的/etc/apt/trusted.gpg 文件(apt-key add 会这样做)。

    他们在 URL https://deb.nodesource.com/gpgkey/nodesource.gpg.key 上拥有的是公钥,而不是钥匙串。要获取钥匙串文件,我们可以像这样通过管道将其 [不是apt-key,而是]:

    curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | \
    gpg --import --no-default-keyring --keyring ./nodesource.gpg
    

    这会创建nodesource.gpg,我们可以通过将其放入您的 Dockerfile 中来使用它:

    COPY nodesource.gpg   /etc/apt/trusted.gpg.d/nodesource.gpg
    

    照常安装

    疯狂的间距和\ 终止行是我使用的,因为我倾向于安装很多额外的包。

    # Install software packages
    ARG DEBIAN_FRONTEND=noninteractive
    RUN apt-get update -qq && apt-get clean
    RUN apt-get install -qqy \
                             nodejs \
        && \
        apt-get clean
    

    你可以在https://gist.github.com/RichardBronosky/f748563dc328b12b39cd864973fcb138#file-dockerfile看到完整的Dockerfile

    【讨论】:

      猜你喜欢
      • 2018-09-29
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多