Skip to content

Commit b77fcec

Browse files
blolecdaringe
authored andcommitted
[readme] add docker tips
- covers installation using BASH_ENV Co-authored-by: Christopher Dieringer <[email protected]> Co-authored-by: Björn Holm <[email protected]>
1 parent 287d535 commit b77fcec

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

Dockerfile

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,20 @@ RUN echo 'nvm ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
9191
# Switch to user "nvm" from now
9292
USER nvm
9393

94+
# Create a script file sourced by both interactive and non-interactive bash shells
95+
ENV BASH_ENV /home/nvm/.bash_env
96+
RUN touch "$BASH_ENV"
97+
RUN echo '. "$BASH_ENV"' >> "$HOME/.bashrc"
98+
9499
# nvm
95-
RUN echo 'export NVM_DIR="$HOME/.nvm"' >> "$HOME/.bashrc"
96-
RUN echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> "$HOME/.bashrc"
97-
RUN echo '[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion' >> "$HOME/.bashrc"
100+
RUN echo 'export NVM_DIR="$HOME/.nvm"' >> "$BASH_ENV"
101+
RUN echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> "$BASH_ENV"
102+
RUN echo '[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion' >> "$BASH_ENV"
98103

99104
# nodejs and tools
100-
RUN bash -c 'source $HOME/.nvm/nvm.sh && \
101-
nvm install node && \
102-
npm install -g doctoc urchin eclint dockerfile_lint && \
103-
npm install --prefix "$HOME/.nvm/"'
105+
RUN nvm install node
106+
RUN npm install -g doctoc urchin eclint dockerfile_lint
107+
RUN npm install --prefix "$HOME/.nvm/"
104108

105109
# Set WORKDIR to nvm directory
106110
WORKDIR /home/nvm/.nvm

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [Installing and Updating](#installing-and-updating)
1919
- [Install & Update Script](#install--update-script)
2020
- [Additional Notes](#additional-notes)
21+
- [Installing in Docker](#installing-in-docker)
2122
- [Troubleshooting on Linux](#troubleshooting-on-linux)
2223
- [Troubleshooting on macOS](#troubleshooting-on-macos)
2324
- [Ansible](#ansible)
@@ -129,6 +130,25 @@ Eg: `curl ... | NVM_DIR="path/to/nvm"`. Ensure that the `NVM_DIR` does not conta
129130

130131
- 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'`
131132

133+
#### Installing in Docker
134+
135+
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.
136+
137+
```Dockerfile
138+
# Use bash for the shell
139+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
140+
141+
# Create a script file sourced by both interactive and non-interactive bash shells
142+
ENV BASH_ENV /home/user/.bash_env
143+
RUN touch "${BASH_ENV}"
144+
RUN echo '. "${BASH_ENV}"' >> ~/.bashrc
145+
146+
# Download and install nvm
147+
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | PROFILE="${BASH_ENV}" bash
148+
RUN echo node > .nvmrc
149+
RUN nvm install
150+
```
151+
132152
#### Troubleshooting on Linux
133153

134154
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.

0 commit comments

Comments
 (0)