52

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?

Delirium
  • 1,033
  • 3
  • 14
  • 25

11 Answers11

101

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.

castis
  • 8,041
  • 4
  • 41
  • 62
Nathan
  • 1,011
  • 2
  • 4
  • 2
  • 5
    Why do you think so? – Dawid Laszuk Sep 01 '19 at 18:26
  • 3
    This seems like a better answer, as it lets you easily specify the Node version, and the docker build is much faster with nvm, versus building from source within docker – Sam Jun 20 '20 at 17:41
  • 2
    To add how fast: Answer from Nathaniel took 5 minutes before I quit the build to try this one. This one took around 15 secs. – Marek Židek Aug 28 '20 at 09:40
  • 7
    FYI, for anyone else wanting to put the NVM_DIR in some non-standard location, the `ENV NVM_DIR=/somewhere-else/.nvm` line must go before the "curl | bash" line in order to be respected by the install script. – jdmcnair Oct 16 '20 at 02:29
  • FYI, the ```NVM_DIR``` was giving an error that the path does not exists. So I had ```mkdir``` the path before ```curl``` line – Sagar Kamble Sep 23 '21 at 17:54
  • line 3 should be `RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash` *the path has an extra `/install.sh` at the end – dgrijuela Nov 21 '21 at 12:01
30

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
NiRR
  • 4,382
  • 4
  • 29
  • 53
Nathaniel Waisbrot
  • 21,093
  • 6
  • 69
  • 94
  • 3
    If you are looking for a more recent answer have a look at this one below https://stackoverflow.com/a/67491580/411428 His/her/their idea work just fine as well and does not require `sudo` which you may not want or have in your docker image. – Manfred May 22 '21 at 23:39
22

Just 2 lines

RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - 
RUN apt-get install -y nodejs
giapnh
  • 2,382
  • 21
  • 19
  • This line gives me following error message: `debconf: unable to initialize frontend: Dialog debconf: (TERM is not set, so the dialog frontend is not usable.) debconf: falling back to frontend: Readline debconf: unable to initialize frontend: Readline debconf: (This frontend requires a controlling tty.) debconf: falling back to frontend: Teletype dpkg-preconfigure: unable to re-open stdin:` – alper Apr 15 '22 at 22:06
20

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
Manfred
  • 4,924
  • 3
  • 30
  • 28
loretoparisi
  • 14,485
  • 11
  • 91
  • 127
  • 3
    Great answer! Short, to the point and it just works. Nice! To use LTS just replace `latest` with `lts` in the code snippet of the answer. – Manfred May 22 '21 at 23:43
14

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
gulp
  • 529
  • 3
  • 14
7

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.

Sijo M Cyril
  • 542
  • 1
  • 6
  • 13
6

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
Maryan
  • 1,135
  • 10
  • 14
2

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/

Maoz Zadok
  • 3,925
  • 2
  • 27
  • 38
1

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.

bersling
  • 14,793
  • 7
  • 51
  • 61
1
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
Benoit
  • 31
  • 3
0

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
jla
  • 8,833
  • 2
  • 19
  • 17