-
-
Save remarkablemark/aacf14c29b3f01d6900d13137b21db3a to your computer and use it in GitHub Desktop.
#!/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 |
# 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 |
There is apparently an offical docker container for NVM. Here is the docker file link
I have created a Dockerfile for nvm v0.39.1 (latest today) and Ubuntu 22.04 by copying and pasting commands from the official Dockerfile into my custom container's Dockerfile
FROM ubuntu:22.04
# removed docker command for my stuff
USER ${user}
# we need a .bashrc for install.sh to work
RUN touch ~/.bashrc && chmod +x ~/.bashrc
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash ;
# this node version is old but I need it for my use case
ENV NODE_VERSION=12.16.1
# relevant code I pasted from above link
# nvm
RUN echo 'export NVM_DIR="$HOME/.nvm"' >> "$HOME/.bashrc"
RUN echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> "$HOME/.bashrc"
RUN echo '[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion' >> "$HOME/.bashrc"
# nodejs and tools
RUN bash -c 'source $HOME/.nvm/nvm.sh && \
nvm install $NODE_VERSION && nvm use $NODE_VERSION && nvm alias default $NODE_VERSION && \
npm install -g doctoc urchin eclint dockerfile_lint && \
npm install --prefix "$HOME/.nvm/" && \
npm install --global gulp-cli';
# I added gulp for my use case
# then the rest of the dockerfile
WORKDIR /home/${user}
# add your entrypoint
ENTRYPOINT ["path-to-my-shell-file"]
How to install nvm in Docker / devcontainer
FROM mcr.microsoft.com/vscode/devcontainers/cpp:ubuntu-20.04
RUN mkdir /usr/local/nvm
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 14.18.1
RUN curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash \
&& . $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
it worked for me
grinsteindavid commented 7 days ago •
How to install nvm in Docker / devcontainer
FROM mcr.microsoft.com/vscode/devcontainers/cpp:ubuntu-20.04
RUN mkdir /usr/local/nvm
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 14.18.1
RUN curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
&& . $NVM_DIR/nvm.sh
&& nvm install $NODE_VERSION
&& nvm alias default $NODE_VERSION
&& nvm use default
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
it worked for me
+1 Thanks
How to install nvm in Docker / devcontainer
FROM mcr.microsoft.com/vscode/devcontainers/cpp:ubuntu-20.04
RUN mkdir /usr/local/nvm ENV NVM_DIR /usr/local/nvm ENV NODE_VERSION 14.18.1 RUN curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash \ && . $NVM_DIR/nvm.sh \ && nvm install $NODE_VERSION \ && nvm alias default $NODE_VERSION \ && nvm use default ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
it worked for me
THANKS!!!! :)
If anyone still finding themselves wondering here, I made a working gist with the above + python2.7 for old-timer apps
https://gist.github.com/aviadhahami/910b7f51f9fd53b4a3624f888b2981c9
You can add the nvm to your docker-entrypoint.sh file. This will make nvm/npm work in any login, even /bin/sh. The exception is when using the image in a multi-stage build. See below.
. "$NVM_DIR/nvm.sh"
exec "$@" # Last line
Include it like:
COPY docker-entrypoint.sh .
ENTRYPOINT [ "/docker-entrypoint.sh" ]
Images setup with NVM that are used in a multi-stage build require nvm to be setup manually.
FROM some_nvm_image
RUN \. "$NVM_DIR/nvm.sh" \
&& npm --version \
&& node --version
But don't put a lot of time on such problems. NVM is a localdev tool. If you're building for production, either use an official node docker image, or borrow their Dockerfile to build your own.
I'm using the .nvmrc file to automatically install and use the node version, but after building my dockerfile, it is using always the Default version as default -> 18.3.0 (-> v18.3.0 *) even though using this version "v16.14.1". Could someone have any idea about this issue? please find the docker file and image building log below.
Dockerfile
Building docker image log