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
node:<version>-alpine
images? Also, I see you are using QEMU, which may interfere with the binaries that are being downloaded. – Crownnode:current-alpine
and you clearly seeFROM alpine
as a base. Don't reinvent the wheel. – Crown$NODE_VERSION
ARG (which can be passed todocker build/run
) from the proposed answer... – Peper