diff --git a/Dockerfile b/Dockerfile index d95bb1382a..97ff98ccb6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -91,16 +91,20 @@ RUN echo 'nvm ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers # Switch to user "nvm" from now USER nvm +# Create a script file sourced by both interactive and non-interactive bash shells +ENV BASH_ENV /home/nvm/.bash_env +RUN touch "$BASH_ENV" +RUN echo '. "$BASH_ENV"' >> "$HOME/.bashrc" + # 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" +RUN echo 'export NVM_DIR="$HOME/.nvm"' >> "$BASH_ENV" +RUN echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> "$BASH_ENV" +RUN echo '[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion' >> "$BASH_ENV" # nodejs and tools -RUN bash -c 'source $HOME/.nvm/nvm.sh && \ - nvm install node && \ - npm install -g doctoc urchin eclint dockerfile_lint && \ - npm install --prefix "$HOME/.nvm/"' +RUN nvm install node +RUN npm install -g doctoc urchin eclint dockerfile_lint +RUN npm install --prefix "$HOME/.nvm/" # Set WORKDIR to nvm directory WORKDIR /home/nvm/.nvm diff --git a/README.md b/README.md index 4d152d1eb0..291fa427bb 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ - [Installing and Updating](#installing-and-updating) - [Install & Update Script](#install--update-script) - [Additional Notes](#additional-notes) + - [Installing in Docker](#installing-in-docker) - [Troubleshooting on Linux](#troubleshooting-on-linux) - [Troubleshooting on macOS](#troubleshooting-on-macos) - [Ansible](#ansible) @@ -129,6 +130,25 @@ Eg: `curl ... | NVM_DIR="path/to/nvm"`. Ensure that the `NVM_DIR` does not conta - You can instruct the installer to not edit your shell config (for example if you already get completions via a [zsh nvm plugin](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/nvm)) by setting `PROFILE=/dev/null` before running the `install.sh` script. Here's an example one-line command to do that: `PROFILE=/dev/null bash -c 'curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash'` +#### Installing in Docker + +When invoking bash as a non-interactive shell, like in a Docker container, none of the regular profile files are sourced. In order to use `nvm`, `node`, and `npm` like normal, you can instead specify the special `BASH_ENV` variable, which bash sources when invoked non-interactively. + +```Dockerfile +# 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 /home/user/.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.39.2/install.sh | PROFILE="${BASH_ENV}" bash +RUN echo node > .nvmrc +RUN nvm install +``` + #### Troubleshooting on Linux On Linux, after running the install script, if you get `nvm: command not found` or see no feedback from your terminal after you type `command -v nvm`, simply close your current terminal, open a new terminal, and try verifying again.