node.js 在 Dockerfile 中安装节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36399848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:05:43  来源:igfitidea点击:

Install node in Dockerfile?

node.jsamazon-web-servicesjenkinsdockerless

提问by Delirium

I am user of AWS elastic beanstalk, and I have a little problem. I want to build my CSS files with less+node. But I don`t know how to install node in my dockerfile, when building with jenkins.

我是 AWS elastic beanstalk 的用户,我有一个小问题。我想用 less+node 构建我的 CSS 文件。但是在使用 jenkins 构建时,我不知道如何在我的 dockerfile 中安装节点。

Here is installation packages what I am using in my docker. I will be glad for any suggestions.

这是我在 docker 中使用的安装包。我会很高兴有任何建议。

FROM php:5.6-apache


# Install PHP5 and modules along with composer binary
RUN apt-get update
RUN apt-get -y install \
    curl \
    default-jdk \
    git \
    libcurl4-openssl-dev \
    libpq-dev \
    libmcrypt-dev \
    libpq5 \
    npm \
    node \
    zlib1g-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng12-dev

RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/

RUN docker-php-ext-install curl json mbstring opcache pdo_mysql zip gd exif sockets mcrypt

# Install pecl
RUN pecl install -o -f memcache-beta \
    && rm -rf /tmp/pear \
    && echo 'extension=memcache.so' > /usr/local/etc/php/conf.d/memcache.ini

After this I am runing my entrypoint.sh with code

在此之后,我使用代码运行我的 entrypoint.sh

#!/usr/bin/env sh

composer run-script post-install-cmd --no-interaction

chmod 0777 -R /var/app/app/cache
chmod 0777 -R /var/app/app/logs

exec apache2-foreground

But then I`ve got this error

但是后来我遇到了这个错误

 Error Output: [2016-04-04 11:23:44] assetic.ERROR: The template ":tmp:module.html.twig" contains an error: A template that extends another one cannot have a body in ":tmp:module.ht  
  ml.twig" at line 7.     

But when I install inside the Docker container node this way

但是当我以这种方式安装在 Docker 容器节点中时

apt-get install git-core curl build-essential openssl libssl-dev
 git clone https://github.com/nodejs/node.git
 cd node
 ./configure
 make
 sudo make install
 node -v

I can build my CSS. So question is..how this installation above make install inside my Dockerfile when I am building it with Jenkins?

我可以构建我的 CSS。所以问题是..当我用 Jenkins 构建它时,上面的这个安装是如何在我的 Dockerfile 中安装的?

采纳答案by Nathaniel Waisbrot

Running apt-get install nodedoes not install Node.js, because that's not the package you're asking for.

运行apt-get install node不会安装 Node.js,因为那不是您要的包。

If you run apt-cache info nodeyou can see that what you are installing is a "Amateur Packet Radio Node program (transitional package)"

如果你运行apt-cache info node可以看到你安装的是一个“业余分组无线电节点程序(过渡包)”

You should follow the Node.js install instructionsto install via package manager.

您应该按照Node.js 安装说明通过包管理器进行安装。

Or if you like building from git, you can just do that inside Docker:

或者,如果您喜欢从 git 构建,您可以在 Docker 中执行此操作:

RUN apt-get install git-core curl build-essential openssl libssl-dev \
 && git clone https://github.com/nodejs/node.git \
 && cd node \
 && ./configure \
 && make \
 && sudo make install

回答by Nathan

I think this works slightly better.

我认为这稍微好一点。

ENV NODE_VERSION=12.6.0
RUN apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.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

回答by Sijo M Cyril

I am using following Dockerfile to setup node version 8.10.0.

我正在使用以下 Dockerfile 来设置节点版本 8.10.0。

Here I have used NVM (Node Version Manager ), so we can choose which node version should be installed on that container. Please use absolute path of npm when installing node modules (eg: /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm install leasot@latest -g)

在这里我使用了NVM(节点版本管理器),因此我们可以选择应该在该容器上安装哪个节点版本。安装节点模块时请使用npm的绝对路径(例如:/root/.nvm/versions/node/v${NODE_VERSION}/bin/npm install leasot@latest -g)

   FROM ubuntu:18.04
   ENV NODE_VERSION=8.10.0
   RUN apt-get update && \
       apt-get install wget curl ca-certificates rsync -y
   RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/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}
   RUN cp /root/.nvm/versions/node/v${NODE_VERSION}/bin/node /usr/bin/
   RUN cp /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm /usr/bin/
   RUN /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm install  leasot@latest -g

Note: This is a cropped Dockerfile.

注意:这是一个裁剪的 Dockerfile。

回答by gulp

Get the node image and put it at the top of your dockerfile:

获取节点图像并将其放在 dockerfile 的顶部:

FROM node:[tag_name] AS [alias_name]

Verify the version by adding following code:

通过添加以下代码来验证版本:

RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version

Then add the following code every time you need to use nodejs in a container:

然后每次需要在容器中使用 nodejs 时添加以下代码:

COPY --from=[alias_name] . .



From the codes above, replace the following with:

从上面的代码中,将以下内容替换为:

[tag_name]- the tag value of the node image you want to use. Visit https://hub.docker.com/_/node?tab=tagsfor the list of available tags.

[tag_name]- 要使用的节点图像的标签值。访问https://hub.docker.com/_/node?tab=tags获取可用标签列表。

[alias_name]- your preferred image name to use in your dockerfile.

[alias_name]- 在 dockerfile 中使用的首选图像名称。



Example:

例子:

FROM node:latest AS node_base

RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version


FROM php:5.6-apache

COPY --from=node_base . .

### OTHER CODE GOES HERE