[readme] add docker tips by cdaringe · Pull Request #2318 · nvm-sh/nvm · GitHub
Skip to content

[readme] add docker tips - #2318

Merged
ljharb merged 1 commit into
nvm-sh:masterfrom
cdaringe:docs/install-in-docker
Feb 3, 2025
Merged

[readme] add docker tips#2318
ljharb merged 1 commit into
nvm-sh:masterfrom
cdaringe:docs/install-in-docker

Conversation

@cdaringe

Copy link
Copy Markdown
Contributor

Problem

It's common to install node in docker, and using NVM is quite helpful in this regard. Installing and using NVM isn't always. straightforward.

Solution

Make it easier for users to setup nvm in docker by droppin' some docs.

Ref: https://stackoverflow.com/questions/25899912/how-to-install-nvm-in-docker

@ljharb ljharb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread README.md Outdated
@cdaringe

Copy link
Copy Markdown
Contributor Author

Comment thread README.md Outdated
Comment on lines +97 to +98
# patch PATH
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not belong in docs; nobody should be manually editing the PATH, only sourcing nvm.sh and letting nvm do it.

Suggested change

it's also not necessary here since nvm install handles it in the next RUN statement.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't each run in docker happen in a new shell? iirc, mutating your env in a RUN doesn't persist in downstream runs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - but instead of hardcoding the PATH, is there a way the RUN that sources nvm.sh could export the resulting PATH for further RUNs to use?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb @cdargine ENV PATH="$NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH" sholdnt this export the path to resulting run commands ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

he's looking for something like ENV=$(/path/to/nvm env) I think. Feel free to push to or close this or, btw.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb, i have been thinking about it, and I don't see a way forward otherwise, which is disappointing. ENV stanzas do not accept child commands, so we are left with only build args, the file system, and RUN blocks as sources of dynamic input, however only build arg inputs can be pulled into the image ENV definition from what i can tell. or, better said, i cannot find a way to dynamically promote content from within a build step to the image ENV. if ENV PATH=$PATH;$(nvm path) worked, we would have a clean solve. it does not.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, a paragraph in the docs that explains these things clearly (including to me, who has minimal docker experience) might be a good way to resolve this PR?

@blole blole Nov 8, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I found a pretty neat way of installing nvm in a docker container by leveraging the bash environment variable $BASH_ENV, this way we don't even need to make the bash in the container into an interactive/login shell.

FROM ubuntu:22.10
SHELL ["/bin/bash", "-o", "pipefail", "-c"]

ENV BASH_ENV /root/.env
RUN touch $BASH_ENV
RUN echo '. "'"$BASH_ENV"'"' >> ~/.bashrc # only needed if executing interactively into the container

RUN apt-get update && apt-get install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | PROFILE="$BASH_ENV" bash

COPY .nvmrc .nvmrc
RUN nvm install
RUN node -v

This way nvm+node is easily available both later in the Dockerfile, and when executing into the container

> echo 19.0.1 > .nvmrc
> docker build -t foo .
...
Step 9/10 : RUN nvm install
Found '//.nvmrc' with version <19.0.1>
...
Now using node v19.0.1 (npm v8.19.2)
Creating default alias: default -> 19.0.1 (-> v19.0.1 *)
...
Step 10/10 : RUN node -v
v19.0.1
> docker run --rm -it --entrypoint bash foo
root@4e5088bdde4d:/# tail -1 ~/.bashrc
. "/root/.env"
root@4e5088bdde4d:/# node -v
v19.0.1

Maybe it's neat enough to fit in the readme?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does seem pretty compelling.

Do you think that would be a replacement for this PR? If so, please post a link to a branch (NOT a new PR) and i'll pull in the changes. If not, please file a new PR :-)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think it could be.

Sure!
https://github.com/blole/nvm/tree/docs/install-in-docker

I tried my hand at updating the Dockerfile in the repo as well, it seems to work like I'd expect, but I'm not sure what people use it for, so I can't really verify that.

 - covers installation using BASH_ENV

Co-authored-by: Christopher Dieringer <cdaringe@users.noreply.github.com>
Co-authored-by: Björn Holm <blollle@gmail.com>
@ljharb
ljharb marked this pull request as draft December 23, 2022 20:24
@ljharb
ljharb force-pushed the master branch 2 times, most recently from c6cfc3a to c20db2a Compare June 10, 2024 18:13
@ljharb
ljharb force-pushed the docs/install-in-docker branch from 8b14f36 to b77fcec Compare February 3, 2025 19:53
@ljharb
ljharb marked this pull request as ready for review February 3, 2025 19:53
@ljharb ljharb changed the title docs: add docker tips [readme] add docker tips Feb 3, 2025
@ljharb
ljharb merged commit b77fcec into nvm-sh:master Feb 3, 2025
ljharb added a commit that referenced this pull request Mar 11, 2025
New Stuff
 - `install.sh`: add `$ZDOTDIR` to zsh search (#3458)

Fixes
 - `reinstall-packages`: do not reinstall corepack (#3544)
 - avoid bash-specific syntax (#3499)
 - `install-latest-npm`: npm v11 is out
 - `nvm_install_latest_npm`: avoid unbound variable (#3447)
 - give a more helpful message when `lts` alias is mistakenly used (#3441)
 - `nvm ls`, `nvm alias`, `nvm install`: error when an LTS name is invalid
 - `nvm_normalize_lts`: error when an LTS name is not lowercase (#3417)

Documentation
 - [readme] update link
 - [readme] fix `--no-use` example (#3479)
 - [readme] update copyright notice (#3507)
 - [readme] note zsh-nvm's AUTO_USE option (#2670)
 - [readme] add note about reloading zshrc after editing (#3052)
 - [readme] Update shell profile file install notes (#2241)
 - [readme] add docker tips (#2318)
 - [readme] remove `avn` from readme (#3469)
 - [readme] fnm -> nvm.fish (#2541)

Refactors
 - prefer `case` over if/else chains
 - combine `sed -e` invocations/arguments

Tests
 - `nvm exec`/`nvm run`: add `--silent` tests (#1259)
 - [actions] release test needs git tags
 - migrate `installation_iojs` test suite to GitHub Actions (#3476)
 - Migrate slow test suite from Travis CI (#3470)
 - temporarily skip this failing travis test to unblock progress
 - [actions] TOC: use latest LTS node
 - `install.sh`: clean up `nvm_detect_profile` tests
 - `nvm_detect_profile`: refactor (#3467)
 - run urchin tests on pull requests (#3466)
 - update mocks
 - ensure that unit tests use only mocked LTS names
 - [actions] use `node/install` instead of `node/run`

Meta
 - disable blank issues
 - update issue template
 - add DCO (#3456)
 - Rename .github/ISSUE_TEMPLATE.md to .github/ISSUE_TEMPLATE/ISSUE_TEMPLATE.md (#3454)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants