[英]Access denied issue in docker container node application
I have a MERN stack application.我有一个 MERN 堆栈应用程序。 I deployed the app on render cloud.我在渲染云上部署了应用程序。 I noticed that my image upload functionality is not working but works on local machine.我注意到我的图片上传功能不起作用,但可以在本地机器上使用。 I 'dockerized' the node application.我“码头化”了节点应用程序。 The error message I got when I tried to upload image is this:我尝试上传图片时收到的错误消息是这样的:
Error: EACCES: permission denied, open 'images/1666890887146download.png'
First, I am using multer
npm package to handle image upload.首先,我使用multer
npm 包来处理图片上传。 The image is saved inside a folder called image
in my api
project folder.图像保存在我的api
项目文件夹中名为image
的文件夹中。 I think because of the user access in dockerfile, that is why the access is denied.我认为由于 dockerfile 中的用户访问,这就是访问被拒绝的原因。 Here is my dockerfile:这是我的码头文件:
FROM node:lts-alpine
WORKDIR /app
COPY package*.json ./
COPY client/package*.json client/
RUN npm run install-client --only=production
COPY api/package*.json api/
RUN npm run install-api --only=production
COPY client/ client/
RUN npm run client-build --prefix client
COPY api/ api/
USER node
CMD [ "npm", "start", "--prefix", "api" ]
EXPOSE 5000
Here is my multer
image upload function:这是我的multer
图上传功能:
const ALLOWED_FORMATS = ['image/jpeg', 'image/png', 'image/jpg'];
//login to upload images using libery called multer
const storage = multer.diskStorage({//choosing destination of the file
destination:(req, file, cb) =>{
if(ALLOWED_FORMATS.includes(file.mimetype)){
cb(null, "./images")
}else{
cb(new Error('Not supported file type!'), false)
}
}, filename:(req, file,cb)=> {//choosing file name
cb(null, req.body.name)
}
});
const upload = multer({storage:storage})
I am sensing that the node
User I assigned in dockerfile
does not have access to read and write file in the image
folder.我感觉到我在dockerfile
中分配的node
用户无权读取和写入image
文件夹中的文件。 My logics rely on that because after uploading the image from the image
folder to cloudinary, I also deleted it immediately.我的逻辑依赖于此,因为在将图像从image
文件夹上传到 cloudinary 后,我也立即将其删除。 Is there a way I can assign node
User the permission to read and write files in image folder?有没有办法可以为node
用户分配在图像文件夹中读取和写入文件的权限? I am not too familiar with docker.我对docker不太熟悉。
Yes, you can add the node user to the sudo group...是的,您可以将节点用户添加到 sudo 组...
sudo usermod -aG sudo $USER
or you can give permissions to the user using something like或者您可以使用类似的方式向用户授予权限
chown -R $USER:$USER images/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.