Search code examples
node.jsdockerdockerfileazure-functionsnvm

How to refresh your shell when using a Dockerfile?


I am trying to build a Dockerfile that can make use of Azure functions. After unsuccessfully trying to build it using alpine:3.9 because of library issues, I swapped to ubuntu:18.04. Now I have a problem in that I can't install nvm (node version manager) in such a way that I can install node. My Dockerfile is below. I have managed to install nvm but now, while trying to use nvm, I cannot install the node version I want. The problem probably has to do with refreshing the shell but that is tricky to do as it appears that Docker continues to use the original shell it entered to run the next build stages. Any suggestions on how to refresh the shell so nvm can work effectively?

FROM ubuntu:18.04

RUN apt update && apt upgrade -y && apt install -qq -y --no-install-recommends \
    python-pip \
    python-setuptools \
    wget \
    build-essential \
    libssl-dev

RUN pip install azure-cli

RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash 

RUN . /root/.nvm/nvm.sh && nvm install 10.14.1 && node

ENTRYPOINT ["/bin/bash"]

Solution

  • After install nvm command put:

    SHELL ["/bin/bash", "--login" , "-c"]
    RUN nvm install 17
    SHELL ["/bin/sh", "-c"]
    

    Default shell is sh and first command switches it to bash. Parameter --login is required as you want to source .bashrc.

    As all subsequent commands would be executed with changed shell it's good to switch it back to sh if you don't need it anymore.