Hey everyone,
I’m having trouble with an old docker setup that I need to update for some yearly maintenance work. When I try to execute this docker command:
docker run --rm -p 8080:8080 -v "${PWD}:/workdir" -v /workdir/node_modules myimage:dev npm run build
I keep getting this permission error:
> [email protected] build
> vue-cli-service build
npm ERR! code EACCES
npm ERR! syscall open
npm ERR! path /home/node/.npm/_cacache/tmp/a42891f7
npm ERR! errno -13
npm ERR!
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR!
npm ERR! To permanently fix this problem, please run:
npm ERR! sudo chown -R 1000:1000 "/home/node/.npm"
npm ERR! Log files were not written due to an error writing to the directory: /home/node/.npm/_logs
npm ERR! You can rerun the command with `--loglevel=verbose` to see the logs in your terminal
The problem is that when I try to run the suggested fix sudo chown -R 1000:1000 /home/node/.npm inside the container, I get:
/workdir $ sudo chown -R 1000:1000 /home/node/.npm
ash: sudo: not found
The container runs as the node user (uid 1000, gid 1000) but doesn’t have sudo installed. Has anyone dealt with this npm cache permission issue in docker before? Not sure if this is more of a docker problem or npm problem.
I encountered a similar npm cache permission issue while working with Docker. In this case, it seems the cache files were created with incorrect ownership in previous builds, which is causing the error. One workaround is to include the command npm cache clean --force before executing your build command; this clears the cache. Alternatively, you can modify your docker run command by adding --user $(id -u):$(id -g) to run the container with the host user’s permissions. If you are able to rebuild the Docker image, including a line like RUN chown -R node:node /home/node/.npm in your Dockerfile can also help, ensuring that ownership is set correctly. For a more permanent solution, consider using a named volume specifically for the npm cache and setting the correct ownership upon container startup.
Classic Docker layer caching issue - I’ve hit this tons of times. Your npm cache got corrupted with root ownership from a previous container run. Probably happened when the container accidentally ran as root at some point. Since you can’t sudo inside the container, easiest fix is mounting a fresh volume for the npm cache. Add -v npm-cache:/home/node/.npm to your docker run command - creates a clean named volume. If that doesn’t work right away, try npm config set cache /tmp/.npm before your build command. Temporarily redirects the cache somewhere writeable. Alternatively, rebuild your image with USER root temporarily, run the chown command during build, then switch back to USER node. This fixes ownership at the image level instead of runtime.
had that problem too! best bet is using --user root in your docker run so you can chown without sudo. once you’ve fixed the permissions, switch back to the node user. a lil script to automate that could save time too!