nvm-sh/nvm

PATH wrong (node not found) when running container-image with non-interactive shell like in Gitlab CICD-Jobs

zenhighzer opened this issue · 4 comments

Hi,

we followed your guide with a slightly modification:

https://github.com/nvm-sh/nvm?tab=readme-ov-file#installing-in-docker:

FROM ubuntu:latest
ARG NODE_VERSION=20

RUN apt update && apt install -y curl

# Use bash for the shell
SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# Create a script file sourced by both interactive and non-interactive bash shells
ENV BASH_ENV=/root/.bash_env
RUN touch "${BASH_ENV}"
RUN echo '. "${BASH_ENV}"' >> ~/.bashrc

# Download and install nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | PROFILE="${BASH_ENV}" bash
RUN echo node > .nvmrc
RUN nvm install $NODE_VERSION && npm install --global yarn

But running the container like 'docker run --rm -it testnvm yarn -v' throws some error:

docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: exec: "yarn": executable file not found in $PATH: unknown

If we start Container interactively the PATH is OK;

docker run --rm -it testnvm:latest bash
root@7a71be1a2bec:/# yarn -v
1.22.22
root@7a71be1a2bec:/# 

So we asked gpt for help. after several tries this Dockerfile seems to be working fine with interactive and non-interactive containers:

FROM ubuntu
ARG NODE_VERSION=20

# install curl 
RUN apt update && apt install curl -y 

# install nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash

# set env
ENV NVM_DIR=/root/.nvm

# install nodejs and yarn
RUN bash -c "source $NVM_DIR/nvm.sh && \
    nvm install $NODE_VERSION && \
    nvm alias default $NODE_VERSION && \
    nvm use default && \
    npm install -g yarn"

# test node and yarn installation
RUN bash -c "source $NVM_DIR/nvm.sh && node -v && yarn -v"

# set ENTRYPOINT for reloading nvm, node-env, etc. everytime
ENTRYPOINT ["bash", "-c", "source $NVM_DIR/nvm.sh && exec \"$@\"", "--"]

# set cmd to bash 
CMD ["/bin/bash"]

So I wonder if you want to update your example for a more robust nodejs-container.

Best Regards

If you omit the yarn parts, a PR to make that change would be great! that way it can get proper review.

Hi,

I think i dont have permissions to create PR. I am not very familiar with github tbh.
I updated the README / added a further docker-section.

Maybe you could have a look and create a PR?

README.md

Everyone has permissions to make a PR :-) I’ll take a look, i can probably apply your patch.

damn, next time i will try to create a PR :-)
Thank you very much! :-)