Repository navigation
fix: use a user-writable npm cache location - #2702
Closed
Cdineshreddy12 wants to merge 2 commits into
Closed
Cdineshreddy12 wants to merge 2 commits into
Cdineshreddy12 wants to merge 2 commits into
Conversation
npm defaults its cache to $HOME/.npm. When the image is run as a user without a home directory (an arbitrary --user UID on Jenkins, rootless Docker, or similar CI runners), HOME resolves to / and npm fails with EACCES trying to mkdir /.npm. Set NPM_CONFIG_CACHE=/tmp/.npm in the templates and the generated Dockerfiles so npm install works for any UID, including with a read-only root filesystem when a writable /tmp is mounted. Add build-test steps covering an arbitrary-UID npm install and a read-only root filesystem install. Fixes nodejs#2495
Explain why the images set NPM_CONFIG_CACHE=/tmp/.npm, the trade-off on cache persistence, and how to override it.
|
To anyone reviewing this - it's very poor workaround, and not real solution. See #2495 (comment) why. |
Author
|
Tightened the description above for precision on two points raised in the linked issue:
Fix and tests are unchanged — this only corrects the written rationale to be accurate. |
Member
|
Closing, on the basis that the author of the issue #2495 it states to resolve, rejects the PR. We can re-open if there is any member of the Node.js Docker team that would sponsor the PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Sets
NPM_CONFIG_CACHE=/tmp/.npmin the three templates and all generated Dockerfiles, and adds two build-test steps (arbitrary-UID install, read-only root filesystem install) so the behaviour stays covered.npm defaults its cache to
$HOME/.npm. When the image runs as a user with no home directory — an arbitrary--userUID on Jenkins, rootless Docker, GitHub/GitLab runners —HOMEresolves to/and everynpm installdies withEACCES: mkdir /.npm.Motivation and Context
Fixes #2495.
The obvious objection is that
NPM_CONFIG_CACHEis already the documented workaround, so baking it in looks like giving up on a "real" fix. I went through the alternatives before settling on this;/tmpis the default most likely to work without extra configuration, for reasons worth stating precisely:/usr/local/share/npm-cachewith mode 1777): on its own, in a single ephemeral container with nothing mounted there, this works fine — only one UID ever touches it. The problem shows up once that directory is persisted and reused across runs under different UIDs, which is a common CI pattern for speeding up repeated installs (a cache action, acache:key, a shared build-agent host mount). npm's cache (cacache) ties integrity bookkeeping to whichever UID wrote it first, and the next UID hits the same class ofEACCES/ "cache folder contains root-owned files" error this issue is about — not solved, just relocated to whoever reuses the cache.readOnlyRootFilesystem: true, since that setting only locks the container's own filesystem — an explicitly mounted volume stays writable at any path. The reason to default to/tmpspecifically is convention, not necessity: it's the one path that hardened setups (restricted Kubernetes pod security profiles, common CI runner images) already tend to mount a tmpfs/emptyDir at without the operator adding anything. Any other path needs that same mount, just pointed somewhere nonstandard./tmp/.npm-$UID) can't be expressed in a DockerfileENV— there's no runtime expansion.npm_config_userconfigon its own doesn't move the cache at all.So pointing the default cache at
/tmpisn't the workaround promoted out of convenience — it's the option that needs the least extra configuration to avoid the scenarios this issue and its persistent-cache cousin both report.Trade-offs, stated plainly:
NPM_CONFIG_CACHEor mount one, exactly as today.nodeuser previously cached persistently under/home/node/.npm; it now caches under/tmp/.npm. Same override applies if someone depends on the old location./tmp/.npm— unavoidable with a fixedENV, and a much narrower case than the single-UID-per-container CI runs this issue is about.Documentation: added an "npm cache" section to
docs/BestPractices.mdcovering the default location, the persistence trade-off, and how to override it.Testing Details
Built
24/bookworm-slimlocally and verified:node --version,npm --version, installs fine)npm installsucceeds as UID 1002 with no home directory (the Jenkins / rootless case)npm installsucceeds with--read-only --tmpfs /tmpas UID 1002 (thereadOnlyRootFilesystemcase)nodeuser (UID 1000) works as before, caching under/tmp/.npmThe two new
build-test.ymlsteps run the UID-1002 and read-only-rootfs installs against every image variant in the matrix, so a regression here fails CI.Example Output
Before (any arbitrary UID):
After:
Types of changes
Checklist