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.

You May Also Like…

List your VSCode Extensions

Ever wondered what extensions you have installed and want to keep a list? This actually is a good way to audit your...

array_merge vs the + operator

<?php $options = [ 'rootNode' => 'response' ]; // array_merge // the same key appearing later will overwrite echo...