Last active
March 18, 2025 01:08
-
Star
(203)
You must be signed in to star a gist -
Fork
(44)
You must be signed in to fork a gist
-
-
Save remarkablemark/aacf14c29b3f01d6900d13137b21db3a to your computer and use it in GitHub Desktop.
Install node and npm with nvm using Docker.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# confirm docker daemon is running and connected | |
docker version | |
# build the image based on the Dockerfile and name it `nvm` | |
docker build -t nvm . | |
# confirm image is present | |
docker images | |
# enter container terminal | |
docker run -it nvm bash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set the base image to Debian | |
# https://hub.docker.com/_/debian/ | |
FROM debian:latest | |
# replace shell with bash so we can source files | |
RUN rm /bin/sh && ln -s /bin/bash /bin/sh | |
# update the repository sources list | |
# and install dependencies | |
RUN apt-get update \ | |
&& apt-get install -y curl \ | |
&& apt-get -y autoclean | |
# nvm environment variables | |
ENV NVM_DIR /usr/local/nvm | |
ENV NODE_VERSION 4.4.7 | |
# install nvm | |
# https://github.com/creationix/nvm#install-script | |
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/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 | |
# confirm installation | |
RUN node -v | |
RUN npm -v |
@gustavo-olegario-aba thanks as long as you had a good time, I'm good with that ๐
Here is my solution which doesn't care about version numbers but installs latest npm and node LTS, doesn't use any symlinks, doesn't mess up with
$PATH
and/bin/sh
but uses a login shell:FROM debian:buster ARG DEBIAN_FRONTEND=noninteractive SHELL ["/bin/bash", "-l", "-euxo", "pipefail", "-c"] RUN apt-get update; \ apt-get full-upgrade -y; \ apt-get install -y --no-install-recommends \ ca-certificates \ curl \ ; \ apt-get clean; \ rm -rf /var/lib/apt/lists/* ENV NVM_DIR /usr/local/nvm RUN mkdir -p "$NVM_DIR"; \ curl -o- \ "https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh" | \ bash \ ; \ source $NVM_DIR/nvm.sh; \ nvm install --lts --latest-npm RUN command -v nvm; \ command -v node; \ node --version; \ command -v npm; \ npm --version
Thanks to https://www.theguild.nl/nvm-in-docker/ to show a more elegant way by using a login shell.
This one worked to me. Thanks @bheisig !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It was not exactly what I was looking for, but it was worthy the time for sure