I need to use a standard Alpine docker image and install a specific version of Node and NPM. Heres is my attempt so far:
FROM alpine:3.17.2
RUN apk update
RUN apk upgrade
RUN apk add bash git helm openssh yq github-cli
RUN apk add \
curl \
docker \
openrc
# nvm environment variables
ENV NVM_DIR /usr/local/nvm
ENV NVM_VERSION 0.39.3
ENV NODE_VERSION 18.16.0
# install nvm
# https://github.com/creationix/nvm#install-script
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v$NVM_VERSION/install.sh | bash
# install node and npm
RUN source $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
# add node and npm to path so the commands are available
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
RUN ls -asl $NVM_DIR/versions/node/v$NODE_VERSION/bin
RUN ls -asl $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules/npm/bin
RUN $NVM_DIR/versions/node/v$NODE_VERSION/bin/node -v
RUN $NVM_DIR/versions/node/v$NODE_VERSION/bin/npm install --global yarn
# Start docker on boot
RUN rc-update add docker boot
# Default commands to bash
ENTRYPOINT ["bash"]
I am getting this:
#7 [ 4/10] RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.39.3/install.sh | bash
#7 sha256:76a5a08c3c01075cd22585bc1f3df8f47fe258b116742db843cea6fa553a09c6
#7 0.181 % Total % Received % Xferd Average Speed Time Time Time Current
#7 0.182 Dload Upload Total Spent Left Speed
100 15916 100 15916 0 0 57463 0 --:--:-- --:--:-- --:--:-- 60287
#7 0.478 => Downloading nvm from git to '/usr/local/nvm'
=> Cloning into '/usr/local/nvm'...
#7 3.239 * (HEAD detached at FETCH_HEAD)
#7 3.240 master
#7 3.268 => Compressing and cleaning up git repository
#7 3.307
#7 3.338 => Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zprofile, ~/.zshrc, and ~/.profile.
#7 3.338 => Create one of them and run this script again
#7 3.338 OR
#7 3.338 => Append the following lines to the correct file yourself:
#7 3.338
#7 3.338 export NVM_DIR="/usr/local/nvm"
#7 3.338 [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
#7 3.338
#7 3.440 => Installing Node.js version 18.16.0
#7 4.948 Downloading and installing node v18.16.0...
#7 5.646 Downloading https://nodejs.org/dist/v18.16.0/node-v18.16.0-linux-x64.tar.gz...
######################################################################## 100.0%
#7 8.333 Computing checksum with sha256sum
#7 8.832 Checksums matched!
#7 11.93 Now using node v18.16.0 (npm v)
#7 12.41 Creating default alias: default -> 18.16.0 (-> v18.16.0 *)
#7 12.63 Failed to install Node.js 18.16.0
#7 12.63 => Close and reopen your terminal to start using nvm or run the following to use it now:
#7 12.63
#7 12.63 export NVM_DIR="/usr/local/nvm"
#7 12.63 [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
#7 DONE 12.7s
I am not sure about the "Failed to install Node.js 18.16.0" message, as you will see in my tests with "ls" it seems to be installed?
First I "ls" the bin directory where I expect node to be installed:
RUN ls -asl /usr/local/nvm/versions/node/v18.16.0/bin
#9 sha256:42ba843ab812861bf82e0e493e211095cc749408940b5ec21ce94fabe3997538
#9 0.138 total 88820
#9 0.141 4 drwxr-xr-x 2 1000 1000 4096 Apr 27 07:35 .
#9 0.141 4 drwxr-xr-x 6 root root 4096 Apr 27 07:35 ..
#9 0.141 0 lrwxrwxrwx 1 root root 45 Apr 27 07:35 corepack -> ../lib/node_modules/corepack/dist/corepack.js
#9 0.141 88812 -rwxr-xr-x 1 1000 1000 90940576 Apr 12 05:31 node
#9 0.141 0 lrwxrwxrwx 1 root root 38 Apr 27 07:35 npm -> ../lib/node_modules/npm/bin/npm-cli.js
#9 0.141 0 lrwxrwxrwx 1 root root 38 Apr 27 07:35 npx -> ../lib/node_modules/npm/bin/npx-cli.js
#9 DONE 0.1s
This seem ok to me, is it not?
My other "ls" gives this:
RUN ls -asl /usr/local/nvm/versions/node/v18.16.0/lib/node_modules/npm/bin
#10 sha256:c2872332dbb58f191400fa23211a691db9a5f5dc07425bc9d3c83bf1cafb31f8
#10 0.141 total 36
#10 0.144 4 drwxr-xr-x 3 1000 1000 4096 Apr 27 07:35 .
#10 0.144 4 drwxr-xr-x 7 1000 1000 4096 Apr 27 07:35 ..
#10 0.144 4 drwxr-xr-x 2 1000 1000 4096 Apr 27 07:35 node-gyp-bin
#10 0.144 4 -rwxr-xr-x 1 1000 1000 1365 Oct 11 2022 npm
#10 0.144 4 -rwxr-xr-x 1 1000 1000 54 Oct 11 2022 npm-cli.js
#10 0.144 4 -rwxr-xr-x 1 1000 1000 483 Oct 11 2022 npm.cmd
#10 0.144 4 -rwxr-xr-x 1 1000 1000 1567 Oct 11 2022 npx
#10 0.144 4 -rwxr-xr-x 1 1000 1000 2922 Dec 7 06:00 npx-cli.js
#10 0.144 4 -rwxr-xr-x 1 1000 1000 539 Oct 11 2022 npx.cmd
#10 DONE 0.2s
So npm is here also, looking good?
In my initial post here, I was giving the wrong error message, I am sorry. The error message was from my local machine, which is a Mac with the M2 CPU, and I believe this caused that error, but that is for another day. I need it to run on the build servers, where we are running linux on amd64 so there I have a different error, the following error:
Step 14/17 : RUN $NVM_DIR/versions/node/v$NODE_VERSION/bin/node -v
---> Running in d63ec3b287d9
/bin/sh: /usr/local/nvm/versions/node/v18.16.0/bin/node: not found
The command '/bin/sh -c $NVM_DIR/versions/node/v$NODE_VERSION/bin/node -v' returned a non-zero code: 127
Error: Process completed with exit code 127.
So my "ls" say node is there but when I try and run it, it is not?
Best regards
Søren
Here is my solution which is using unofficial node builds from https://nodejs.org/en/download
FROM alpine:3.17.2
ENV NODE_PACKAGE_URL https://unofficial-builds.nodejs.org/download/release/v18.16.0/node-v18.16.0-linux-x64-musl.tar.gz
RUN apk add libstdc++
WORKDIR /opt
RUN wget $NODE_PACKAGE_URL
RUN mkdir -p /opt/nodejs
RUN tar -zxvf *.tar.gz --directory /opt/nodejs --strip-components=1
RUN rm *.tar.gz
RUN ln -s /opt/nodejs/bin/node /usr/local/bin/node
RUN ln -s /opt/nodejs/bin/npm /usr/local/bin/npm
# npm version coming with node is 9.5.1
# To install specific npm version, run the following command, or remove it to use the default npm version:
RUN npm install -g npm@9.6.6
docker build -t node-18.16.0-npm-9.6.6-alpine-3.17.2 .
[+] Building 24.4s (15/15) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 753B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/alpine:3.17.2 0.7s
=> [ 1/11] FROM docker.io/library/alpine:3.17.2@sha256:ff6bdca1701f3a8a67e328815ff2346b0e4067d32ec36b7992c1fdc001dc8517 0.0s
=> CACHED [ 2/11] RUN apk add libstdc++ 0.0s
=> CACHED [ 3/11] WORKDIR /opt 0.0s
=> CACHED [ 4/11] RUN wget https://unofficial-builds.nodejs.org/download/release/v18.16.0/node-v18.16.0-linux-x64-musl.tar.gz 0.0s
=> CACHED [ 5/11] RUN mkdir -p /opt/nodejs 0.0s
=> [ 6/11] RUN tar -zxvf *.tar.gz --directory /opt/nodejs --strip-components=1 4.2s
=> [ 7/11] RUN rm *.tar.gz 0.3s
=> [ 8/11] RUN ls -l /opt/nodejs 0.6s
=> [ 9/11] RUN ln -s /opt/nodejs/bin/node /usr/local/bin/node 0.2s
=> [10/11] RUN ln -s /opt/nodejs/bin/npm /usr/local/bin/npm 0.2s
=> [11/11] RUN npm install -g npm@9.6.6 17.1s
=> exporting to image 1.0s
=> => exporting layers 0.9s
=> => writing image sha256:e133d992089c2efd071b423fa093f81bf4a732ac06c1ec7dd6cb09cec894a9b3 0.0s
=> => naming to docker.io/library/node-18.16.0-npm-9.6.6-alpine-3.17.2
docker run -it node-18.16.0-npm-9.6.6-alpine-3.17.2 node --version
v18.16.0
docker run -it node-18.16.0-npm-9.6.6-alpine-3.17.2 npm --version
9.6.6