0
votes

I have the start of my Dockerfile as below. Currently this installs version 10 of Node JS but I need a minimum of version 12. How can I change this to get v12?

FROM ruby:2.6.5

# Install 3rd party dependencies.
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
    apt update && \
    apt -y install nodejs \
    yarn \
    build-essential \
    libpq-dev

EDIT:

Error after trying @enrique-tejeda's answer:


[ 2/13] RUN curl -fsSL https://deb.nodesource.com/setup_12.x | bash - apt-get install -y nodejs: #5 0.328 /usr/bin/apt-get: /usr/bin/apt-get: cannot execute binary file #5 0.440 curl: (23) Failed writing body (0 != 7027) ------ failed to solve: rpc error: code = Unknown desc = executor failed running [/bin/sh -c curl -fsSL https://deb.nodesource.com/setup_12.x | bash - apt-get install -y nodejs]: exit code: 126

2
If you're building a Node application, would it make more sense to start the image FROM node:12, instead of starting from a different language runtime?David Maze
@DavidMaze It's a Ruby on Rails app which is why i'm starting from a Ruby image. I just need to install some Yarn dependencies and one of them requires NodeJS v12+rctneil

2 Answers

1
votes

You can install a specific version of nodejs installing the official Node.js Binary Distributions, for the 12.x version try this:

# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_12.x | bash -
apt-get install -y nodejs

Reference: https://github.com/nodesource/distributions/blob/master/README.md#debinstall

0
votes

In your Docker-based use case, as you already have node.js installed and maybe npm as well, you could try installing a more recent version of node by directly relying on npm? I mean:

RUN npm install -g node@12

This strategy is mentioned here, among many other approaches, but it might not work, depending on how node.js has been installed in your base image.

Otherwise, a robust and user-wide way to update the version of node.js, consists in using nvm, the Node Version Manager.