Install node in Dockerfile?

node.jsAmazon Web-ServicesJenkinsDockerLess

node.js Problem Overview


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.

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

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

#!/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

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?

node.js Solutions


Solution 1 - node.js

I think this works slightly better.

ENV NODE_VERSION=16.13.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

>Note that nvm is a version manager for node.js, designed to be installed per-user, and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.

Solution 2 - node.js

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

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

You should follow the Node.js install instructions to install via package manager.

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

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

Solution 3 - node.js

Just 2 lines

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

Solution 4 - node.js

According to the following answer, I would suggest using npm via the n package, that lets you choose the nodejs version, or use the latest tag or the lts tag. For example for latest:

RUN apt-get update && apt-get install -y \
    software-properties-common \
    npm
RUN npm install npm@latest -g && \
    npm install n -g && \
    n latest

Solution 5 - node.js

Get the node image and put it at the top of your 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:

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=tags for the list of available tags.

[alias_name] - your preferred image name to use in your 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

Solution 6 - node.js

I am using following Dockerfile to setup node version 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)

   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.

Solution 7 - node.js

Binary download without any compilation

FROM ubuntu

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

ARG NODE_VERSION=14.16.0
ARG NODE_PACKAGE=node-v$NODE_VERSION-linux-x64
ARG NODE_HOME=/opt/$NODE_PACKAGE

ENV NODE_PATH $NODE_HOME/lib/node_modules
ENV PATH $NODE_HOME/bin:$PATH

RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz | tar -xzC /opt/

# comes with npm
# RUN npm install -g typescript

Solution 8 - node.js

The short answer, for example, install v14.17.1

ENV PATH="/opt/node-v14.17.1-linux-x64/bin:${PATH}"
RUN curl https://nodejs.org/dist/v14.17.1/node-v14.17.1-linux-x64.tar.gz |tar xzf - -C /opt/ 

list of all available versions can be found here -> https://nodejs.org/dist/

Solution 9 - node.js

The accepted answer gives the link to the installation instructions for all systems, but it won't run out of the box since you often (e.g. for ubuntu) don't have all required dependencies installed (namely curl and sudo).

So here's for example how you'd do it for ubuntu:

FROM ubuntu

# Core dependencies
RUN apt-get update && apt-get install -y curl sudo

# Node
# Uncomment your target version
# RUN curl -fsSL https://deb.nodesource.com/setup_10.x | sudo -E bash -
# RUN curl -fsSL https://deb.nodesource.com/setup_12.x | sudo -E bash -
# RUN curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
# RUN curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
RUN sudo apt-get install -y nodejs
RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version

then build with

docker build . --progress=plain

to see the output of the echo statements. Of course you could also leave away the echo statements and run it regularly with docker build ., after you've made sure everything is working as intended.

You can also leave away the installation of sudo, but then you'll have to get rid of the sudo occurrences in the script.

Solution 10 - node.js

FROM ubuntu:20.04

# all necessaries for next RUN
RUN set -e; \
    apt-get update && \
    apt-get install -qqy --no-install-recommends \
    curl wget nano gnupg2 software-properties-common && \
    rm -rf /var/lib/apt/lists;

RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -

# uncomment for checking versions
  # Step 4/10 : RUN apt-cache show nodejs | grep Version;return 1;
  #  ---> Running in xxxxxxxxx
  # Version: 14.18.2-deb-1nodesource1
  # Version: 10.19.0~dfsg-3ubuntu1
#RUN apt-cache show nodejs | grep Version;return 1;

RUN set -e; \
    apt-get update && \
    apt-get install -qqy \
    nodejs && \
    rm -rf /var/lib/apt/lists;

# uncomment for check
# RUN node  -v

Solution 11 - node.js

Directly into /usr/local so it's already in your $PATH

ARG NODE_VERSION=8.10.0
RUN curl https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz | tar -xz -C /usr/local --strip-components 1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeliriumView Question on Stackoverflow
Solution 1 - node.jsNathanView Answer on Stackoverflow
Solution 2 - node.jsNathaniel WaisbrotView Answer on Stackoverflow
Solution 3 - node.jsgiapnhView Answer on Stackoverflow
Solution 4 - node.jsloretoparisiView Answer on Stackoverflow
Solution 5 - node.jsgulpView Answer on Stackoverflow
Solution 6 - node.jsSijo M CyrilView Answer on Stackoverflow
Solution 7 - node.jsMaryanView Answer on Stackoverflow
Solution 8 - node.jsMaoz ZadokView Answer on Stackoverflow
Solution 9 - node.jsberslingView Answer on Stackoverflow
Solution 10 - node.jsBenoitView Answer on Stackoverflow
Solution 11 - node.jsjlaView Answer on Stackoverflow