I have a dockerfile like this:
FROM node:alpine
WORKDIR /app
COPY package.json .
RUN npm install
VOLUME [ "/app/node_modules" ]
COPY . .
ENTRYPOINT [ "npm" ]
CMD [ "run", "dev" ]
I build my image by using the command below:
docker build -t my-vue-app .
Then I run my container by using the command below:
docker run -v $(pwd):/app --name app -it --rm my-vue-app
It outputs a permission error:
error when starting dev server:
Error: EACCES: permission denied, mkdir '/app/node_modules/.vite'
at Object.mkdirSync (node:fs:1024:3)
at optimizeDeps (/app/node_modules/vite/dist/node/chunks/dep-efe32886.js:65416:21)
at runOptimize (/app/node_modules/vite/dist/node/chunks/dep-efe32886.js:69180:54)
at Server.httpServer.listen (/app/node_modules/vite/dist/node/chunks/dep-efe32886.js:69194:23)
at processTicksAndRejections (node:internal/process/task_queues:94:5)
I override my entrypoint to access my container's file system to check permissions.
docker run -v $(pwd):/app --name app -it --rm --entrypoint /bin/sh my-vue-app
/app # ls -l | grep node_modules
I can see the owner and group of node_modules is root
Output:
drwxr-xr-x 52 root root 4096 Mar 18 20:04 node_modules
All the other files have owner and group node
/app # ls -l
total 28
-rw-rw-r-- 1 node node 154 Mar 18 20:02 Dockerfile
-rw-r--r-- 1 node node 337 Mar 18 19:26 index.html
drwxr-xr-x 52 root root 4096 Mar 18 20:04 node_modules
-rw-r--r-- 1 node node 307 Mar 18 19:26 package.json
drwxr-xr-x 2 node node 4096 Mar 18 19:26 public
drwxr-xr-x 4 node node 4096 Mar 18 19:26 src
-rw-r--r-- 1 node node 156 Mar 18 19:26 vite.config.js
So, I wonder why does it happen and what can I do to solve it? Also one thing is, when I use node:lts instead of node:alpine, user and group of node_modules is still root but it works fine.
Currently node:lts is node:14.16.0.