node.js - 在Dockerfile中安装Node.js和NPM

标签 node.js docker npm

上下文

我有一个Dockerfile创建包含Apache Web服务器的镜像。但是,我也想使用Dockerfile构建我的网站,以便构建过程不依赖于开发人员本地环境。请注意,docker容器将仅用于本地开发,而不用于生产。

问题

我有这个Dockerfile:

FROM httpd
RUN apt-get update -yq
RUN apt-get -yq install curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash
RUN apt-get update -yq
RUN apt-get install -yq \
        dh-autoreconf=19 \
        ruby=1:2.5.* \
        ruby-dev=1:2.5.* \
        nodejs

我建立它:
sudo docker build --no-cache .

构建成功完成,这是输出的一部分:
Step 9/15 : RUN curl -sL https://deb.nodesource.com/setup_12.x | bash
 ---> Running in e6c747221ac0
......
......
......
Removing intermediate container 5a07dd0b1e01
 ---> 6279003c1e80
Successfully built 6279003c1e80

但是,当我使用此命令在容器中运行图像时:
sudo docker container run --rm -it --name=debug 6279003c1e80 /bin/bash

然后,在容器内执行apt-cache policy时,它不会显示应该使用curl命令添加的存储库。此外,在执行apt-cache policy nodejs时,它还会显示已安装旧版本。

但是,当我在容器中运行以下命令时:
curl -sL https://deb.nodesource.com/setup_12.x | bash
apt-cache policy
apt-cache policy nodejs

它显示了已添加存储库,并显示了可用的更新的nodejs版本。

那么,为什么在docker文件中使用带有RUN的curl命令时,它似乎不起作用,但是从shell在容器中手动进行操作就可以了吗?我该如何解决这个问题?

更新
  • 注意,为了防止出现缓存问题,我使用了--no-cache标志。
  • 我还删除了所有容器,并做了sudo docker system prune并重建了图像,但没有成功。
  • 我尝试按照用户“hmm”的建议将所有内容 bundle 在一个RUN命令中(因为这是apt命令的最佳实践):
  • RUN apt-get update -yq \
        && apt-get -yq install curl gnupg && \
        && curl -sL https://deb.nodesource.com/setup_12.x | bash \
        && apt-get update -yq \
        && apt-get install -yq \
            dh-autoreconf=19 \
            ruby=1:2.5.* \
            ruby-dev=1:2.5.* \
            nodejs \
        && rm -rf /var/lib/apt/lists/*
    

    最佳答案

    您可能会遇到缓存图层的问题。 Dockerfile最佳实践文档中有一个关于apt-get的long section。大概值得一读。

    要点是,Docker无法识别第一个和第二个RUN apt-get update之间的区别,也不知道apt-get install取决于新的apt-get update层。

    解决方案是将所有这些组合到单个RUN命令中(推荐),或者在构建过程中禁用缓存(docker build --no-cache)。

    RUN apt-get update -yq \
        && apt-get -yq install curl gnupg ca-certificates \
        && curl -L https://deb.nodesource.com/setup_12.x | bash \
        && apt-get update -yq \
        && apt-get install -yq \
            dh-autoreconf=19 \
            ruby=1:2.5.* \
            ruby-dev=1:2.5.* \
            nodejs
    

    编辑:在本地运行Dockerfile,我发现curl命令没有输出。删除-s标志(静默失败)后,您会看到它由于无法验证服务器的SSL证书而失败:
    curl: (60) SSL certificate problem: unable to get local issuer certificate
    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.
    

    该问题的解决方案是在运行ca-certificates之前安装curl。我已经更新了上面的RUN命令。

    关于node.js - 在Dockerfile中安装Node.js和NPM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62325403/

    相关文章:

    javascript - 使用node js和mongodb时出现多个连接错误

    mysql - 我如何在 Sequelize findAll 中检索静态值作为列

    无法访问 Docker 远程 API

    node.js - 我可以显示 npm 运行的扩展命令吗?

    node.js - 如何在 Node 中导入全局模块?我得到 "Error: Cannot find module <module>"?

    node.js - 续更新

    node.js - NodeJS 与 Einaros WebSocket : Client Ping Server VS Server Ping Client

    Docker Gatsby Strapi 生成 ECONNREFUSED 127.0.0.1 :1337 error

    docker - 在 pods 创建时设置环境变量的最佳方法是什么?

    npm - 找不到gulp命令