(EACCES: permission denied, mkdir '/usr/app/node_modules/.cache) How can I create a docker-compose file to make node_modules a non-root folder?
Adding this line just after RUN npm install
in your Dockerfile would solve the issue:
RUN mkdir -p node_modules/.cache && chmod -R 777 node_modules/.cache
Final Dockerfile
FROM node:alpine
WORKDIR /usr/app
COPY package.json .
RUN npm install
RUN mkdir node_modules/.cache && chmod -R 777 node_modules/.cache
COPY . .
CMD ["npm", "run", "start"]
Then you don't need to copy the node_modules
folder from your local dir to the container. You can safely bookmark it.
Docker: EACCES: permission denied, mkdir '/app/node_modules/.cache'
try this
Docker - EACCES: permission denied, mkdir '/app/node_modules/.cache'
this worked for me
RUN chown -R node:node /app/node_modules
or sh into docker container and run
chown -R node:node /app/node_modules
EACCES: permission denied mkdir ... while trying to use docker volumes in a node project
So, what I gathered from looking around and seeing other people's Dockerfiles and a bit of running commands in a bash terminal in my container, is that the node:alpine
image I used as base, creates a user named node
inside the container.
The npm run start
is executed as a process of node
user(process with the UID
of node) which does not have root privileges (which I found out while looking in the htop
), so in order to make node
user the owner of /app directory I added this in the docker file-
RUN chown -R node.node /app
The updated Dockerfile.dev is-
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
RUN chown -R node.node /app
COPY . .
CMD ["npm", "run", "start"]
This has fixed the problem for me, hope it can help someone else too. :)
Docker: npm run start causing error, EACCES: permission denied, open '/home/node/node_modules/.cache/.eslintcache
It is clear that node_modules folder in container is built by root user during the step npm install, therefore has root as user.
This is the reason we don't have access to that folder when we set up our node user.
To resolve this what we have to do is firstly using the root user we have to give permission to the node user while copying files from local directory to image and then later set up node as the user as shown below:
COPY --chown=node:node package.json .
RUN npm install
COPY --chown=node:node . .
USER node
Docker EACCES permission denied mkdir
COPY
normally copies things into the image owned by root, and it will create directories inside the image if they don't exist. In particular, when you COPY ./api/package.json ./api/
, it creates the api
subdirectory owned by root, and when you later try to run yarn install
, it can't create the node_modules
subdirectory because you've switched users.
I'd recommend copying files into the container and running the build process as root. Don't chown
anything; leave all of these files owned by root. Switch to an alternate USER
only at the very end of the Dockerfile, where you declare the CMD
. This means that the non-root user running the container won't be able to modify the code or libraries in the container, intentionally or otherwise, which is a generally good security practice.
FROM node:alpine
# Don't RUN mkdir; WORKDIR creates the directory if it doesn't exist
WORKDIR /usr/src/node-app
# All of these files and directories are owned by root
COPY package.json yarn.lock ./
COPY ./api/package.json ./api/
COPY ./iso/package.json ./iso/
# Run this installation command still as root
RUN yarn install --pure-lockfile
# Copy in the rest of the application, still as root
COPY . .
# RUN yarn build
# Declare how to run the container -- _now_ switch to a non-root user
EXPOSE 3000
USER node
CMD yarn start
Related Topics
Using Curl with Commands in Go
How to Execute a Command in a Bash Script and Then Focus The Appearing Window
Sending Snmp2 Trap Message from Linux Command Lne
How to Create a File in Assembly with a Dynamically Specified File Path
"Nothing to Commit (Working Directory Clean)" When a Folder Has Been Added
Linux Kernel Hardware Break Points
Finding and Listing Duplicate Words in a Plain Text File
How to Install and Run Tacotron2 on Ubuntu Wsl
Re-Stream a Mpeg2 Ts Pal Stream with Crtmpserver
Is It Secure to Rely on "X-Forwarded-For" to Restrict Access by Ip in Apache While Using Cloudflare
Xfs - How to Not Modify Mtime When Writing to File
Unexpected Periodic Behaviour of an Ultra Low Latency Hard Real Time Multi-Threaded X86 Code
Kate Text Editor Cannot Handle Lines Longer Than 1024
Write Something to Linux Hid Device
How to Compile Redis So That I Can Upload and Run It on Shared Hosting
How to Type "Cargo Run" Without Needing to Set The Ld_Library_Path Shell Variable