Skip to content

Fix: Pre-create node_modules with correct ownership for non-root users#2485

Open
mulder999 wants to merge 1 commit intonodejs:mainfrom
mulder999:main
Open

Fix: Pre-create node_modules with correct ownership for non-root users#2485
mulder999 wants to merge 1 commit intonodejs:mainfrom
mulder999:main

Conversation

@mulder999
Copy link
Copy Markdown

@mulder999 mulder999 commented May 6, 2026

Description

This PR ensures that the node_modules directory is explicitly created and owned by the node user during the image build process.

Currently, if a volume is mounted to /home/node/node_modules and the directory does not exist in the image, Docker creates it with root:root ownership. This prevents the non-root node user from performing npm install or updating dependencies at runtime, leading to EACCES errors.

Changes

Updated the user creation layer to:

  • Pre-emptively create the /home/node/node_modules directory with node:node permissions.

Why this is necessary

When running in environments like Docker Swarm or Kubernetes, persistent volumes or bind mounts are often used to cache node_modules. If the container process runs as a non-root user (UID 1000), it lacks the privileges to modify directories created by the Docker daemon (UID 0). By defining this directory in the Dockerfile, we establish the correct metadata before any volume masking occurs.

Testing Details

docker run --rm -u node -v test_volume:/home/node/node_modules  node:<tag>  sh -c "touch /home/node/node_modules/.test_file && ls -la /home/node/node_modules/.test_file" 

Original behavior (problem)

touch: cannot touch '/home/node/node_modules/.test_file': Permission denied

Improved behavior (this PR)

-rw-r--r--    1 node     node            0 May  6 15:35 /home/node/node_modules/.test_file

Types of changes

  • Documentation
  • Version change (Update, remove or add more Node.js versions)
  • Variant change (Update, remove or add more variants, or versions of variants)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Other (none of the above)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING.md document.
  • All new and existing tests passed.

Ensure the node_modules folder exists and is owned by the 'node' user
to prevent permission issues (EACCES) when mounting volumes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant