Install nodejs in docker container and auto detect architecture

Written by James McDonald

February 21, 2022

Installing node binary package from https://nodejs.org/en/download/

I use Docker on my x86_64 Ubuntu Workstation and Macbook Pro M1 aarm64 so Dockerfiles need to be multi-arch

This Dockerfile snippet detects the architecture we are running in and sets the correct DISTRO environment variable so we don’t have to hard code it.

# hard code VERSION 
# ARG doesn't persist into the future container like ENV does
ARG VERSION=v16.14.0

RUN apArch=$(arch); \
    case "$apArch" in \
        x86_64) export DISTRO='linux-x64' ;; \
        aarch64) export DISTRO='linux-arm64' ;;\
        esac;\ 
    cd /build && \
    wget https://nodejs.org/dist/v16.14.0/node-${VERSION}-${DISTRO}.tar.xz && \
    tar -xvf node-${VERSION}-${DISTRO}.tar.xz --strip-components=1 -C /usr/local

npm builds fail on v17+ of node

Note: For the moment I am staying with node v16 LTS because when you run v17 you have to use --openssl-legacy-provider in your package.json build / run args to work around deprecated SSL

 "scripts": {
    "start": "react-scripts --openssl-legacy-provider start",
    "build": "react-scripts --openssl-legacy-provider build && rm -rf ../../../webroot/shipment-app && cp -rv build ../../../webroot/shipment-app",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

Very useful blog post about ENV, ARG etc here https://vsupalov.com/docker-build-pass-environment-variables/

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…