Search code examples
node.jsdockernvm

How to execute nvm from docker run


I have a bare docker image with globally installed nvm and latest lts nodejs. Everything operates as expected except one thing. I am not able to execute nvm using docker run:

docker run -ti myimage:latest nvm

This produces output

--: line 0: exec: nvm: not found

Here is the Dockerfile

FROM ubuntu:20.04
ARG DEBIAN_FRONTEND=noninteractive
ENV NVM_DIR='/usr/local/nvm'
RUN mkdir -pv "$NVM_DIR" && \
    apt-get -yq clean && apt-get -q update && \
    apt-get -yq --no-install-recommends install curl ca-certificates git-core && \
    curl -o- 'https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh' | bash && \
    . "$NVM_DIR/nvm.sh" && \
    nvm install --default --latest-npm 'lts/*' && \
    apt-get -yq clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
ENTRYPOINT ["bash", "-c", ". /usr/local/nvm/nvm.sh && { [ \"x$*\" = 'x' ] || { exec ${@} ; exit $? ; } ; } && bash", "--"]

Solution

  • OK, I figured this one out. The reason nvm does not execute is because it is not executable at all but preloaded bash function so it has to be executed directly and not through exec. This is what works:

    FROM ubuntu:20.04
    ARG DEBIAN_FRONTEND=noninteractive
    ENV NVM_DIR='/usr/local/nvm'
    RUN mkdir -pv "$NVM_DIR" && \
        apt-get -yq clean && apt-get -q update && \
        apt-get -yq --no-install-recommends install curl ca-certificates git-core && \
        curl -o- 'https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh' | bash && \
        . "$NVM_DIR/nvm.sh" && \
        nvm install --default --latest-npm 'lts/*' && \
        apt-get -yq clean && \
        rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
    ENTRYPOINT ["bash", "-c", ". /usr/local/nvm/nvm.sh && { [ \"x$*\" = 'x' ] || { ${@} ; exit $? ; } ; } && bash", "--"]